0

The re-frame todomvc views namespace contains a function todo-item which contains the following snippet:

(when @editing
         [todo-edit {:class "edit"
                     :title title
                     :on-save #(dispatch [:save id %])
                     :on-stop #(reset! editing false)}])

the :on-save key is passed to and used in the todo-input function which contains the following snippet:

let [val (atom title)
        stop #(do (reset! val "")
                  (if on-stop (on-stop)))
        save #(let [v (-> @val str clojure.string/trim)]
               (if-not (empty? v) (on-save v))
               (stop))]

What is the meaning of the % character in the first snippet:

:on-save #(dispatch [:save id %])

and how should I interpret in the second snippet:

(on-save v)

?

Find the todomvc views namespace here.

nilo de roock
  • 4,077
  • 4
  • 34
  • 62

1 Answers1

2

It is shorthand for this inline anonymous function definition

(fn [x] 
  (dispatch [:save id x]))

For more info please see:

Gra
  • 1,542
  • 14
  • 28
Alan Thompson
  • 29,276
  • 6
  • 41
  • 48
  • Also: https://clojure.org/reference/reader#_dispatch (I tried to edit this answer, waiting for the review) – Gra Aug 13 '17 at 17:11