Search code examples
clojure

How to identify the variable of thread-last?


Consider the following code:

(defn delete-last-line [list]
  (take (- (count list) 1) list)
  )

(->>
(create-list)
(delete-last-line))

Now i would like to replace delete-last-line by an anonymous function in thread-last. Something like the following, but this will not work. The problem is the i need to somehow have an identifier of the variable.

 (take (- (count %) 1))

Solution

  • Put an extra pair of parens around your anonymous function so you're calling it; that way ->> threads into the invocation of the function, not its definition.

    (->>
      (create-list)
      (#(take (- (count %) 1) %)))
    

    I don't know what you mean by "somehow have an identifier of the variable", but if you want to give it a name instead of using %, you can always do that:

    (println
      (->>
        (create-list)
        ((fn [lst] (take (- (count lst) 1) lst)))))