Created
June 26, 2012 16:27
-
-
Save msullivan/2996876 to your computer and use it in GitHub Desktop.
traits thing that doesn't work
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
import dvec::dvec; | |
iface core_iter<A> { | |
fn each2(blk: fn(A) -> bool); | |
} | |
impl core_iter<A> for option<A> { | |
fn each2(f: fn(A) -> bool) { | |
alt self { | |
none { } | |
some(a) { f(a); } | |
} | |
} | |
} | |
impl core_iter<A> for dvec<A> { | |
fn each2(f: fn(A) -> bool) { | |
import dvec::extensions; | |
self.swap { |v| vec::each(v, f); v } | |
} | |
} | |
impl extensions<A,IA:core_iter<A>> for IA { | |
fn all2(blk: fn(A) -> bool) -> bool { | |
for self.each2 {|a| | |
if !blk(a) { ret false; } | |
} | |
ret true; | |
} | |
fn foldl2<B>(+b0: B, blk: fn(B, A) -> B) -> B { | |
let mut b <- b0; | |
for self.each2 {|a| | |
b = blk(b, a); | |
} | |
ret b; | |
} | |
} | |
fn main() { | |
for some(5).each2() {|x| | |
log(error, x); | |
} | |
//let z = some(5).foldl2(0, {|a, b| a + b}); | |
let z = some(5).all2() {|a| a == 5}; | |
log(error, z); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment