Re: I'm trying to decode some source code to understand how it works

2015-06-23 Thread gianluca torta
Hi Gregg,
 
>
> When you look at the function given as the first argument to 'recurse', (fn 
> [f g]   #(f (apply g %&))), how do you think about when '%&' is replaced 
> by [1 2 3 4]? Does this happen only when 'recurse' has "consumed" all the 
> items in the collection it's been given (as the second argument)?
>
> I would guess that given an expression such as:
((mycomp inc first reverse) [1 2 3 4])

Clojure will first evaluate the innermost list, building a composed 
function with reduce, and only then pass the vector [1 2 3 4] to such 
composed function
 
in any case, the good thing about pure functional programming is that you 
don't have to worry too much about the evaluation order, the result should 
be the same

hth,
-Gianluca

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


I'm trying to decode some source code to understand how it works

2015-06-22 Thread Gregg Williams
Hi--

The code in question is a solution to http://www.4clojure.com/problem/58 
(write a function that does function composition, without using 'comp'). 
The solution I'm trying to understand is by amalloy:

(defn mycomp [& fs]
  (reduce (fn [f g]
#(f (apply g %&)))
  fs))

For example ((mycomp inc first reverse) [1 2 3 4]) would return the value 5.

Here are the notes I've written so far, my attempt to explain how this code 
is evaluated, using ((mycomp inc first reverse) [1 2 3 4]) as the code to 
be evaluated:

- begin

; name the function used by reduce 'f1'
(defn f1 [f g]
  #(f (apply g %&)))

; the collection used by reduce is:
[inc first reverse]

; first iteration of `reduce`; f = inc, g = first
; (f1 inc first) returns #(inc (apply first %&))

; the collection is now:
[first reverse]

; next iteration of reduce; f = #(inc (apply first %&)), g = reverse

 ; (f1 f g) returns:
 #( #(inc (apply first %&)) (apply reverse %&) )
 <-- function > <-- arg, sorta -->
; -- BTW, this is invalid as Clojure code


; now, apply this function to the data:
( #( #(inc (apply first %&)) (apply reverse %&) ) [1 2 3 4])   ; #1
   ( #(inc (apply first %&)) (apply reverse  ([1 2 3 4]) )); #2
  (inc (apply first ((apply reverse  ([1 2 3 4])   ; #3
  (inc (apply first ((  reverse   [1 2 3 4]) )))   ; #4
  (inc (apply first (( 4 3 2 1 ; #5
  (inc (  first (  4 3 2 1)))  ; #6
  (inc 4)  ; #7
  RESULT = 5
- end

(FYI, I know that you can't nest anonymous functions. Consider the above to 
be a thought experiment.)

Is this a good way to think of how the code is evaluated?

I have to admit, I'm not sure I'm thinking about the code correctly. The 
code recurses until it gets to the rightmost function, then it is finally 
able to execute (apply reverse ([1 2 3 4])) (see #3 above) and begin moving 
"up and out of" the nested recursion.

When you look at the function given as the first argument to 'recurse', (fn 
[f g]   #(f (apply g %&))), how do you think about when '%&' is replaced by [1 
2 3 4]? Does this happen only when 'recurse' has "consumed" all the items 
in the collection it's been given (as the second argument)?

I'd appreciate any elaborations, observations, corrections, or alternate 
explanations you would care to add.

Many thanks!


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