Re: problem 58 on 4clojure
Hello Ritchie! I had almost the same: (fn [ fs] (let [f (last fs) r (rest (reverse fs))] (fn [ data] (reduce #(%2 %) (apply f data) r But then I really liked your destructuring, so I'll take it with me: (fn [ fs] (let [[f r] (reverse fs)] (fn [ data] (reduce #(%2 %) (apply f data) r Thanks a lot! Le dimanche 26 août 2012 14:06:20 UTC-4, Sam Ritchie a écrit : Here's a solution using reduce that handles passing multiple arguments into the rightmost function: (fn [ fns] (fn [ args] (let [[f fns] (reverse fns)] (reduce #(%2 %1) (apply f args) fns On Sun, Aug 26, 2012 at 9:12 AM, Tyler Perkins thinks@gmail.comjavascript: wrote: It might help to simplify. Whenever you're accumulating over a sequence of things, think of reduce: (let [__ (fn [ fs] ;; Here's the function: (reduce #(fn [x] (%1 (%2 x))) fs)) ] ;; Testing: [ (= 5 ((__ (partial + 3) second) [1 2 3 4])) (= [3 2 1] ((__ rest reverse) [1 2 3 4])) ]) Each step in the accumulation creates a new function that just applies the current function to an argument that is the result of applying the already-composed ones. -- You received this message because you are subscribed to the Google Groups Clojure group. To post to this group, send email to clo...@googlegroups.comjavascript: 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 javascript: For more options, visit this group at http://groups.google.com/group/clojure?hl=en -- Sam Ritchie, Twitter Inc 703.662.1337 @sritchie (Too brief? Here's why! http://emailcharter.org) -- -- 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: problem 58 on 4clojure
It might help to simplify. Whenever you're accumulating over a sequence of things, think of reduce: (let [__ (fn [ fs] ;; Here's the function: (reduce #(fn [x] (%1 (%2 x))) fs)) ] ;; Testing: [ (= 5 ((__ (partial + 3) second) [1 2 3 4])) (= [3 2 1] ((__ rest reverse) [1 2 3 4])) ]) Each step in the accumulation creates a new function that just applies the current function to an argument that is the result of applying the already-composed ones. -- 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: problem 58 on 4clojure
Here's a solution using reduce that handles passing multiple arguments into the rightmost function: (fn [ fns] (fn [ args] (let [[f fns] (reverse fns)] (reduce #(%2 %1) (apply f args) fns On Sun, Aug 26, 2012 at 9:12 AM, Tyler Perkins thinks.outs...@gmail.comwrote: It might help to simplify. Whenever you're accumulating over a sequence of things, think of reduce: (let [__ (fn [ fs] ;; Here's the function: (reduce #(fn [x] (%1 (%2 x))) fs)) ] ;; Testing: [ (= 5 ((__ (partial + 3) second) [1 2 3 4])) (= [3 2 1] ((__ rest reverse) [1 2 3 4])) ]) Each step in the accumulation creates a new function that just applies the current function to an argument that is the result of applying the already-composed ones. -- 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 -- Sam Ritchie, Twitter Inc 703.662.1337 @sritchie (Too brief? Here's why! http://emailcharter.org) -- 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
problem 58 on 4clojure
This problem is really confusing me. I found a solution online, but I can't understand the solution. Can anyone explain to me why this works? The problem is stated as: Write a function which allows you to create function compositions. The parameter list should take a variable number of functions, and create a function applies them from right-to-left. (= [3 2 1] ((__ rest reverse) [1 2 3 4])) (= 5 ((__ (partial + 3) second) [1 2 3 4])) The examples would accept the solution replacing the . The solution I found is: (fn [x xs] (fn [ args] ((fn step [[f fs] a] (if fs (f (step fs a)) (apply f a))) (cons x xs) args))) This works, and baffles me when I try to understand 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
Re: problem 58 on 4clojure
check out clojure.core/comp, and it's source 2012/8/25 John Holland jbholl...@gmail.com This problem is really confusing me. I found a solution online, but I can't understand the solution. Can anyone explain to me why this works? The problem is stated as: Write a function which allows you to create function compositions. The parameter list should take a variable number of functions, and create a function applies them from right-to-left. (= [3 2 1] ((__ rest reverse) [1 2 3 4])) (= 5 ((__ (partial + 3) second) [1 2 3 4])) The examples would accept the solution replacing the . The solution I found is: (fn [x xs] (fn [ args] ((fn step [[f fs] a] (if fs (f (step fs a)) (apply f a))) (cons x xs) args))) This works, and baffles me when I try to understand 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 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: problem 58 on 4clojure
its* 2012/8/25 Bronsa brobro...@gmail.com check out clojure.core/comp, and it's source 2012/8/25 John Holland jbholl...@gmail.com This problem is really confusing me. I found a solution online, but I can't understand the solution. Can anyone explain to me why this works? The problem is stated as: Write a function which allows you to create function compositions. The parameter list should take a variable number of functions, and create a function applies them from right-to-left. (= [3 2 1] ((__ rest reverse) [1 2 3 4])) (= 5 ((__ (partial + 3) second) [1 2 3 4])) The examples would accept the solution replacing the . The solution I found is: (fn [x xs] (fn [ args] ((fn step [[f fs] a] (if fs (f (step fs a)) (apply f a))) (cons x xs) args))) This works, and baffles me when I try to understand 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 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: problem 58 on 4clojure
How can I find the problem # 58? This is something I was looking right now. What's the best order to follow? I know I can sort by complexity but I think there should be a better way to sort them. Thanks On Sat, Aug 25, 2012 at 1:05 PM, Bronsa brobro...@gmail.com wrote: its* 2012/8/25 Bronsa brobro...@gmail.com check out clojure.core/comp, and it's source 2012/8/25 John Holland jbholl...@gmail.com This problem is really confusing me. I found a solution online, but I can't understand the solution. Can anyone explain to me why this works? The problem is stated as: Write a function which allows you to create function compositions. The parameter list should take a variable number of functions, and create a function applies them from right-to-left. (= [3 2 1] ((__ rest reverse) [1 2 3 4])) (= 5 ((__ (partial + 3) second) [1 2 3 4])) The examples would accept the solution replacing the . The solution I found is: (fn [x xs] (fn [ args] ((fn step [[f fs] a] (if fs (f (step fs a)) (apply f a))) (cons x xs) args))) This works, and baffles me when I try to understand 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 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
Re: problem 58 on 4clojure
On Sat, Aug 25, 2012 at 10:54 PM, Erlis Vidal er...@erlisvidal.com wrote: How can I find the problem # 58? http://www.4clojure.com/problem/58 Just modify the parameter to the problem number you want to see. This is something I was looking right now. What's the best order to follow? Start with the first problem and continue in that order. I find that the best way to do as lot of problems are build up on previous set of problems. Cheers. -- 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: problem 58 on 4clojure
Thanks for the link. I didnt notice the problem number in the URL. I've sorted the problems and lost the default order. Thanks again Erlis On Sat, Aug 25, 2012 at 1:29 PM, Mayank Jain firesof...@gmail.com wrote: On Sat, Aug 25, 2012 at 10:54 PM, Erlis Vidal er...@erlisvidal.comwrote: How can I find the problem # 58? http://www.4clojure.com/problem/58 Just modify the parameter to the problem number you want to see. This is something I was looking right now. What's the best order to follow? Start with the first problem and continue in that order. I find that the best way to do as lot of problems are build up on previous set of problems. Cheers. -- 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
Re: problem 58 on 4clojure
On Sat, Aug 25, 2012 at 6:47 PM, John Holland jbholl...@gmail.com wrote: This problem is really confusing me. I found a solution online, but I can't understand the solution. Can anyone explain to me why this works? The problem is stated as: Write a function which allows you to create function compositions. The parameter list should take a variable number of functions, and create a function applies them from right-to-left. (= [3 2 1] ((__ rest reverse) [1 2 3 4])) (= 5 ((__ (partial + 3) second) [1 2 3 4])) The examples would accept the solution replacing the . The solution I found is: (fn [x xs] (fn [ args] ((fn step [[f fs] a] (if fs (f (step fs a)) (apply f a))) (cons x xs) args))) This works, and baffles me when I try to understand it. It's obfuscated by the fact that in 4clojure, the smaller is the code, the higher is your score. Divide Conquer works fine here to understand what's going on: https://gist.github.com/3468564 : ;; you are starting here (fn [x xs] (fn [ args] ((fn step [[f fs] a] (if fs (f (step fs a)) (apply f a))) (cons x xs) args))) ;; name the fn (defn mycomp [x xs] (fn [ args] ((fn step [[f fs] a] (if fs (f (step fs a)) (apply f a))) (cons x xs) args))) ;; extract the step function (defn step Equivalent to (apply (comp f fs) args) [[f fs] args] (if fs (f (step fs args)) (apply f args))) (defn mycomp Equivalent to (partial step fns) [ fns] (fn [ args] (step fns args))) ;; rename a few things for clarity (defn comp-then-apply Equivalent to (apply (comp f fs) args) [[f fs] args] (if fs (f (comp-then-apply fs args)) (apply f args))) (defn mycomp Equivalent to (partial comp-then-apply fns) [ fns] (fn [ args] (comp-then-apply fns args))) And use the REPL luke! HTH Denis -- 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
Re: problem 58 on 4clojure
Here's my take: We want to define a function my-comp. It takes n functions and return their composition. We want to return a function of any number of arguments, so let's start by working with a given set of argument args, and returning the value of the composition applied to those arguments. - If I compose only one function, you get the value of the function itself: (value-my-comp f) = (apply f args) - By definition of composition: (value-my-comp f1 f2 fn) = (f1 (value-my-comp f2 fn)) (Take all the arguments, apply it to the composition of function f2 fn, and then apply the result to f1). Then you remove the dots: (value-my-comp f1 f2-fn) = (f1 (apply value-my-comp f2-fn)) Then you can just put them all together: (fn [ fs] ;; we take a bunch of functions in (fn [ args ] ;; we take the arguments of the resulting function in. (letfn [(value-my-comp ;; the value of the result in term of the functions is: ([f] (apply f args)) ;; only one function ([f1 f2-fn] (f1 (apply value-my-comp f2-fn] ;; more than one function (apply value-my-comp fs ;; we compute the value of the actual function we got. If you don't like (apply value-my-comp f2-fn), you can make value-my-comp actually takes a sequence of functions: (fn [ fs] ;; take the functions (fn [ args ] ;; the arguments of the resulting functions (letfn [(value-my-comp [[f1 f2-fn]] ;; a list of functions (if (empty? f2-fn) ;; only one function? (apply f1 args) ;; then we apply the args to it (f1 (value-my-comp f2-fn] ;; else we compose (value-my-comp fs This one is another way of writing the one you posted. (Using a let instead of applying it directly, and not using a non-varying argument a.) If you want to see how this function can be written to be very performant look at comp source code, it is very interesting. Best, Nicolas. -- 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: problem 58 on 4clojure
Thanks to a for the replies. I will study them later when I am free. On Aug 25, 2012 2:37 PM, nicolas.o...@gmail.com nicolas.o...@gmail.com wrote: Here's my take: We want to define a function my-comp. It takes n functions and return their composition. We want to return a function of any number of arguments, so let's start by working with a given set of argument args, and returning the value of the composition applied to those arguments. - If I compose only one function, you get the value of the function itself: (value-my-comp f) = (apply f args) - By definition of composition: (value-my-comp f1 f2 fn) = (f1 (value-my-comp f2 fn)) (Take all the arguments, apply it to the composition of function f2 fn, and then apply the result to f1). Then you remove the dots: (value-my-comp f1 f2-fn) = (f1 (apply value-my-comp f2-fn)) Then you can just put them all together: (fn [ fs] ;; we take a bunch of functions in (fn [ args ] ;; we take the arguments of the resulting function in. (letfn [(value-my-comp ;; the value of the result in term of the functions is: ([f] (apply f args)) ;; only one function ([f1 f2-fn] (f1 (apply value-my-comp f2-fn] ;; more than one function (apply value-my-comp fs ;; we compute the value of the actual function we got. If you don't like (apply value-my-comp f2-fn), you can make value-my-comp actually takes a sequence of functions: (fn [ fs] ;; take the functions (fn [ args ] ;; the arguments of the resulting functions (letfn [(value-my-comp [[f1 f2-fn]] ;; a list of functions (if (empty? f2-fn) ;; only one function? (apply f1 args) ;; then we apply the args to it (f1 (value-my-comp f2-fn] ;; else we compose (value-my-comp fs This one is another way of writing the one you posted. (Using a let instead of applying it directly, and not using a non-varying argument a.) If you want to see how this function can be written to be very performant look at comp source code, it is very interesting. Best, Nicolas. -- 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
Re: problem 58 on 4clojure
OK, I think I'm starting to get it now - the idea is a function that accepts x xs , which are functions, and returns a function which has a var-args arity [ args], which is the composition of the xs. Thanks to all for the replies - I still haven't looked at the source to comp but it will be fun to see. On Sat, Aug 25, 2012 at 2:46 PM, John Holland jbholl...@gmail.com wrote: Thanks to a for the replies. I will study them later when I am free. On Aug 25, 2012 2:37 PM, nicolas.o...@gmail.com nicolas.o...@gmail.com wrote: Here's my take: We want to define a function my-comp. It takes n functions and return their composition. We want to return a function of any number of arguments, so let's start by working with a given set of argument args, and returning the value of the composition applied to those arguments. - If I compose only one function, you get the value of the function itself: (value-my-comp f) = (apply f args) - By definition of composition: (value-my-comp f1 f2 fn) = (f1 (value-my-comp f2 fn)) (Take all the arguments, apply it to the composition of function f2 fn, and then apply the result to f1). Then you remove the dots: (value-my-comp f1 f2-fn) = (f1 (apply value-my-comp f2-fn)) Then you can just put them all together: (fn [ fs] ;; we take a bunch of functions in (fn [ args ] ;; we take the arguments of the resulting function in. (letfn [(value-my-comp ;; the value of the result in term of the functions is: ([f] (apply f args)) ;; only one function ([f1 f2-fn] (f1 (apply value-my-comp f2-fn] ;; more than one function (apply value-my-comp fs ;; we compute the value of the actual function we got. If you don't like (apply value-my-comp f2-fn), you can make value-my-comp actually takes a sequence of functions: (fn [ fs] ;; take the functions (fn [ args ] ;; the arguments of the resulting functions (letfn [(value-my-comp [[f1 f2-fn]] ;; a list of functions (if (empty? f2-fn) ;; only one function? (apply f1 args) ;; then we apply the args to it (f1 (value-my-comp f2-fn] ;; else we compose (value-my-comp fs This one is another way of writing the one you posted. (Using a let instead of applying it directly, and not using a non-varying argument a.) If you want to see how this function can be written to be very performant look at comp source code, it is very interesting. Best, Nicolas. -- 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 -- __ Note new email address jbholl...@gmail.com -- 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