- first-class function
- when you can store a function in a variable
- anonymous function
- a function without a name
- closures and pure functions
- closure - function with at least one free variable (variable defined elsewhere)
- pure function - a function where the variables are bound to the function's scope
- also, a function that always returns the same output for some input
This file contains 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
require 'io/console' | |
Kernel.load('Auspice/Auspice.rb') | |
module Auspice | |
class SplashScreen | |
def initialize | |
@win = Screen.new | |
#@sin = Gosu::Image.new(@win, Surface.new(64,64,[100,100,100,100]),false) | |
@words = BottomFeeder.new 24,800,400 | |
#@gl = OpenGLInjection.new | |
#@win.addent(@gl) |
This file contains 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
int[] numbers = {1, 2, 3, 4}; | |
HashMap myMap = new HashMap(); | |
for(int i; i < numbers.length(); i++) { | |
myMap.put(numbers[i], numbers[i]*numbers[i]); | |
} | |
// You will get a HashMap that looks like this: {1: 1, 2: 4, 3: 9, 4: 16} |
This file contains 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
class String | |
def **(n) | |
case n | |
when 2 | |
([self]*self.length).join("\n") | |
end | |
end | |
end | |
puts "hello"**2 |
This file contains 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
map = function(f) { | |
return function(rf) { | |
return function(acc, elem) { | |
return rf(acc, f(elem)); | |
}; | |
}; | |
}; | |
square = function(n) {return n*n;}; | |
add = function(a,b) {return a+b;}; |
This file contains 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
map = -> f, rf, acc, elem { | |
rf[acc, f[elem]] | |
} | |
[1,2,3].reduce(0, map.curry[-> n {puts n; n}][:+.to_proc]) | |
[1,2,3].reduce(0, &-> rf, acc, elem { rf[acc, elem] }.curry[:+.to_proc]) | |
identity = -> x { x } | |
zero = -> f { -> x { x } } |
This file contains 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
map = -> f { | |
-> rf { | |
-> acc, elem { | |
rf[acc, f[elem]] | |
} | |
} | |
} | |
square = -> n { n**2 } | |
add = -> acc, n { acc + n } |
This file contains 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
(defn notify [n] (println (str "Working on: " n)) n) | |
(reduce + 0 (map notify (range 4))) | |
;=> Working on: 0 | |
;=> Working on: 1 | |
;=> Working on: 2 | |
;=> Working on: 3 | |
;=> 6 | |
(redcue ((map notify) +) 0 (range 4)) |
This file contains 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
;; without transducers | |
(defn notify [n] | |
(println (str "Doing something to: " n)) | |
n) | |
(defn notify-and-add-up | |
([] 0) | |
([result] result) | |
([result input] (+ result (notify input))) |
This file contains 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
;; The tranducer-returning arity of map does as follows: | |
(defn map [f] | |
(fn [rf] | |
(fn | |
([] (rf)) | |
([result] (rf result)) | |
([result input] | |
(rf result (f input))) | |
([result input & inputs] |