|
@@ -60,6 +60,72 @@ fn nullalgebraicdatatypes(x:MyOption<i32>) -> bool {
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
+// lambda expressions & higher-order functions
|
|
|
|
+
|
|
|
|
+// ||int,int| -> int, int| -> int
|
|
|
|
+// fn ff(f:(|i32,i32|->i32), x:i32) -> i32 { f(x, x) }
|
|
|
|
+// can't put bare function types in there, must make an
|
|
|
|
+// ad-hoc named type and use that
|
|
|
|
+fn ff<F: Fn(i32,i32) -> i32> (f:F, x:i32) -> i32 { f(x,x) }
|
|
|
|
+
|
|
|
|
+// m2: |int| -> int
|
|
|
|
+fn m2(n:i32) -> i32 { ff((|x,y| { x + y }), n) }
|
|
|
|
+
|
|
|
|
+// trying some function type stuff
|
|
|
|
+
|
|
|
|
+type F = Fn(i32,i32) -> i32;
|
|
|
|
+
|
|
|
|
+// fn blergh : F(x,y) -- doesn't work
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+// type classes
|
|
|
|
+trait Testable {
|
|
|
|
+ fn test(&self) -> bool;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+impl Testable for i32 {
|
|
|
|
+ fn test(&self) -> bool {
|
|
|
|
+ if *self == 0 { false }
|
|
|
|
+ else { true }
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+fn hellotest(x:i32) -> bool {
|
|
|
|
+ x.test()
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+// operator overloading
|
|
|
|
+
|
|
|
|
+enum Peano {
|
|
|
|
+ PZero,
|
|
|
|
+ Succ(Box<Peano>)
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+impl PartialEq for Box<Peano> {
|
|
|
|
+ fn eq(&self, other:&Box<Peano>) -> bool {
|
|
|
|
+ (self.deref() == other.deref())
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+impl PartialEq for Peano {
|
|
|
|
+ fn eq(&self, other:&Peano) -> bool {
|
|
|
|
+ use Peano::PZero;
|
|
|
|
+ use Peano::Succ;
|
|
|
|
+ match (self, other) {
|
|
|
|
+ (&PZero, &PZero) => true,
|
|
|
|
+ (&Succ(ref a), &Succ(ref b)) => (a == b),
|
|
|
|
+ (_, _) => false
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+trait PartialEq {
|
|
|
|
+ fn eq(&self, other:&Self) -> bool;
|
|
|
|
+ fn ne(&self, other:&Self) -> bool
|
|
|
|
+ { ! self.eq(other) }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
|
|
// function types and definitions
|
|
// function types and definitions
|
|
|
|
|
|
@@ -118,7 +184,7 @@ fn main() {
|
|
// println!("fact: {}", fact(start as u64));
|
|
// println!("fact: {}", fact(start as u64));
|
|
|
|
|
|
for i in 1..10 {
|
|
for i in 1..10 {
|
|
- println!("{}, {}", i, fact_rec(i))
|
|
|
|
|
|
+ println!("{}, {}, {}", i, fact_rec(i), m2(i as i32))
|
|
}
|
|
}
|
|
|
|
|
|
for i in 1..10 {
|
|
for i in 1..10 {
|