Skip to content

Instantly share code, notes, and snippets.

@aamedina
Last active December 20, 2015 21:18
Show Gist options
  • Select an option

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

Select an option

Save aamedina/6196217 to your computer and use it in GitHub Desktop.
;; just like you can create a new function in terms of a partial function, you can also compose functions.
(def false? (comp not true?))
;; see what we did there? We defined a new function, false?, which returns true if the argument is false
;; by composing two functions - true? and not.
;; true? is a function which returns true if its argument is truthy
;; and false if its argument is not truthy, which in Clojure is only one of two values - false and nil.
;; in math, when you compose functions, you "unroll" them from the innermost function call outward.
;; let f(x) = x^2, and g(x) = x^3. f . g ( . being the compose operation) == f(g(x))
;; let x be 2. f(2) = 4, g(2) = 8. f(g(2)) = f(8) = 64.
;; back to our example, let f be the function "not", and g be the function "true?"
;; not inverts the boolean value given, so (not true) = false and (not false) = true.
;; (true? false) = false, and (true? true) = true.
;; hence, f(g(false)) = f(false) = true!
(defn squared
[x]
(* x x))
(defn cubed
[x]
(* x x x))
(def squared-cubed (comp squared cubed))
(squared-cubed 2)
64
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment