diff options
| author | henesy <henesy.dev@gmail.com> | 2019-03-11 17:39:03 -0500 |
|---|---|---|
| committer | henesy <henesy.dev@gmail.com> | 2019-03-11 17:39:03 -0500 |
| commit | 1f76d9d4cf660c5fb66d406c5488ce4d734d8e25 (patch) | |
| tree | f1f466639f24fec97e37e5e1cc02c1a8d9b28480 /Generics | |
| parent | b88feaef76f1c93fc3e0c4125a8bbb2f536878e3 (diff) | |
more work on generics
Diffstat (limited to 'Generics')
| -rw-r--r-- | Generics/README.md | 64 | ||||
| -rw-r--r-- | Generics/generics.b | 20 |
2 files changed, 78 insertions, 6 deletions
diff --git a/Generics/README.md b/Generics/README.md index c381e29..a6ffbee 100644 --- a/Generics/README.md +++ b/Generics/README.md @@ -1,22 +1,78 @@ # Generics - +Limbo supports polymorphic types as well as a form of pseudo-unions and pseudo-interfaces as per C and Go respectively. Warning: This is an example about an undocumented feature of Limbo. All assertions or explanations provided are considered conjecture until proven otherwise. ## Source -### +### generics.b:17,21 + + + +### generics.b:23,32 + + + +### generics.b:34,44 + + + +### generics.b:46,50 + + + +### generics.b:56,63 + + + +`.ws()` is defined at: generics.b:103,109 + +### generics.b:65,69 + + + +### generics.b:71,83 + + + +`ismember()` is defined at: generics.b:132,144 + +`.eq()` is defined at: generics.b:111,114 + +### generics.b:85,87 + + + +`rev()` is defined at: generics.b:154,161 + +### generics.b:89,98 + + + +`pair()` is defined at: generics.b:146,152 ## Demo + ; generics + Type something: asdf + Broke on 'a' + 5 + Five + Found 9! + Head was: :( + Head is: ☺ + Head of plist: ( hello, { :(, 7 } ) + ; + ## Exercises -- Can you find a way to make the eq() function do a deep comparison of Int's? +- Can you find a way to make the `eq()` function do a deep comparison of `Int`'s? - Try removing the `.String` from the `words` definition, what happens? - Try removing `ref anywhere, see what happens. ## References -- https://github.com/caerwynj/inferno-lab/blob/master/27/sexprs.b - https://github.com/caerwynj/inferno-lab/blob/master/78/README.md +- https://github.com/caerwynj/inferno-lab/blob/master/27/sexprs.b +- /appl/lib/lists.b diff --git a/Generics/generics.b b/Generics/generics.b index 75bc9e8..085b5af 100644 --- a/Generics/generics.b +++ b/Generics/generics.b @@ -73,15 +73,30 @@ init(nil: ref Draw->Context, nil: list of string) { smiley := "☺"; frowny := ":("; + # Format is: Adt[Type].PickTag(fields...) sword := ref Word[Int].String(Int(9), smiley); - # Format is: Adt[Type].PickTag(fields...) words = sword :: words; words = ref Word[Int].String(Int(7), frowny) :: words; if(ismember(sword, words)) print("Found %d!\n", sword.w.d); + backwords := rev(words); + + print("Head was: %s\nHead is: %s\n", (hd words).s, (hd backwords).s); + + strings: list of string; + strings = "hi" :: strings; + strings = "hello" :: strings; + + # plist is a `list of (string, ref Word.String)` + plist := pair(strings, words); + + (str, word) := hd plist; + + print("Head of plist: ( %s, { %s, %d } )\n", str, word.s, word.w.d); + exit; } @@ -117,7 +132,8 @@ isspace(c: int): int { # Checks if x is a member of l ismember[T](x: T, l: list of T): int for { - T => eq: fn(a, b: T): int; + T => + eq: fn(a, b: T): int; } { for(; l != nil; l = tl l) |
