annotate modules/list.tp @ 188:7e313849ab41

Add join method to lists
author Mike Pavone <pavone@retrodev.com>
date Mon, 26 Aug 2013 18:21:57 -0700
parents 158444b77c09
children 372cbd2cd243
rev   line source
pavone@170 1 {
pavone@170 2 _empty <- #{
pavone@170 3 length <- { 0 }
pavone@170 4 empty? <- { true }
pavone@170 5 fold:with <- :acc :fun { acc }
pavone@170 6 foldr:with <- :acc :fun { acc }
pavone@170 7 map <- :fun { self }
pavone@170 8 | <- :val {
pavone@170 9 list node: val withTail: self
pavone@170 10 }
pavone@170 11 . <- :right { right }
pavone@188 12 join <- { "" }
pavone@173 13 string <- { "[]" }
pavone@173 14 print <- { print: string }
pavone@170 15 }
pavone@170 16 #{
pavone@170 17 empty <- { _empty }
pavone@170 18 node:withTail <- :_val :_tail {
pavone@170 19 #{
pavone@170 20 value <- { _val }
pavone@170 21 tail <- { _tail }
pavone@170 22 empty? <- { false }
pavone@170 23 length <- {
pavone@170 24 fold: 0 with: :acc val { acc + 1 }
pavone@170 25 }
pavone@170 26 fold:with <- :acc :fun {
pavone@170 27 cur <- self
pavone@170 28 while: { not: (cur empty?)} do: {
pavone@170 29 acc <- fun: acc (cur value)
pavone@170 30 cur <- cur tail
pavone@170 31 }
pavone@170 32 acc
pavone@170 33 }
pavone@170 34 foldr:with <- :acc fun {
pavone@170 35 fun: (_tail foldr: acc with: fun) _val
pavone@170 36 }
pavone@170 37 map <- :fun {
pavone@170 38 node: (fun: _val) withTail: (_tail map: fun)
pavone@170 39 }
pavone@173 40 foreach <- :self fun {
pavone@173 41 fold: 0 with: :idx el{
pavone@173 42 fun: idx el
pavone@173 43 idx + 1
pavone@173 44 }
pavone@173 45 self
pavone@173 46 }
pavone@170 47 | <- :val {
pavone@170 48 node: val withTail: self
pavone@170 49 }
pavone@170 50 . <- :right {
pavone@170 51 foldr: right with: :tail val {
pavone@170 52 node: val withTail: tail
pavone@170 53 }
pavone@170 54 }
pavone@188 55 join <- :sep {
pavone@188 56 _tail fold: (string: _val) with: :acc el {
pavone@188 57 acc . sep . el
pavone@188 58 }
pavone@188 59 }
pavone@173 60 string <- {
pavone@173 61 (fold: "[\n" with: :acc el {
pavone@173 62 acc . " " . (string: el) . "\n"
pavone@173 63 }) . "]"
pavone@173 64 }
pavone@173 65 print <- {
pavone@173 66 print: "[\n"
pavone@173 67 foreach: :_ el {
pavone@173 68 print: " " . (string: el) . "\n"
pavone@173 69 }
pavone@173 70 print: "]"
pavone@173 71 }
pavone@170 72 }
pavone@170 73 }
pavone@170 74 }
pavone@170 75 }