Last active
August 29, 2015 14:05
-
-
Save kornysietsma/2d8d92d178f94233abdd to your computer and use it in GitHub Desktop.
basic clojure functional functions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; this page: http://tinyurl.com/ndfzluz | |
; "higher order functions" basically means using functions as parameters to functions. Lots of languages have this - even C has function pointers | |
; Structure and Interpretation of Computer Programs, a classic 1993 text from MIT, says: | |
; "It is better to have 100 functions operate on one data structure than to have 10 functions operate on 10 data structures" | |
; http://mitpress.mit.edu/sicp/ | |
; simple example - define "is-even": | |
(def is-even (fn [x] (= (mod x 2) 0))) | |
(is-even 2) | |
;> true | |
(is-even 7) | |
;> false | |
; this is actually the same as the built in "even?" function! | |
; Then can filter a sequence for only even values: | |
(filter is-even [1 2 3 4]) | |
;> (2 4) | |
(filter even? [1 2 3 4]) | |
;> (2 4) | |
; exercise - filter [1 2 3 4 3 2 1] to remove all numbers greater than 2, giving [1 2 2 1] | |
; hint: there is a ">" function that tests for greater than - so (> 5 3) returns true as 5 is greater than 3 | |
;----- | |
; map - apply a function to all members of a sequence | |
(map is-even [1 2 3 4]) | |
;> (false true false true) | |
(map inc [1 2 3 4]) | |
;> (2 3 4 5) | |
; exercise - convert a list of numbers [1 2 3 4 5] into their squares [1 4 9 16 25] | |
; exercise - combine the above, convert a list of numbers [1 2 3 4 5 6 7] into the squares of any even numbers, removing any odd ones: [4 16 36] | |
; ------- | |
; reduce - applies a function to a sequence, accumulating a summary value as you go. | |
; digression: discuss Map/Reduce and Google and parallel computation. Do some hand waving! | |
; let's define the function first: | |
(def myfn (fn [memo val] (+ memo val))) | |
(myfn 1 2) | |
;> 3 | |
; you can then call reduce using that function and an initial value: | |
(reduce myfn 1 [2 3 4]) | |
;> 10 | |
; or you can use the first member of the sequence as initial value | |
(reduce myfn [1 2 3 4]) | |
;> 10 | |
; note that "myfn" is basically a version of the built in function "+": | |
(reduce + [1 2 3 4]) | |
;> 10 | |
; exercises - let's see how we are going for time, but try: | |
; find the largest value of a sequence [1 2 3 4 3 2 1] - should be 4 | |
; advanced exercise: find the average of a sequence! | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; just a quick cheat sheet of commonly used sequence functions | |
; first - first member of a sequence | |
(first [1 2 3 4]) | |
> 1 | |
; nth - any other member | |
(nth 3 [1 2 3 4]) | |
> 3 | |
; rest - all but the first | |
(rest [1 2 3 4]) | |
> (2 3 4) | |
; take - take part of a sequence | |
(take 2 [1 2 3 4]) | |
> (1 2) | |
; drop - drop some parts of a sequence | |
(drop 2 [1 2 3 4]) | |
> (3 4) | |
; count - self evident | |
(count [5 4 3 2 1]) | |
> 5 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment