clojure

New to Clojure? Try the #beginners channel. Official docs: https://clojure.org/ Searchable message archives: https://clojurians-log.clojureverse.org/
murtaza52 2020-12-26T04:05:38.465400Z

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)

2020-12-26T05:55:34.467100Z

I feel like the call to the top of my-juxt-2 inside the cons isn’t quite right

2020-12-26T06:10:34.468Z

If you rewrite this to use loop/recur inside the innermost function it should work fine, right?

2020-12-26T06:38:52.469300Z

Currently this code is trying to cons the result of f with the function returned from my-juxt-2

murtaza52 2020-12-26T06:49:43.470600Z

yup, with loop-recur I can make it work, I am stuck on the regular recursion one.

Tamas 2020-12-26T07:50:13.471600Z

(defn my-juxt-2
  [f & fns]
  (fn [& args]
    (letfn [(jr [[f & fns]]
              (if f (cons (apply f args) (jr fns)) []))]
      (jr (cons f fns)))))

2020-12-26T07:50:22.472Z

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

2020-12-26T07:50:25.472200Z

Yep, like that 😄

Tamas 2020-12-26T07:50:29.472400Z

how about something like this?

Tamas 2020-12-26T07:50:40.472600Z

@anders152 lol

Tamas 2020-12-26T07:53:35.474500Z

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.

Tamas 2020-12-26T07:54:59.475700Z

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.

adityaathalye 2020-12-26T08:21:12.478600Z

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

p-himik 2020-12-26T08:34:46.478700Z

I think (cycle [args]) can be replaced with (repeat args).

adityaathalye 2020-12-26T08:39:29.478900Z

And I think that would work juxt fine too...

Tamas 2020-12-26T08:40:00.479100Z

Why can't you just leave them out altogether?

➕ 1
Tamas 2020-12-26T08:40:27.479300Z

ie. do the mapv just on the fns

Tamas 2020-12-26T08:41:53.479500Z

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

adityaathalye 2020-12-26T09:11:35.479800Z

> 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

2020-12-26T10:28:33.481300Z

Using map with fns as the input is something I really gotta remember to use more, can lead to really neat solutions. Nice one!

🎉 1
2020-12-26T18:00:54.487900Z

The clj -M:dev:sql looks suspicious to me, try clj -A:dev:sql <the rest>