cgrand / xforms

Extra transducers and reducing fns for Clojure(script)
575 stars 32 forks source link

`transjuxt` documentation and name unclear #26

Open glfmnbw opened 6 years ago

glfmnbw commented 6 years ago

When I heard the name of transjuxt I assumed it could perform a transformation like so:

[[:a :b] [:a :b] [:a :b]] -> [[:a :a :a] [:b :b :b]]

Or act as if juxt defined a function:

((juxt (partial into [] (map inc))
          (partial into [] (map dec)))
 (range 100)

but using transjuxt inside the stack like so to avoid multiple traversals:

(into [] (transjuxt [(map inc) (map dec)]) (range 100))

However, I can't get it to behave this way. I can't tell if I've misunderstood its use and purpose, or if I have called it wrong.

Can you add an example to the documentation of what it's output might look like in practice and how to call it?

cgrand commented 6 years ago

transjuxt ever produces only one value made of the first value produced by each of its transducers arguments: .

=> (into [] (x/transjuxt [(map first) (map second)]) [[:a :b] [:a :b] [:a :b]])
[[:a :b]]
; unexpected, let's see which :a and :b we are talking about:
=> (into [] (x/transjuxt [(map first) (map second)]) [[:a :b] [:c :d] [:e :f]])
[[:a :b]]
; ok the first ones (as explained above)
; if we want to get all values for each map we have to collect them with x/into:
=> (into [] (x/transjuxt [(comp (map first) (x/into [])) (comp (map second) (x/into []))]) [[:a :b] [:a :b] [:a :b]])
[[[:a :a :a] [:b :b :b]]]
; ok but we have an extra nesting due to the top-level into [], let's use x/some!
=> (x/some (x/transjuxt [(comp (map first) (x/into [])) (comp (map second) (x/into []))]) [[:a :b] [:a :b] [:a :b]])
[[:a :a :a] [:b :b :b]]
; but in this case it's just simpler to call transjuxt with two arguments (xforms and items source):
=> (x/transjuxt [(comp (map first) (x/into [])) (comp (map second) (x/into []))] [[:a :b] [:a :b] [:a :b]])
[[:a :a :a] [:b :b :b]]

Hope this helps

cgrand commented 6 years ago

Note to self: review existing docstring