calling java static member using string?

117 views
Skip to first unread message

Daniel Meneses Báez

unread,
Aug 13, 2013, 6:14:13 PM8/13/13
to clo...@googlegroups.com
Hi :)

I really want to know if there is a way to do this:

(ns ...
   (:import [java.util Calendar]))

(defsomething ;; if it is possible using a macro I'm ok with that
   calendar-member
   [member]
    (symbol (str "Calendar/" member)))

what I want to know if an instance of Calendar "isMonday", "isFriday" "isSunday" etc...

so I was thinking to write something like

(defn- isss [day instant]
    (= (.get instant Calendar/DATE) (calendar-member day)))

and then use it like (def is-friday (partial isss 'FRIDAY)) ;;

am I being to crazy?

btw I'm really loving the language.



--
Daniel Meneses Báez

Daniel Meneses Báez

unread,
Aug 13, 2013, 6:31:34 PM8/13/13
to clo...@googlegroups.com
ok I'm trying

CompilerException java.lang.NoSuchFieldException: s, compiling:(NO_SOURCE_PATH:1:1)

user> (defmacro is [s instant]
        `(= (.get ~instant Calendar/DAY_OF_WEEK)
            (. Calendar ~s)))
#'user/is
user> (is 'TUESDAY (Calendar/getInstance))
CompilerException java.lang.RuntimeException: Unable to resolve symbol: TUESDAY in this context, compiling:(NO_SOURCE_PATH:1:1)

user>
user>
user> (defmacro is [s instant]
        `(= (.get ~instant Calendar/DAY_OF_WEEK)
            (. Calendar ~s)))
#'user/is
user> (defmacro is [s instant]
        `(= (.get ~instant Calendar/DAY_OF_WEEK)
            (. Calendar (symbol ~s))))
#'user/is
user> (is "TUESDAY" (Calendar/getInstance))
CompilerException java.lang.IllegalArgumentException: No matching method: symbol, compiling:(NO_SOURCE_PATH:1:1)

user>


isn't working :[

--
Daniel Meneses Báez

Sean Corfield

unread,
Aug 13, 2013, 8:21:39 PM8/13/13
to clo...@googlegroups.com
Perhaps clj-time might help you?

https://github.com/clj-time/clj-time

(ns time.core
(:require [clj-time.core :as time]
[clj-time.local :as local]
[clj-time.predicates :as p]))

(p/monday? (time/now)) ;; false
(p/tuesday? (time/now)) ;; false
(p/wednesday? (time/now)) ;; true (for me in California since (time/now) is UTC)

(p/monday? (local/local-now)) ;; false
(p/tuesday? (local/local-now)) ;; true (for me)
(p/wednesday? (local/local-now)) ;; false (not yet in California)

Sean
> --
> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clo...@googlegroups.com
> Note that posts from new members are moderated - please be patient with your
> first post.
> To unsubscribe from this group, send email to
> clojure+u...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en
> ---
> You received this message because you are subscribed to the Google Groups
> "Clojure" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to clojure+u...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>



--
Sean A Corfield -- (904) 302-SEAN
An Architect's View -- http://corfield.org/
World Singles, LLC. -- http://worldsingles.com/

"Perfection is the enemy of the good."
-- Gustave Flaubert, French realist novelist (1821-1880)

Daniel Meneses

unread,
Aug 14, 2013, 11:19:22 AM8/14/13
to clo...@googlegroups.com
Hi!

Thanks for your answer Sean I got it solved using clj-time

Also I found the problem with my macro attempt


user> (defmacro is
        [s instant]
        `(= (.get ~instant Calendar/DAY_OF_WEEK)
            (. Calendar ~s)))
#'current-day.core/is
user> (is FRIDAY (Calendar/getInstance))
false
user> (is WEDNESDAY (Calendar/getInstance))
true

I was quoting the symbol !!

Jim - FooBar();

unread,
Aug 14, 2013, 11:33:19 AM8/14/13
to clo...@googlegroups.com
why on earth is this a macro and not a regular fn?


Jim

Daniel Meneses Báez

unread,
Aug 14, 2013, 11:45:38 AM8/14/13
to clo...@googlegroups.com
I don't know if you have a differente approach, but as a defn it doesn't work

user> (import '[java.util Calendar])
java.util.Calendar
user> (defn is [s instant]
        (= (.get instant Calendar/DAY_OF_WEEK)
           (. Calendar s)))
CompilerException java.lang.NoSuchFieldException: s, compiling:(NO_SOURCE_PATH:3:12)

user> (defmacro is [s instant]
        `(= (.get ~instant Calendar/DAY_OF_WEEK)
           (. Calendar ~s)))
#'user/is

user> (is WEDNESDAY (Calendar/getInstance))
true

I think that when you declare this as a function it will it attempt to validate that Calendar has an 's' member, at this point that is an error, on the other hand when the macro gets replaced the generated form is valid.

just for the fun of it, I got this working:

user> (defmacro is [s]
        `#(= (.get % Calendar/DAY_OF_WEEK)
           (. Calendar ~s)))
#'user/is
user> ((is WEDNESDAY) (Calendar/getInstance))
true

now I can write what I had in mind

user> (def is-friday (is FRIDAY))
#'user/is-friday
user> (is-friday (Calendar/getInstance))
false
user> (def is-wednesday (is WEDNESDAY))
#'user/is-wednesday
user> (is-wednesday (Calendar/getInstance))
true

cool xD



You received this message because you are subscribed to a topic in the Google Groups "Clojure" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/clojure/yXRXJCdsOrM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to clojure+u...@googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.



--
Daniel Meneses Báez

Dave Della Costa

unread,
Aug 14, 2013, 12:14:33 PM8/14/13
to clo...@googlegroups.com
I know you said clj-time solved this for you, but here's another way to
handle it which avoids using a macro (using a map of keywords to
java.util.Calendar weekday enums for convenience and to be more
Clojure-esque, but it isn't necessary):

user=> (def weekdays {:mon Calendar/MONDAY :tues Calendar/TUESDAY :wed
Calendar/WEDNESDAY :thurs Calendar/THURSDAY :fri Calendar/FRIDAY :sat
Calendar/SATURDAY :sun Calendar/SUNDAY})
#'user/weekdays
user=> (defn is-day-of-week? [day-enum] (= Calendar/DAY_OF_WEEK
(day-enum weekdays)))
#'user/is-day-of-week?
user=> (is-day-of-week? :sat)
false
user=> (is-day-of-week? :wed)
true
user=>

You don't need to use all the Java interop syntax you're using, you can
refer to and compare these static fields directly. In the end these are
simply Integer comparisons.

DD
>> https://github.com/clj-time/__clj-time
>> <https://github.com/clj-time/clj-time>
>>
>> (ns time.core
>> (:require [clj-time.core :as time]
>> [clj-time.local :as local]
>> [clj-time.predicates :as p]))
>>
>> (p/monday? (time/now)) ;; false
>> (p/tuesday? (time/now)) ;; false
>> (p/wednesday? (time/now)) ;; true (for me in California since
>> (time/now) is UTC)
>>
>> (p/monday? (local/local-now)) ;; false
>> (p/tuesday? (local/local-now)) ;; true (for me)
>> (p/wednesday? (local/local-now)) ;; false (not yet in California)
>>
>> Sean
>>
>> On Tue, Aug 13, 2013 at 3:14 PM, Daniel Meneses B�ez
>> <dap...@gmail.com> wrote:
>> > Hi :)
>> >
>> > I really want to know if there is a way to do this:
>> >
>> > (ns ...
>> > (:import [java.util Calendar]))
>> >
>> > (defsomething ;; if it is possible using a macro I'm ok with
>> that
>> > calendar-member
>> > [member]
>> > (symbol (str "Calendar/" member)))
>> >
>> > what I want to know if an instance of Calendar "isMonday",
>> "isFriday"
>> > "isSunday" etc...
>> >
>> > so I was thinking to write something like
>> >
>> > (defn- isss [day instant]
>> > (= (.get instant Calendar/DATE) (calendar-member day)))
>> >
>> > and then use it like (def is-friday (partial isss 'FRIDAY)) ;;
>> >
>> > am I being to crazy?
>> >
>> > btw I'm really loving the language.
>> >
>> >
>> >
>> > --
>> > Daniel Meneses B�ez
>> >
>> > --
>> > --
>> > You received this message because you are subscribed to the
>> Google
>> > Groups "Clojure" group.
>> > To post to this group, send email to clo...@googlegroups.com
>> > Note that posts from new members are moderated - please be
>> patient with your
>> > first post.
>> > To unsubscribe from this group, send email to
>> > clojure+u...@__googlegroups.com
>> > For more options, visit this group at
>> > http://groups.google.com/__group/clojure?hl=en
>> <http://groups.google.com/group/clojure?hl=en>
>> > ---
>> > You received this message because you are subscribed to the
>> Google Groups
>> > "Clojure" group.
>> > To unsubscribe from this group and stop receiving emails
>> from it, send an
>> > email to clojure+u...@__googlegroups.com.
>> > For more options, visit
>> https://groups.google.com/__groups/opt_out
>> <https://groups.google.com/groups/opt_out>.
>> >
>> >
>>
>>
>>
>> --
>> Sean A Corfield -- (904) 302-SEAN
>> An Architect's View -- http://corfield.org/
>> World Singles, LLC. -- http://worldsingles.com/
>>
>> "Perfection is the enemy of the good."
>> -- Gustave Flaubert, French realist novelist (1821-1880)
>>
>> --
>> --
>> You received this message because you are subscribed to the Google
>> Groups "Clojure" group.
>> To post to this group, send email to clo...@googlegroups.com
>> <mailto:clo...@googlegroups.com>
>> Note that posts from new members are moderated - please be patient
>> with your first post.
>> To unsubscribe from this group, send email to
>> clojure+u...@googlegroups.com
>> <mailto:clojure+u...@googlegroups.com>
>> For more options, visit this group at
>> http://groups.google.com/group/clojure?hl=en
>> ---
>> You received this message because you are subscribed to the Google
>> Groups "Clojure" group.
>> To unsubscribe from this group and stop receiving emails from it,
>> send an email to clojure+u...@googlegroups.com
>> <mailto:clojure+u...@googlegroups.com>.
>> For more options, visit https://groups.google.com/groups/opt_out.
>>
>>
>
> --
> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clo...@googlegroups.com
> <mailto:clo...@googlegroups.com>
> Note that posts from new members are moderated - please be patient
> with your first post.
> To unsubscribe from this group, send email to
> clojure+u...@googlegroups.com
> <mailto:clojure%2Bunsu...@googlegroups.com>
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en
> ---
> You received this message because you are subscribed to a topic in
> the Google Groups "Clojure" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/clojure/yXRXJCdsOrM/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
> clojure+u...@googlegroups.com
> <mailto:clojure%2Bunsu...@googlegroups.com>.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>
>
>
> --
> Daniel Meneses B�ez

Daniel Meneses Báez

unread,
Aug 14, 2013, 12:43:15 PM8/14/13
to clo...@googlegroups.com
m... the function you wrote only returns true on saturdays

but I get the point!

thanks for your answer


On Wed, Aug 14, 2013 at 12:14 PM, Dave Della Costa <ddell...@gmail.com> wrote:
I know you said clj-time solved this for you, but here's another way to
handle it which avoids using a macro (using a map of keywords to
java.util.Calendar weekday enums for convenience and to be more
Clojure-esque, but it isn't necessary):

user=> (def weekdays {:mon Calendar/MONDAY :tues Calendar/TUESDAY :wed
Calendar/WEDNESDAY :thurs Calendar/THURSDAY :fri Calendar/FRIDAY :sat
Calendar/SATURDAY :sun Calendar/SUNDAY})
#'user/weekdays
user=> (defn is-day-of-week? [day-enum] (= Calendar/DAY_OF_WEEK
(day-enum weekdays)))
#'user/is-day-of-week?
user=> (is-day-of-week? :sat)
false
user=> (is-day-of-week? :wed)
true
user=>

You don't need to use all the Java interop syntax you're using, you can
refer to and compare these static fields directly.  In the end these are
simply Integer comparisons.

DD
>>         On Tue, Aug 13, 2013 at 3:14 PM, Daniel Meneses Báez

>>         <dap...@gmail.com> wrote:
>>         > Hi :)
>>         >
>>         > I really want to know if there is a way to do this:
>>         >
>>         > (ns ...
>>         >    (:import [java.util Calendar]))
>>         >
>>         > (defsomething ;; if it is possible using a macro I'm ok with
>>         that
>>         >    calendar-member
>>         >    [member]
>>         >     (symbol (str "Calendar/" member)))
>>         >
>>         > what I want to know if an instance of Calendar "isMonday",
>>         "isFriday"
>>         > "isSunday" etc...
>>         >
>>         > so I was thinking to write something like
>>         >
>>         > (defn- isss [day instant]
>>         >     (= (.get instant Calendar/DATE) (calendar-member day)))
>>         >
>>         > and then use it like (def is-friday (partial isss 'FRIDAY)) ;;
>>         >
>>         > am I being to crazy?
>>         >
>>         > btw I'm really loving the language.
>>         >
>>         >
>>         >
>>         > --
>>         > Daniel Meneses Báez
> Daniel Meneses Báez
You received this message because you are subscribed to a topic in the Google Groups "Clojure" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/clojure/yXRXJCdsOrM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to clojure+u...@googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.





--
Daniel Meneses Báez

Jim - FooBar();

unread,
Aug 14, 2013, 12:46:03 PM8/14/13
to clo...@googlegroups.com
On 14/08/13 16:45, Daniel Meneses B�ez wrote:
> (defn is [s instant]
> (= (.get instant Calendar/DAY_OF_WEEK)
> (. Calendar s)))

(def ^:private day->int
{:MONDAY 2 :TUESDAY 3 :WEDNESDAY 4 :THURSDAY 5 :FRIDAY 6 :SATURDAY 7
:SUNDAY 1})


(defn is-today?
([s instant]
(= (.get instant Calendar/DAY_OF_WEEK)
((keyword s) day->int)))
([s]
(is s (Calendar/getInstance)))
([]
(let [int-id (.get (Calendar/getInstance) Calendar/DAY_OF_WEEK)]
(->> (some (fn [[k v]] (when (= v int-id) k)) day->int)
str
(drop 1)
(apply str)
symbol))) )

(is-today? 'WEDNESDAY)
=> true

(is-today? MONDAY)
=> false

(is-today?)
=> WEDNESDAY

Jim

Jim - FooBar();

unread,
Aug 14, 2013, 1:17:15 PM8/14/13
to clo...@googlegroups.com
and the non-reflective version which also fixes the typos and the
inefficient transform from keyword -> symbol.

(def ^:private day->int
{:MONDAY 2 :TUESDAY 3 :WEDNESDAY 4 :THURSDAY 5 :FRIDAY 6 :SATURDAY 7
:SUNDAY 1})

(defn is-today?
([s ^java.util.GregorianCalendar instant]
(= (.get instant Calendar/DAY_OF_WEEK)
((keyword s) day->int)))
([s]
(is-today? s (Calendar/getInstance)))
([]
(let [inst (Calendar/getInstance)
int-id (.get inst Calendar/DAY_OF_WEEK)]
(-> (some (fn [[k v]] (when (= v int-id) k)) day->int)
name
symbol))) )

(is-today? (is-today?))
=> true

Jim

Dave Della Costa

unread,
Aug 14, 2013, 1:26:58 PM8/14/13
to clo...@googlegroups.com
Sorry, somehow I got the wrong line pasted in there! Should be:

user=> (defn is-day-of-week? [day-enum] (= (.get (Calendar/getInstance)
Calendar/DAY_OF_WEEK) (day-enum weekdays)))

...but you probably figured that out. ;-)

DD
> >> > Daniel Meneses B�ez
> >> >
> >> > --
> >> > --
> >> > You received this message because you are subscribed to the
> >> Google
> >> > Groups "Clojure" group.
> >> > To post to this group, send email to
> clo...@googlegroups.com <mailto:clo...@googlegroups.com>
> >> > Note that posts from new members are moderated - please be
> >> patient with your
> >> > first post.
> >> > To unsubscribe from this group, send email to
> >> > clojure+u...@__googlegroups.com <http://googlegroups.com>
> >> > For more options, visit this group at
> >> > http://groups.google.com/__group/clojure?hl=en
> >> <http://groups.google.com/group/clojure?hl=en>
> >> > ---
> >> > You received this message because you are subscribed to the
> >> Google Groups
> >> > "Clojure" group.
> >> > To unsubscribe from this group and stop receiving emails
> >> from it, send an
> >> > email to clojure+u...@__googlegroups.com
> <http://googlegroups.com>.
> >> > For more options, visit
> >> https://groups.google.com/__groups/opt_out
> >> <https://groups.google.com/groups/opt_out>.
> >> >
> >> >
> >>
> >>
> >>
> >> --
> >> Sean A Corfield -- (904) 302-SEAN
> >> An Architect's View -- http://corfield.org/
> >> World Singles, LLC. -- http://worldsingles.com/
> >>
> >> "Perfection is the enemy of the good."
> >> -- Gustave Flaubert, French realist novelist (1821-1880)
> >>
> >> --
> >> --
> >> You received this message because you are subscribed to the
> Google
> >> Groups "Clojure" group.
> >> To post to this group, send email to clo...@googlegroups.com
> <mailto:clo...@googlegroups.com>
> >> <mailto:clo...@googlegroups.com
> <mailto:clo...@googlegroups.com>>
> >> Note that posts from new members are moderated - please be
> patient
> >> with your first post.
> >> To unsubscribe from this group, send email to
> >> clojure+u...@googlegroups.com
> <mailto:clojure%2Bunsu...@googlegroups.com>
> >> <mailto:clojure+u...@googlegroups.com
> <mailto:clojure%2Bunsu...@googlegroups.com>>
> >> For more options, visit this group at
> >> http://groups.google.com/group/clojure?hl=en
> >> ---
> >> You received this message because you are subscribed to the
> Google
> >> Groups "Clojure" group.
> >> To unsubscribe from this group and stop receiving emails from it,
> >> send an email to clojure+u...@googlegroups.com
> <mailto:clojure%2Bunsu...@googlegroups.com>
> >> <mailto:clojure+u...@googlegroups.com
> <mailto:clojure%2Bunsu...@googlegroups.com>>.
> >> For more options, visit https://groups.google.com/groups/opt_out.
> >>
> >>
> >
> > --
> > --
> > You received this message because you are subscribed to the Google
> > Groups "Clojure" group.
> > To post to this group, send email to clo...@googlegroups.com
> <mailto:clo...@googlegroups.com>
> > <mailto:clo...@googlegroups.com
> <mailto:clo...@googlegroups.com>>
> > Note that posts from new members are moderated - please be patient
> > with your first post.
> > To unsubscribe from this group, send email to
> > clojure+u...@googlegroups.com
> <mailto:clojure%2Bunsu...@googlegroups.com>
> > <mailto:clojure%2Bunsu...@googlegroups.com
> <mailto:clojure%252Buns...@googlegroups.com>>
> > For more options, visit this group at
> > http://groups.google.com/group/clojure?hl=en
> > ---
> > You received this message because you are subscribed to a topic in
> > the Google Groups "Clojure" group.
> > To unsubscribe from this topic, visit
> > https://groups.google.com/d/topic/clojure/yXRXJCdsOrM/unsubscribe.
> > To unsubscribe from this group and all its topics, send an
> email to
> > clojure+u...@googlegroups.com
> <mailto:clojure%2Bunsu...@googlegroups.com>
> > <mailto:clojure%2Bunsu...@googlegroups.com
> <mailto:clojure%252Buns...@googlegroups.com>>.
> > For more options, visit https://groups.google.com/groups/opt_out.
> >
> >
> >
> >
> > --
> > Daniel Meneses B�ez
> >
> > --
> > --
> > You received this message because you are subscribed to the Google
> > Groups "Clojure" group.
> > To post to this group, send email to clo...@googlegroups.com
> <mailto:clo...@googlegroups.com>
> > Note that posts from new members are moderated - please be patient
> with
> > your first post.
> > To unsubscribe from this group, send email to
> > clojure+u...@googlegroups.com
> <mailto:clojure%2Bunsu...@googlegroups.com>
> > For more options, visit this group at
> > http://groups.google.com/group/clojure?hl=en
> > ---
> > You received this message because you are subscribed to the Google
> > Groups "Clojure" group.
> > To unsubscribe from this group and stop receiving emails from it, send
> > an email to clojure+u...@googlegroups.com
> Daniel Meneses B�ez
Reply all
Reply to author
Forward
0 new messages