Mercurial > repos > tabletprog
view modules/dict.tp @ 242:0e7982adc76b
Make the successful return value from a match expression be truthy and the failure value false. This avoids an extra method call when checking the result and avoids allocating a new object when a match fails.
author | Mike Pavone <pavone@retrodev.com> |
---|---|
date | Sun, 05 Jan 2014 20:56:25 -0800 |
parents | cea671c4056c |
children | 96fdc5b37ceb |
line wrap: on
line source
{ linearWithEls <- :els { key:val <- :k v { #{ key <- k val <- v } } find <- :tofind { idx <- 0 while: { if: idx < (els length) { ((els get: idx) key: ) != tofind } else: {false} } do: { idx <- idx + 1 } if: idx < (els length) {idx} else: {-1} } #{ set <- :k v { idx <- find: k if: idx < 0 { els append: (key: k val: v) } else: { (els get: idx) val!: v } self } get <- :k { get: k withDefault: false } get:withDefault <- :k default { idx <- find: k if: idx < 0 { default } else: { (els get: idx) val } } get:elseSet <- :k :else { idx <- find: k if: idx < 0 { v <- else: els append: (key: k val: v) v } else: { (els get: idx) val } } contains? <- :k { (find: k) >= 0 } foreach <- :l { foreach: els :idx el { l: (el key) (el val) } } map <- :fun { newels <- #[] foreach: els :idx el { newels append: (key: (el key) val: (fun: (el val))) } linearWithEls: newels } length <- { els length } } } #{ //requires only that keys support equality linear <- { linearWithEls: #[] } } }