Compulsory let?


#1

Hello clojurists

I don’t understand the behavior of the following code:

(-> 1 (partial + 1)) 

I expect this expression to return 2 but instead I see #function[clojure.core/partial/fn--4761]

However, if I use a let I see the expected value:

(let [add1 (partial + 1)]
  (-> 1 add1)) ; 2

Is it due to -> which is a macro? Or something else?

Thanks!


#2

Hi @Lud,

Great question.

Try macroexpanding your initial expression? the -> macro treats lists (expressions with parens) differently from others.

(macroexpand '(-> 1 (partial + 1))) ;; => (partial 1 + 1)

Eric


#3

Hi Lud,

You are probably trying to get (+ 1 1), which will return 2. In this case, there is no need for partial. Instead, just do

(-> 1 (+ 1))

You may find it easier for larger expressions to use the (it-> ...) form found in the Tupelo library, which works like this:

(ns xyz
  (:use tupelo.core))
(it-> 1
      (+ 1 it))
;=> 2

This version uses the placeholder name it in each threading form to show exactly where you want the results of the previous form to be inserted. Enjoy!


#4

Thanks to both of you! It makes more sense now. It’s time for me to have a closer look at the macro of clojure :slight_smile: