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

2011-11-21 Thread gchristnsn
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?

-- 
You received this message because you are subscribed to the Google
Groups Clojure group.
To post to this group, send email to clojure@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+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


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

2011-11-21 Thread gaz jones
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


On Mon, Nov 21, 2011 at 12:46 AM, gchristnsn gchrist...@gmail.com wrote:
 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?

 --
 You received this message because you are subscribed to the Google
 Groups Clojure group.
 To post to this group, send email to clojure@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+unsubscr...@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 post to this group, send email to clojure@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+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en