Created
May 31, 2013 18:54
-
-
Save MarkJr94/5687112 to your computer and use it in GitHub Desktop.
Simple Chain outline
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
fn add_one(n: int) ⟶ int { | |
n + 1 | |
} | |
fn times_two(n: int) ⟶ int { | |
n * 2 | |
} | |
fn chain<A,B,C>(f1: ~fn(a: A) ⟶ B,f2: ~fn(b: B) ⟶ C) ⟶ ~fn(A) ⟶ C { | |
|x: A| { | |
f2(f1(x)) | |
} | |
} | |
fn main() { | |
let plus_one_times_two = chain(add_one,times_two); | |
let times_two_plus_one = chain(times_two,add_one); | |
let res1 = plus_one_times_two(50); | |
let res2 = times_two_plus_one(50); | |
println(fmt!("plus_one_times_two(50) = %d",res1)); | |
println(fmt!("times_two_plus_one(50) = %d",res2)); | |
} | |
// Output | |
// plus_one_times_two(50) = 102 | |
// times_two_plus_one(50) = 101 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment