murtaza52
murtaza52

Reputation: 47441

how to work with collections implementing IReduceInit

The plan function of next.jdbc returns collections which implement IReduceInit, what does that mean ? Trying to wrap my head around it, what is the difference between having a regular collection and then applying transducers to it, rather than having a coll which implements the above interface.

Also how do you work with a coll which implements the above interface, cant find any material on it.

In the below code -

(def results (jdbc/plan @datasource2 ["select * from cabs"]))

(into [] (map :name) results) => ["a", "b"]

(into [] (map identity) results) => gives an error - [..... missing `map` or `reduce` {row}]

Why does the first transducer work while the second one fails ?

Upvotes: 2

Views: 144

Answers (1)

Sean Corfield
Sean Corfield

Reputation: 6666

identity simply returns its argument without touching it so (map identity) doesn't "touch" the rows and they are not realized.

jdbc/plan requires that each row be "touched" to either select elements from it (which does not realize a Clojure data structure, it simply extracts fields from the current row of the underlying ResultSet), or to force realization through various hash map operations: assoc, cons (which implicitly calls seq), dissoc, iterator, seq, etc.

This is not just about collections and IReduceInit -- jdbc/plan adds an additional layer which is the "lazy" representation of the current row of the underlying mutable ResultSet object.

Upvotes: 1

Related Questions