| @@ -0,0 +1,87 @@ |
| 1 | +import std/io |
| 2 | +import std/list |
| 3 | + |
| 4 | +// A trait with two parameters (ADR 0018): an impl is chosen by both. |
| 5 | +trait Convert<a, b> { |
| 6 | + fn convert(x: a) -> b |
| 7 | +} |
| 8 | + |
| 9 | +pub type Celsius { Celsius(Float) } |
| 10 | + |
| 11 | +pub type Fahrenheit { Fahrenheit(Float) } |
| 12 | + |
| 13 | +impl Convert<Celsius, Fahrenheit> { |
| 14 | + fn convert(c) { |
| 15 | + match c { Celsius(t) -> Fahrenheit(t * 9.0 / 5.0 + 32.0) } |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +impl Convert<Int, String> { |
| 20 | + fn convert(n) { "#" <> show(n) } |
| 21 | +} |
| 22 | + |
| 23 | +impl Convert<Bool, Int> { |
| 24 | + fn convert(b) { if b { 1 } else { 0 } } |
| 25 | +} |
| 26 | + |
| 27 | +// The impl's own constraint is passed as a dictionary. |
| 28 | +impl Convert<List<a>, List<b>> where Convert<a, b> { |
| 29 | + fn convert(xs) { list.map(xs, convert) } |
| 30 | +} |
| 31 | + |
| 32 | +impl Convert<Option<a>, List<b>> where Convert<a, b> { |
| 33 | + fn convert(o) { |
| 34 | + match o { Some(x) -> [convert(x)], None -> [] } |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +// A trait with several methods and two parameters. |
| 39 | +trait Store<c, k> { |
| 40 | + fn put(c: c, k: k) -> c |
| 41 | + fn has(c: c, k: k) -> Bool |
| 42 | +} |
| 43 | + |
| 44 | +pub type Names { Names(List<String>) } |
| 45 | + |
| 46 | +impl Store<Names, String> { |
| 47 | + fn put(c, k) { match c { Names(ks) -> Names([k, ..ks]) } } |
| 48 | + fn has(c, k) { match c { Names(ks) -> list.any(ks, fn(x) { x == k }) } } |
| 49 | +} |
| 50 | + |
| 51 | +impl Store<List<a>, Int> { |
| 52 | + fn put(c, _) { c } |
| 53 | + fn has(c, k) { list.length(c) > k } |
| 54 | +} |
| 55 | + |
| 56 | +// A `where` clause with two parameters. |
| 57 | +pub fn labels(xs: List<a>) -> List<String> where Convert<a, String> { |
| 58 | + convert(xs) |
| 59 | +} |
| 60 | + |
| 61 | +// Inferred: `fn(List<a>) -> List<b> where Convert<a, b>`. |
| 62 | +fn convert_each(xs) { list.map(xs, convert) } |
| 63 | + |
| 64 | +fn fill(c, keys) { list.fold(keys, c, put) } |
| 65 | + |
| 66 | +pub fn main() -> () / <io> { |
| 67 | + let f: Fahrenheit = convert(Celsius(100.0)) |
| 68 | + match f { Fahrenheit(t) -> io.println(show(t)) } |
| 69 | + io.println(string_of(labels([1, 2, 3]))) |
| 70 | + let bits: List<Int> = convert_each([True, False, True]) |
| 71 | + io.println(show(bits)) |
| 72 | + let some: List<String> = convert(Some(7)) |
| 73 | + let none: Option<Bool> = None |
| 74 | + let missing: List<Int> = convert(none) |
| 75 | + io.println(show(some) <> " " <> show(missing)) |
| 76 | + let names = fill(Names([]), ["ann", "bo"]) |
| 77 | + io.println(show(has(names, "bo")) <> " " <> show(has(names, "cy"))) |
| 78 | + io.println(show(has([10, 20], 1)) <> " " <> show(has(put([10], 5), 3))) |
| 79 | +} |
| 80 | + |
| 81 | +fn string_of(xs: List<String>) -> String { list.fold(xs, "", fn(acc, x) { acc <> x }) } |
| 82 | +//> 212.0 |
| 83 | +//> #1#2#3 |
| 84 | +//> [1, 0, 1] |
| 85 | +//> [#7] [] |
| 86 | +//> True False |
| 87 | +//> True False |