Does this abstraction have any existing name?

2013-07-26 Thread Yoshinori Kohyama


Hello group.

I wrote a tiny macro to apply a function to values in lowest levels in a map 
with arguments.
Three questions:

   - Does this abstraction have any existing name?
   - Is there any existing function/macro to do similar things?
   - Would anyone improve this implementation?


(require '[clojure.test :refer (with-test is are run-tests)])
 
(with-test
  (defmacro mapf [f m & args]
`((fn f# [n#]
(if (map? n#)
(into {} (map (fn [[k# v#]] [k# (f# v#)]) n#))
(~f n# ~@args)))
  ~m))
 
  (is (= (mapf #(* % %) {:a {:b 3 :c 4} :d 5})
 {:a {:b 9 :c 16} :d 25}))
  (is (= (mapf #(+ (* %1 %1) %2) {:a {:b 3 :c 4} :d 5} 1)
 {:a {:b 10 :c 17} :d 26})))

https://gist.github.com/kohyama/6089899

Thank you in advance.

Y.Kohyama

-- 
-- 
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/groups/opt_out.




Re: Does this abstraction have any existing name?

2013-07-26 Thread Gary Trakhman
Why does this need to be a macro?  It doesn't actually manipulate code.

Just turn this:  (~f n# ~@args) into (apply f n args) in the function
implementation.


On Fri, Jul 26, 2013 at 11:49 AM, Yoshinori Kohyama wrote:

> Hello group.
>
> I wrote a tiny macro to apply a function to values in lowest levels in a map 
> with arguments.
> Three questions:
>
>    - Does this abstraction have any existing name?
>- Is there any existing function/macro to do similar things?
>- Would anyone improve this implementation?
>
>
> (require '[clojure.test :refer (with-test is are run-tests)])
>
> (with-test
>   (defmacro mapf [f m & args]
> `((fn f# [n#]
> (if (map? n#)
> (into {} (map (fn [[k# v#]] [k# (f# v#)]) n#))
> (~f n# ~@args)))
>   ~m))
>
>   (is (= (mapf #(* % %) {:a {:b 3 :c 4} :d 5})
>  {:a {:b 9 :c 16} :d 25}))
>   (is (= (mapf #(+ (* %1 %1) %2) {:a {:b 3 :c 4} :d 5} 1)
>  {:a {:b 10 :c 17} :d 26})))
>
> https://gist.github.com/kohyama/6089899
>
> Thank you in advance.
>
> Y.Kohyama
>
>  --
> --
> 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/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 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/groups/opt_out.




Re: Does this abstraction have any existing name?

2013-07-26 Thread Yoshinori Kohyama


Thank you Gary.
There's no reason why this need to be a macro.
It has rewritten as a function.

And I'd still like to hear any response about the same three questions.

(require '[clojure.test :refer (with-test is run-tests)])
 
(with-test
  (defn mapf [f m & args]
((fn g [n]
   (if (map? n)
   (into {} (map (fn [[k v]] [k (g v)]) n))
   (apply f n args)))
 m))
 
  (is (= (mapf #(* % %) {:a {:b 3 :c 4} :d 5})
 {:a {:b 9 :c 16} :d 25}))
  (is (= (mapf #(+ (* %1 %1) %2) {:a {:b 3 :c 4} :d 5} 1)
 {:a {:b 10 :c 17} :d 26})))

-- 
-- 
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/groups/opt_out.




Re: Does this abstraction have any existing name?

2013-07-26 Thread Jay Fields
I defined update-vals in jry:
https://github.com/jaycfields/jry/blob/master/src/clojure/jry.clj#L74-L75

It doesn't traverse nested maps, but I haven't ever needed that ability
either.

1) I've never seen a name for that.
2) not in core. I'm sure it's been written 50 times in various helper libs.
3) I'd probably write it like below, but I'm not convinced it's any better.

(defn deep-update-vals
  [m f & args]
  (if (map? m)
(reduce-kv #(assoc %1 %2 (apply deep-update-vals %3 f args)) {} m)
(apply f m args)))


On Fri, Jul 26, 2013 at 1:31 PM, Yoshinori Kohyama wrote:

> Thank you Gary.
> There's no reason why this need to be a macro.
> It has rewritten as a function.
>
> And I'd still like to hear any response about the same three questions.
>
> (require '[clojure.test :refer (with-test is run-tests)])
>
> (with-test
>   (defn mapf [f m & args]
> ((fn g [n]
>(if (map? n)
>(into {} (map (fn [[k v]] [k (g v)]) n))
>(apply f n args)))
>
>  m))
>
>   (is (= (mapf #(* % %) {:a {:b 3 :c 4} :d 5})
>  {:a {:b 9 :c 16} :d 25}))
>   (is (= (mapf #(+ (* %1 %1) %2) {:a {:b 3 :c 4} :d 5} 1)
>  {:a {:b 10 :c 17} :d 26})))
>
>  --
> --
> 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/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 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/groups/opt_out.




Re: Does this abstraction have any existing name?

2013-07-26 Thread Jay Fields
I'm not sure I'd call this more readable, but it's another option - using
clojure.walk

(defn deep-update-vals
  [m f & args]
  (let [f (fn [[k v]] (if (map? v) [k v] [k (apply f v args)]))]
(clojure.walk/postwalk (fn [x] (if (map? x) (into {} (map f x)) x)) m)))


On Fri, Jul 26, 2013 at 2:06 PM, Jay Fields  wrote:

> I defined update-vals in jry:
> https://github.com/jaycfields/jry/blob/master/src/clojure/jry.clj#L74-L75
>
> It doesn't traverse nested maps, but I haven't ever needed that ability
> either.
>
> 1) I've never seen a name for that.
> 2) not in core. I'm sure it's been written 50 times in various helper libs.
> 3) I'd probably write it like below, but I'm not convinced it's any better.
>
> (defn deep-update-vals
>   [m f & args]
>   (if (map? m)
> (reduce-kv #(assoc %1 %2 (apply deep-update-vals %3 f args)) {} m)
> (apply f m args)))
>
>
> On Fri, Jul 26, 2013 at 1:31 PM, Yoshinori Kohyama wrote:
>
>> Thank you Gary.
>> There's no reason why this need to be a macro.
>> It has rewritten as a function.
>>
>> And I'd still like to hear any response about the same three questions.
>>
>>
>> (require '[clojure.test :refer (with-test is run-tests)])
>>
>>
>> (with-test
>>   (defn mapf [f m & args]
>>
>> ((fn g [n]
>>(if (map? n)
>>
>>(into {} (map (fn [[k v]] [k (g v)]) n))
>>
>>(apply f n args)))
>>
>>  m))
>>
>>   (is (= (mapf #(* % %) {:a {:b 3 :c 4} :d 5})
>>
>>  {:a {:b 9 :c 16} :d 25}))
>>
>>   (is (= (mapf #(+ (* %1 %1) %2) {:a {:b 3 :c 4} :d 5} 1)
>>
>>  {:a {:b 10 :c 17} :d 26})))
>>
>>
>>  --
>> --
>> 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/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 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/groups/opt_out.




Re: Does this abstraction have any existing name?

2013-07-26 Thread Takahiro Hozumi
I think clojure.walk is suited to this purpose.
https://github.com/clojure/clojure/blob/master/src/clj/clojure/walk.clj
See keywordize-keys as an example.

On Saturday, July 27, 2013 2:31:21 AM UTC+9, Yoshinori Kohyama wrote:
>
> Thank you Gary.
> There's no reason why this need to be a macro.
> It has rewritten as a function.
>
> And I'd still like to hear any response about the same three questions.
>
> (require '[clojure.test :refer (with-test is run-tests)])
>  
> (with-test
>   (defn mapf [f m & args]
> ((fn g [n]
>(if (map? n)
>(into {} (map (fn [[k v]] [k (g v)]) n))
>(apply f n args)))
>  m))
>  
>   (is (= (mapf #(* % %) {:a {:b 3 :c 4} :d 5})
>  {:a {:b 9 :c 16} :d 25}))
>   (is (= (mapf #(+ (* %1 %1) %2) {:a {:b 3 :c 4} :d 5} 1)
>  {:a {:b 10 :c 17} :d 26})))
>
>

-- 
-- 
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/groups/opt_out.




Re: Does this abstraction have any existing name?

2013-07-26 Thread Dave Sann
if you are thinking of the nested map as a sort of tree - which you seem to 
be doing - then map-leaves or something similar might convey the intent.

On Saturday, 27 July 2013 04:30:30 UTC+10, Jay Fields wrote:
>
> I'm not sure I'd call this more readable, but it's another option - using 
> clojure.walk
>
> (defn deep-update-vals
>   [m f & args]
>   (let [f (fn [[k v]] (if (map? v) [k v] [k (apply f v args)]))]
> (clojure.walk/postwalk (fn [x] (if (map? x) (into {} (map f x)) x)) 
> m)))
>
>
> On Fri, Jul 26, 2013 at 2:06 PM, Jay Fields 
> > wrote:
>
>> I defined update-vals in jry: 
>> https://github.com/jaycfields/jry/blob/master/src/clojure/jry.clj#L74-L75
>>
>> It doesn't traverse nested maps, but I haven't ever needed that ability 
>> either.
>>
>> 1) I've never seen a name for that.
>> 2) not in core. I'm sure it's been written 50 times in various helper 
>> libs.
>> 3) I'd probably write it like below, but I'm not convinced it's any 
>> better.
>>
>> (defn deep-update-vals
>>   [m f & args]
>>   (if (map? m)
>> (reduce-kv #(assoc %1 %2 (apply deep-update-vals %3 f args)) {} m)
>> (apply f m args)))
>>
>>
>> On Fri, Jul 26, 2013 at 1:31 PM, Yoshinori Kohyama 
>> 
>> > wrote:
>>
>>> Thank you Gary.
>>> There's no reason why this need to be a macro.
>>> It has rewritten as a function.
>>>
>>> And I'd still like to hear any response about the same three questions.
>>>
>>>
>>>
>>> (require '[clojure.test :refer (with-test is run-tests)])
>>>
>>>
>>>  
>>> (with-test
>>>   (defn mapf [f m & args]
>>>
>>>
>>> ((fn g [n]
>>>(if (map? n)
>>>
>>>
>>>(into {} (map (fn [[k v]] [k (g v)]) n))
>>>
>>>
>>>(apply f n args)))
>>>
>>>  m))
>>>  
>>>   (is (= (mapf #(* % %) {:a {:b 3 :c 4} :d 5})
>>>
>>>
>>>  {:a {:b 9 :c 16} :d 25}))
>>>
>>>
>>>   (is (= (mapf #(+ (* %1 %1) %2) {:a {:b 3 :c 4} :d 5} 1)
>>>
>>>
>>>  {:a {:b 10 :c 17} :d 26})))
>>>
>>>
>>>
>>>  -- 
>>> -- 
>>> 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.
>>>  
>>>  
>>>
>>
>>
>

-- 
-- 
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/groups/opt_out.




Re: Does this abstraction have any existing name?

2013-07-26 Thread Yoshinori Kohyama
Thanks, Jay.
Your reply helps me much.

I often think which is better,
  (into {} (map (fn [[k v]] [k (f v)]) m))
and
  (reduce (fn [a [k v]] (assoc a k (f v))) m m)
or
  (reduce-kv (fn [a k v] (assoc a k (f v))) m m)
, where f is a function, m is a map.

Any body have any opinion?

Thanks.
Y. Kohyama

-- 
-- 
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/groups/opt_out.




Re: Does this abstraction have any existing name?

2013-07-26 Thread Ben Wolfson
If the map is a tree with values at the leaves, then it's a functor and
applying a function to the values is fmap.


On Fri, Jul 26, 2013 at 6:02 PM, Dave Sann  wrote:

> if you are thinking of the nested map as a sort of tree - which you seem
> to be doing - then map-leaves or something similar might convey the intent.
>
> On Saturday, 27 July 2013 04:30:30 UTC+10, Jay Fields wrote:
>>
>> I'm not sure I'd call this more readable, but it's another option - using
>> clojure.walk
>>
>> (defn deep-update-vals
>>   [m f & args]
>>   (let [f (fn [[k v]] (if (map? v) [k v] [k (apply f v args)]))]
>> (clojure.walk/postwalk (fn [x] (if (map? x) (into {} (map f x)) x))
>> m)))
>>
>>
>> On Fri, Jul 26, 2013 at 2:06 PM, Jay Fields  wrote:
>>
>>> I defined update-vals in jry: https://github.com/**
>>> jaycfields/jry/blob/master/**src/clojure/jry.clj#L74-L75
>>>
>>> It doesn't traverse nested maps, but I haven't ever needed that ability
>>> either.
>>>
>>> 1) I've never seen a name for that.
>>> 2) not in core. I'm sure it's been written 50 times in various helper
>>> libs.
>>> 3) I'd probably write it like below, but I'm not convinced it's any
>>> better.
>>>
>>> (defn deep-update-vals
>>>   [m f & args]
>>>   (if (map? m)
>>> (reduce-kv #(assoc %1 %2 (apply deep-update-vals %3 f args)) {} m)
>>> (apply f m args)))
>>>
>>>
>>> On Fri, Jul 26, 2013 at 1:31 PM, Yoshinori Kohyama wrote:
>>>
 Thank you Gary.
 There's no reason why this need to be a macro.
 It has rewritten as a function.

 And I'd still like to hear any response about the same three questions.



 (require '[clojure.test :refer (with-test is run-tests)])



 (with-test
   (defn mapf [f m & args]


 ((fn g [n]
(if (map? n)


(into {} (map (fn [[k v]] [k (g v)]) n))


(apply f n args)))

  m))

   (is (= (mapf #(* % %) {:a {:b 3 :c 4} :d 5})


  {:a {:b 9 :c 16} :d 25}))


   (is (= (mapf #(+ (* %1 %1) %2) {:a {:b 3 :c 4} :d 5} 1)


  {:a {:b 10 :c 17} :d 26})))



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



>>>
>>>
>>  --
> --
> 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/groups/opt_out.
>
>
>



-- 
Ben Wolfson
"Human kind has used its intelligence to vary the flavour of drinks, which
may be sweet, aromatic, fermented or spirit-based. ... Family and social
life also offer numerous other occasions to consume drinks for pleasure."
[Larousse, "Drink" entry]

-- 
-- 
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/groups/opt_out.




Re: Does this abstraction have any existing name?

2013-07-26 Thread Yoshinori Kohyama
Thank you, Dave.

Yes, I'm thinking of a nested map as a sort of tree.
Then, what I do may be called 'map-leaves', as you say.

Is there an existing function named like that?
Or did you say that just as 'an abstraction name'?

Y.Kohyama

2013年7月27日土曜日 10時02分17秒 UTC+9 Dave Sann:
>
> if you are thinking of the nested map as a sort of tree - which you seem 
> to be doing - then map-leaves or something similar might convey the intent.
>

-- 
-- 
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/groups/opt_out.




Re: Does this abstraction have any existing name?

2013-07-26 Thread Yoshinori Kohyama
Thank you, Ben.

If I think the map as a tree, then as a functor, what I do is 'fmap' (in 
Haskell or some languages),
,as you say.
Thanks for saying that.

Does Algo supply things around Functor, Applicative and Monad?
I'm going to look at Algo.

Or anybody knows any other libraries or implementations around these things 
in Clojure?

Regards,
Y.Kohyama


2013年7月27日土曜日 13時06分23秒 UTC+9 Ben:
>
> If the map is a tree with values at the leaves, then it's a functor and 
> applying a function to the values is fmap.
>
>
> On Fri, Jul 26, 2013 at 6:02 PM, Dave Sann 
> > wrote:
>
>> if you are thinking of the nested map as a sort of tree - which you seem 
>> to be doing - then map-leaves or something similar might convey the intent.
>>
>> On Saturday, 27 July 2013 04:30:30 UTC+10, Jay Fields wrote:
>>>
>>> I'm not sure I'd call this more readable, but it's another option - 
>>> using clojure.walk
>>>
>>> (defn deep-update-vals
>>>   [m f & args]
>>>   (let [f (fn [[k v]] (if (map? v) [k v] [k (apply f v args)]))]
>>> (clojure.walk/postwalk (fn [x] (if (map? x) (into {} (map f x)) x)) 
>>> m)))
>>>
>>>
>>> On Fri, Jul 26, 2013 at 2:06 PM, Jay Fields  wrote:
>>>
 I defined update-vals in jry: https://github.com/**
 jaycfields/jry/blob/master/**src/clojure/jry.clj#L74-L75

 It doesn't traverse nested maps, but I haven't ever needed that ability 
 either.

 1) I've never seen a name for that.
 2) not in core. I'm sure it's been written 50 times in various helper 
 libs.
 3) I'd probably write it like below, but I'm not convinced it's any 
 better.

 (defn deep-update-vals
   [m f & args]
   (if (map? m)
 (reduce-kv #(assoc %1 %2 (apply deep-update-vals %3 f args)) {} m)
 (apply f m args)))


 On Fri, Jul 26, 2013 at 1:31 PM, Yoshinori Kohyama 
 wrote:

> Thank you Gary.
> There's no reason why this need to be a macro.
> It has rewritten as a function.
>
> And I'd still like to hear any response about the same three questions.
>
>
>
>
> (require '[clojure.test :refer (with-test is run-tests)])
>
>
>
>  
> (with-test
>   (defn mapf [f m & args]
>
>
>
> ((fn g [n]
>(if (map? n)
>
>
>
>(into {} (map (fn [[k v]] [k (g v)]) n))
>
>
>
>(apply f n args)))
>
>  m))
>  
>   (is (= (mapf #(* % %) {:a {:b 3 :c 4} :d 5})
>
>
>
>  {:a {:b 9 :c 16} :d 25}))
>
>
>
>   (is (= (mapf #(+ (* %1 %1) %2) {:a {:b 3 :c 4} :d 5} 1)
>
>
>
>  {:a {:b 10 :c 17} :d 26})))
>
>
>
>
>  -- 
> -- 
> 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
> .
>  
>  
>


>>>  -- 
>> -- 
>> 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.
>>  
>>  
>>
>
>
>
> -- 
> Ben Wolfson
> "Human kind has used its intelligence to vary the flavour of drinks, which 
> may be sweet, aromatic, fermented or spirit-based. ... Family and social 
> life also offer numerous other occasions to consume drinks for pleasure." 
> [Larousse, "Drink" entry]
>
> 

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

Re: Does this abstraction have any existing name?

2013-07-26 Thread Jason Wolfe
This is 'map-leaves' in our open-source lib plumbing.map (besides the 
option for additional args):

https://github.com/Prismatic/plumbing/blob/master/src/plumbing/map.clj

Cheers, Jason

On Friday, July 26, 2013 8:49:14 AM UTC-7, Yoshinori Kohyama wrote:
>
> Hello group.
>
> I wrote a tiny macro to apply a function to values in lowest levels in a map 
> with arguments.
> Three questions:
>
>    - Does this abstraction have any existing name?
>- Is there any existing function/macro to do similar things?
>- Would anyone improve this implementation?
>
>
> (require '[clojure.test :refer (with-test is are run-tests)])
>  
> (with-test
>   (defmacro mapf [f m & args]
> `((fn f# [n#]
> (if (map? n#)
> (into {} (map (fn [[k# v#]] [k# (f# v#)]) n#))
> (~f n# ~@args)))
>   ~m))
>  
>   (is (= (mapf #(* % %) {:a {:b 3 :c 4} :d 5})
>  {:a {:b 9 :c 16} :d 25}))
>   (is (= (mapf #(+ (* %1 %1) %2) {:a {:b 3 :c 4} :d 5} 1)
>  {:a {:b 10 :c 17} :d 26})))
>
> https://gist.github.com/kohyama/6089899
>
> Thank you in advance.
>
> Y.Kohyama
>
>

-- 
-- 
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/groups/opt_out.




Re: Does this abstraction have any existing name?

2013-07-26 Thread Ben Wolfson
On Fri, Jul 26, 2013 at 9:27 PM, Yoshinori Kohyama wrote:

> Thank you, Ben.
>
> If I think the map as a tree, then as a functor, what I do is 'fmap' (in
> Haskell or some languages),
> ,as you say.
> Thanks for saying that.
>
> Does Algo supply things around Functor, Applicative and Monad?
> I'm going to look at Algo.
>
> Or anybody knows any other libraries or implementations around these
> things in Clojure?
>
>
algo.generic has a functor implementation for regular maps which you could
already use, if your maps have uniform depth:

(fmap (partial fmap inc) {:a {:x 1} :b {:y 2}}) ---> {:a {:x 2} :b {:y 3}}

if you want to use that fmap (or, I'd think, the fmaps provided by either
morph or fluokitten) with uneven depths, you'd have to wrap them in a
defrecord or deftype, I'd expect.

-- 
Ben Wolfson
"Human kind has used its intelligence to vary the flavour of drinks, which
may be sweet, aromatic, fermented or spirit-based. ... Family and social
life also offer numerous other occasions to consume drinks for pleasure."
[Larousse, "Drink" entry]

-- 
-- 
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/groups/opt_out.




Re: Does this abstraction have any existing name?

2013-07-26 Thread Ben Wolfson
On Fri, Jul 26, 2013 at 9:31 PM, Ben Wolfson  wrote:

> On Fri, Jul 26, 2013 at 9:27 PM, Yoshinori Kohyama wrote:
>
>> Thank you, Ben.
>>
>> If I think the map as a tree, then as a functor, what I do is 'fmap' (in
>> Haskell or some languages),
>> ,as you say.
>> Thanks for saying that.
>>
>> Does Algo supply things around Functor, Applicative and Monad?
>> I'm going to look at Algo.
>>
>> Or anybody knows any other libraries or implementations around these
>> things in Clojure?
>>
>>
> algo.generic has a functor implementation for regular maps which you could
> already use, if your maps have uniform depth:
>
> (fmap (partial fmap inc) {:a {:x 1} :b {:y 2}}) ---> {:a {:x 2} :b {:y 3}}
>
> if you want to use that fmap (or, I'd think, the fmaps provided by either
> morph or fluokitten) with uneven depths, you'd have to wrap them in a
> defrecord or deftype, I'd expect.
>
>
You could also engage in this underhanded trick:

user=> (defmethod f/fmap Object [f v] (f v)) ;;; "Identity" functor
#
user=> (defmethod f/fmap clojure.lang.IPersistentMap [f v] (into {} (map
(fn [[k v]] [k (f/fmap f v)]) v)))
#
user=> (f/fmap inc {:a {:y 1} :b {:x 2 :z {:c [1 2 3]}}})
{:a {:y 2}, :b {:z {:c [2 3 4]}, :x 3}}

But I wouldn't recommend it.

-- 
-- 
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/groups/opt_out.




Re: Does this abstraction have any existing name?

2013-07-26 Thread Yoshinori Kohyama
Thank you, Jason.

The 'map-leaves' does the same as my 'mapf' and simpler.
Its name and implementation is very sophisticated with other functions doing
similar but different things.
Thanks the information.

Regards,
Y.Kohyama

2013年7月27日土曜日 13時31分28秒 UTC+9 Jason Wolfe:
>
> This is 'map-leaves' in our open-source lib plumbing.map (besides the 
> option for additional args):
>
> https://github.com/Prismatic/plumbing/blob/master/src/plumbing/map.clj
>
> Cheers, Jason
>

-- 
-- 
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/groups/opt_out.




Re: Does this abstraction have any existing name?

2013-07-26 Thread Yoshinori Kohyama
Thanks Ben, for very useful informations!

2013年7月27日土曜日 13時31分41秒 UTC+9 Ben:
>
> if you want to use that fmap (or, I'd think, the fmaps provided by either 
> morph or fluokitten) with uneven depths, you'd have to wrap them in a 
> defrecord or deftype, I'd expect.
>

I see.

2013年7月27日土曜日 13時37分28秒 UTC+9 Ben:
>
> user=> (defmethod f/fmap Object [f v] (f v)) ;;; "Identity" functor
> #
> user=> (defmethod f/fmap clojure.lang.IPersistentMap [f v] (into {} (map 
> (fn [[k v]] [k (f/fmap f v)]) v)))
> #
> user=> (f/fmap inc {:a {:y 1} :b {:x 2 :z {:c [1 2 3]}}})
> {:a {:y 2}, :b {:z {:c [2 3 4]}, :x 3}}
>
> But I wouldn't recommend it.
>

One way.  It can be used, I think.

Thanks again.

Regards,
Y.Kohyama

-- 
-- 
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/groups/opt_out.




Re: Does this abstraction have any existing name?

2013-07-27 Thread Armando Blancas
morph's incomplete because it extends Functor only for collections and 
function composition; I'll add String and boxed types, but for now Long's 
extension must be supplied:
(use 'blancas.morph.core)
(extend-type java.lang.Long Functor (fun [this f] (f this)))

With this, it's like alto.generic but fmap comes curried:
user=> (fmap inc {:a 3 :b 9 :c 16})
{:a 4, :c 17, :b 10}
user=> (fmap (fmap inc) {:a {:b 9 :c 16} :d 25})
{:a {:c 17, :b 10}, :d 26}

For use with arbitrarily nested maps something like this is needed:
(defn f [k] (fn g [x] (if (map? x) (fmap g x) (k x

user=> (fmap (f inc) {:a {:b 9 :c {:x 16 :y 20}} :d 25})
{:a {:c {:y 21, :x 17}, :b 10}, :d 26}
 
On Friday, July 26, 2013 9:31:41 PM UTC-7, Ben wrote:
>
> On Fri, Jul 26, 2013 at 9:27 PM, Yoshinori Kohyama 
> 
> > wrote:
>
>> Thank you, Ben.
>>
>> If I think the map as a tree, then as a functor, what I do is 'fmap' (in 
>> Haskell or some languages),
>> ,as you say.
>> Thanks for saying that.
>>
>> Does Algo supply things around Functor, Applicative and Monad?
>> I'm going to look at Algo.
>>
>> Or anybody knows any other libraries or implementations around these 
>> things in Clojure?
>>
>>
> algo.generic has a functor implementation for regular maps which you could 
> already use, if your maps have uniform depth:
>
> (fmap (partial fmap inc) {:a {:x 1} :b {:y 2}}) ---> {:a {:x 2} :b {:y 3}}
>
> if you want to use that fmap (or, I'd think, the fmaps provided by either 
> morph or fluokitten) with uneven depths, you'd have to wrap them in a 
> defrecord or deftype, I'd expect.
>
> -- 
> Ben Wolfson
> "Human kind has used its intelligence to vary the flavour of drinks, which 
> may be sweet, aromatic, fermented or spirit-based. ... Family and social 
> life also offer numerous other occasions to consume drinks for pleasure." 
> [Larousse, "Drink" entry]
>
>  

-- 
-- 
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/groups/opt_out.