view modules/dict.tp @ 164:75be44ed9df5

Dict improvements
author Mike Pavone <pavone@retrodev.com>
date Sat, 10 Aug 2013 22:26:26 -0700
parents 6735db9b44ba
children a868a2aec930
line wrap: on
line source
#{
	//requires only that keys support equality
	linear <- {
		key:val <- :k v {
			#{
				key <- k
				val <- v
			}
		}
		els <- #[]
		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
				}
			}

			foreach <- :l {
				foreach: els :idx el {
					l: (el key) (el val)
				}
			}

			length <- { els length }
		}
	}

}