For what it's worth, Giancarlo makes a good (though vague) point when
recommending maps. You rarely (but not never!) want to def something
programmatically: defs are for things you as the programmer want to
give names to and have access to directly in source code. Often, this
sort of "define a bunch of similar named things" task is better
accomplished with one top-level def, holding a map. For example, you
might have
(defmacro string-maker [string-name the-string]
`(def ~(symbol string-name) ~the-string))
(string-maker "friend" "Peter")
(string-maker "test" "everything")
but simpler in many cases would be
(def strings (into {} (for [[k v] {"friend" "Peter"
"test" "everything"}]
[(keyword k) v])))
Of course, if you're just writing literals anyway, you might as well
make them keywords instead of strings to begin with:
(def strings {:friend "Peter"
:test "everything"})
Then you can look up strings with (:friend strings), which is easier
to understand than a top-level def of "friend" - you don't have to
remember what kind of thing friend is, since it says right here that
it's a string! And the code is obviously simpler as well.
Anyway, all that said, sometimes macros that create defs are good
ideas, so feel free to use them, but most of the time there's a better
approach.
On Jul 14, 7:57 am, Resty Cena <[email protected]> wrote:
> That works! Awesome!
>
> On Thu, Jul 14, 2011 at 5:17 AM, Jonathan Fischer Friberg <
>
>
>
>
>
>
>
> [email protected]> wrote:
> > I see I misunderstood the question, sorry about that.
>
> > What you want is a macro:
>
> > (defmacro string-maker [string-name the-string]
> > `(def ~(symbol string-name) ~the-string))
>
> > Jonathan
>
> > On Thu, Jul 14, 2011 at 1:13 PM, Giancarlo Angulo
> > <[email protected]>wrote:
>
> >> Please look at this:
> >>http://clojure.org/data_structures
> >> see maps.
> >> ================================
> >> Giancarlo Angulo
> >> ================================
> >> -----|-^_^X@^_^,=====|+^_^X~_~@-----
> >> ================================
>
> >> On Thu, Jul 14, 2011 at 2:41 PM, Tuba Lambanog
> >> <[email protected]>wrote:
>
> >>> Hello,
> >>> Total noobie here. I'm trying to create a sort of a string maker
> >>> function. Something like:
>
> >>> (defn string-maker [string-name the-string]
> >>> (def string-name (str the-string)))
>
> >>> so that I can call the function like so:
>
> >>> (string-maker "friend" "Peter")
>
> >>> which I expect to give me the variable: friend
> >>> with the value: Peter
>
> >>> But alas, no luck. Can anybody point me in the right direction?
> >>> Thanks!
> >>> tuba
>
> >>> --
> >>> You received this message because you are subscribed to the Google
> >>> Groups "Clojure" group.
> >>> To post to this group, send email to [email protected]
> >>> Note that posts from new members are moderated - please be patient with
> >>> your first post.
> >>> To unsubscribe from this group, send email to
> >>> [email protected]
> >>> 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 post to this group, send email to [email protected]
> >> Note that posts from new members are moderated - please be patient with
> >> your first post.
> >> To unsubscribe from this group, send email to
> >> [email protected]
> >> 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 post to this group, send email to [email protected]
> > Note that posts from new members are moderated - please be patient with
> > your first post.
> > To unsubscribe from this group, send email to
> > [email protected]
> > 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 post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en