I wrote a terrible terrible function once to help with this while bored in
jury duty, called flipartial, in the style of core's 'let's expand/optimize
out the most common arities':

(defn flipartial
  "A combo of partial/flip, makes it easier to fake-curry
non-data-last functions.
In general, only used for small/obvious things, because it's about as hard to
mentally reverse complex positional args in usage as this was to implement, and
reversing large lazy seqs is generally more explicit and rare."
  ([f]
   (fn
     ([] (f))
     ([x] (f x))
     ([x y] (f y x))
     ([x y z] (f z y x))
     ([x y z & args] (apply f (concat (reverse args) [z y x])))))
  ([f arg1]
   (fn
     ([] (f arg1))
     ([x] (f x arg1))
     ([x y] (f y x arg1))
     ([x y z] (f z y x arg1))
     ([x y z & args] (apply f (concat (reverse args) [z y x arg1])))))
  ([f arg1 arg2]
   (fn
     ([] (f arg2 arg1))
     ([x] (f x arg2 arg1))
     ([x y] (f y x arg2 arg1))
     ([x y z] (f z y x arg2 arg1))
     ([x y z & args] (apply f (concat (reverse args) [z y x arg2 arg1])))))
  ([f arg1 arg2 arg3]
   (fn
     ([] (f arg3 arg2 arg1))
     ([x] (f x arg3 arg2 arg1))
     ([x y] (f y x arg3 arg2 arg1))
     ([x y z] (f z y x arg3 arg2 arg1))
     ([x y z & args] (apply f (concat (reverse args) [z y x arg3 arg2 arg1])))))
  ([f arg1 arg2 arg3 & more]
   (fn
     ([] (apply f (concat (reverse more) [arg3 arg2 arg1])))
     ([x] (apply f x (concat (reverse more) [arg3 arg2 arg1])))
     ([x y] (apply f y x (concat (reverse more) [arg3 arg2 arg1])))
     ([x y z] (apply f z y x (concat (reverse more) [arg3 arg2 arg1])))
     ([x y z & args] (apply f (concat (reverse args) [z y x]
                                      (reverse more) [arg3 arg2 arg1]))))))




On Tue, Oct 10, 2017 at 4:17 PM hitesh <hitesh.jas...@gmail.com> wrote:

> It's not terse, but it is easier to follow.
>
> (defn flip
>   [f x y]
>   (f y x))
>
> (defn update-nums
>   [m f]
>   (update m :qs
>           (fn [x]
>             (flip map x
>                   (fn [x]
>                     (update-in x [:nums]
>                                #(map f %)))))))
>
> ;; (update-nums m inc)
> ;;=> {:qs ({:nums (4 2 3)} {:nums (8 5)})}
>
>
>
>
>
> On Tuesday, October 10, 2017 at 12:27:45 PM UTC-4, Rob Nikander wrote:
>>
>> Gah. I changed one name and not another. I meant:
>>
>> (update-in m [:qs * :nums] ...)
>>
>>
>>
>> On Tuesday, October 10, 2017 at 10:18:56 AM UTC-4, Rob Nikander wrote:
>>>
>>> Hi,
>>>
>>> Say I have a map like this:
>>>
>>>     (def m {:qs [{:nums [3 1 2]} {:nums [7 4]}]})
>>>
>>> I want to transform each number with a function (say, `inc`). I imagine
>>> something like this:
>>>
>>>     (update-in m [:qs * :cell-fns] #(map inc %))
>>>     ; or
>>>     (update-in m [:qs * :cell-fns *] inc)
>>>
>>>
>>> But of course you can't write that.  The following code works, but I
>>> don't like reading it:
>>>
>>>     (update m :qs
>>>         (fn [qs]
>>>           (mapv
>>>             (fn [q]
>>>               (update q :nums #(mapv inc %)))
>>>             qs)))
>>>     => {:qs [{:nums [4 2 3]} {:nums [8 5]}]}
>>>
>>> Is there an idiomatic way to do this, that looks more like the shorter
>>> `update-in` idea?
>>>
>>> Rob
>>>
>>>
>>>
>>> --
> 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 unsubscribe from this group and stop receiving emails from it, send an
> email to clojure+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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 unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to