Skip to content

Instantly share code, notes, and snippets.

@ssledz
Created September 8, 2019 09:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ssledz/03848122d59c1666ad2996664754f333 to your computer and use it in GitHub Desktop.
Save ssledz/03848122d59c1666ad2996664754f333 to your computer and use it in GitHub Desktop.

Defining Functions

user=> (defn force-it
       "The first function a young Jedi needs"
       [jedi]
       (str "Use the force," jedi))

user=> (force-it "Luke")
"Use the force,Luke"
user=> (doc force-it)                                                                                                                                
-------------------------                                                                                                                                     
my-stuff.core/force-it                                                                                                                                        
([jedi])                                                                                                                                                      
  The first function a young Jedi needs                                                                                                                       
nil              

Higher order functions

Syntax of Anonymous Functions

user=> (def people ["Lea", "Han Solo"])
user=> (map (fn [w] (* 2 (count w))) people)
(6 16)
user=> (map #(* 2 (count %)) people)
(6 16)

filter

user=> (def v [3 1 2])
user=> (filter odd? v)
(3 1)
user=> (filter #(< % 3) v)
(1 2)

apply

user=> (apply + v)
6
user=> (apply max v)
3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment