A few questions on how to do things in more idiomatic ways

27 views
Skip to first unread message

gchristnsn

unread,
Nov 21, 2011, 1:46:43 AM11/21/11
to Clojure
1. Often I use the following construct:

(let [some-var (initial-binding)
some-var (some operations (assoc, etc.) using some-var pvevious
binding)
some-var (some operations using some-var pvevious binding
several times)]
(more operations on some-var))

This is probably something like the `do' monad, and I like the thing
that the usage of previous binding of the variable is possible freely
anywhere in the context without wrapping operations with fn and
calling threading operator.
But things become bad if misspelling occurs. Are there some library
macros for such operations, or possibility to do this in less error-
prone way?

2. Let's assume that there is a map with key :entry, and I need to
assoc new value for this key if it's nil or leave map as is otherwise.
I do this in following way:

...
(let [some-map (if (:entry some-map)
some-map
(assoc some-map :entry (get-new-value)))]
...

The question is the same: what is the most correct way to do this?

gaz jones

unread,
Nov 21, 2011, 2:07:41 PM11/21/11
to clo...@googlegroups.com
off top of my head i would probably do something like:

1. (-> some-var
(assoc :foo "bar")
other-operation
and-another)

(see threading macros)

2. (update-in some-map [:entry] #(or % (get-new-value)))

in particular, im not sure if there is a more idiomatic way

> --
> 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

Reply all
Reply to author
Forward
0 new messages