Skip to content

Instantly share code, notes, and snippets.

@aamedina
Created August 9, 2013 18:50
Show Gist options
  • Select an option

  • Save aamedina/6196120 to your computer and use it in GitHub Desktop.

Select an option

Save aamedina/6196120 to your computer and use it in GitHub Desktop.
;; What's a partial function?
(partial + 1)
;; this partial function is a function which returns a function whose operator is "addition" and whose first parameter is 1
;; let's give this a name to give you some intution.
(def inc (partial + 1))
;; we all know what inc does, it inc(rements) a value!
;; (inc 1) returns 2!
;; we defined incrementing in terms of a partially "called" function - addition normally needs at least two arguments - it's a binary operation.
;; but since we used partial, it only needs at least 1 argument now, since the first argument to addition is already defined!
(inc 10)
11
(inc 15)
16
;; and so on.
(defn inc
[n]
(+ n 1))
;; is how you could define a function which performs the same thing. But using a partial function is a powerful abstraction, it allows us to extend
;; already implemented functions in a natural way
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment