Skip to content

Instantly share code, notes, and snippets.

View domgetter's full-sized avatar

Dominic Muller domgetter

View GitHub Profile
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)
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}
class String
def **(n)
case n
when 2
([self]*self.length).join("\n")
end
end
end
puts "hello"**2
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;};
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 } }

Vocabulary of functional programming

  • 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
map = -> f {
-> rf {
-> acc, elem {
rf[acc, f[elem]]
}
}
}
square = -> n { n**2 }
add = -> acc, n { acc + n }
(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))
;; 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)))
;; 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]