Trying to implement juxt for fun - The below using reduce works fine -
(fn [& fns]
(fn [& args]
(reduce (fn [acc f]
(cons (apply f args) acc))
[]
fns)))
however cant get the one with recursion to work -
(defn my-juxt-2
[f & fns]
(fn [& args]
(when f
(cons (apply f args) (my-juxt-2 fns)))))
((my-juxt-2 + max min) 2 3 5 1 6 4)
I feel like the call to the top of my-juxt-2
inside the cons
isn’t quite right
If you rewrite this to use loop/recur inside the innermost function it should work fine, right?
Currently this code is trying to cons the result of f
with the function returned from my-juxt-2
yup, with loop-recur I can make it work, I am stuck on the regular recursion one.
(defn my-juxt-2
[f & fns]
(fn [& args]
(letfn [(jr [[f & fns]]
(if f (cons (apply f args) (jr fns)) []))]
(jr (cons f fns)))))
loop/recur conceptually creates an inline function recursion too, so I think the answer is that you need one more internal function if you want to recur in that way
Yep, like that 😄
how about something like this?
@anders152 lol
You want to return a function that takes any arguments just once, and do the recursion inside of that, to apply the functions received by juxt one-by-one.
In the original version you had the top level function returned from my-juxt-2
so you would return that each time you recursively call it to progress the recursion.
Hah, juxt
all the things! Contributing my little homage:
(defn yet-another-juxt
"Yet another juxt, because, why not? :)"
[& fns]
(fn [& args]
(mapv (fn [f args] (apply f args))
fns
(cycle [args])))) ; <-- but of course, we can omit this entirely (discussed in the thread to this message)
#_((yet-another-juxt inc dec identity) 1) ; => [2 0 1]
#_((yet-another-juxt + - *) 1 2 3 4 5) ; => [15 -13 120]
#_((yet-another-juxt + *)) ; => [0 1]
#_((yet-another-juxt inc)) ; Barf! (As it should...)
I think (cycle [args])
can be replaced with (repeat args)
.
And I think that would work juxt fine too...
Why can't you just leave them out altogether?
ie. do the mapv
just on the fns
also, not sure if it matters, but at least as curiosity it is worth mentioning the the original juxt
does fail when you invoke it with 0
arguments
> Why can't you just leave them out altogether? Why not indeed... > also, not sure if it matters, but at least as curiosity it is worth mentioning the the original `juxt` does fail when you invoke it with `0` arguments True... On the other hand, if we're mucking around making cheap copies, why not take some creative license? 😁
(defn yet-another-yet-another-juxt
"Yet another yet another juxt, because, why not why not? :)"
[& fns]
(fn [& args]
(seq (mapv (fn [f] (apply f args))
fns))))
#_((yet-another-yet-another-juxt inc dec identity) 1) ; => (2 0 1)
#_((yet-another-yet-another-juxt + - *) 1 2 3 4 5) ; => (15 -13 120)
#_((yet-another-yet-another-juxt + *)) ; => (0 1)
#_((yet-another-yet-another-juxt inc)) ; Barf! (As it should...)
#_((yet-another-yet-another-juxt)) ; => nil ; The juxt of nothing over nothing can be nothing
Using map with fns as the input is something I really gotta remember to use more, can lead to really neat solutions. Nice one!
The clj -M:dev:sql
looks suspicious to me, try clj -A:dev:sql <the rest>