2

In Clojure, what does calling map with one argument, like this:

(map inc) ;=> #object[clojure.core$map$fn__4549 0x1decdb9d "clojure.core$map$fn__4549@1decdb9d"]

...do / return? Because it doesn't do auto currying as expected, so the following two expressions are not equivalent:

;; E1
((map inc) [100 200 300]) ;=> #object[clojure.core$map$fn__4549$fn__4550 0x1b0c8974 "clojure.core$map$fn__4549$fn__4550@1b0c8974"]

;; E2
((partial map inc) [100 200 300]) ;=> (101 201 301)

...and the documentation says nothing.

So what IS the mysterious function returned by (map inc) and other similar expressions?

Nils Blum-Oeste
  • 5,608
  • 4
  • 25
  • 26
NeuronQ
  • 7,527
  • 9
  • 42
  • 60
  • Which version are you using? `(map inc)` throws an `ArityException` on 1.6.0. – Lee Oct 08 '15 at 19:33
  • 1
    It's a transducer. See [http://stackoverflow.com/questions/26317325/can-someone-explain-clojure-transducers-to-me-in-simple-terms](http://stackoverflow.com/questions/26317325/can-someone-explain-clojure-transducers-to-me-in-simple-terms) – user5187212 Oct 08 '15 at 19:33
  • @user5187212 ah, so the mythical transducers... guess I'll have to read up on them – NeuronQ Oct 08 '15 at 19:42

1 Answers1

4

calling map with a single argument in clojure 1.7+ returns a transducer that can be composed with other transducers and eventually a call to into or some other transducing thing.

I recommend this video first, and then this one

This allows you to use all the clojure sequence abstractions without the computer spending a lot of time building intermediate sequences.

In clojure less than 1.7 it will throw an arity exception

Arthur Ulfeldt
  • 90,827
  • 27
  • 201
  • 284