view modules/list.tp @ 377:93c28eee141e tip

Merge
author Michael Pavone <pavone@retrodev.com>
date Sat, 15 Aug 2015 22:45:33 -0700
parents eef8a5cea812
children
line wrap: on
line source
{
	_empty <- #{
		length <- { 0 }
		empty? <- { true }
		fold:with <- :acc :fun { acc }
		foldr:with <- :acc :fun { acc }
		filter <- :pred { self }
		foreach <- :self fun { self }
		map <- :fun { self }
		| <- :val {
			list node: val withTail: self
		}
		. <- :right { right }
		reverse <- { self }
		join <- { "" }
		contains? <- :val { false }
		find <- :pred { option none }
		string <- { "[]" }
		print <- { print: string }
		jsonEncode <- { "[]" }
	}
	#{
		empty <- { _empty }
		node:withTail <- :_val :_tail {
			#{
				value <- { _val }
				tail <- { _tail }
				empty? <- { false }
				length <- {
					fold: 0 with: :acc val { acc + 1 }
				}
				fold:with <- :acc :fun {
					cur <- self
					while: { not: (cur empty?)} do: {
						acc <- fun: acc (cur value)
						cur <- cur tail
					}
					acc
				}
				foldr:with <- :acc fun {
					fun: (_tail foldr: acc with: fun) _val
				}
				filter <- :pred {
					reverse: (fold: [] with: :acc el {
						if: (pred: el) { el | acc } else: { acc }
					})
				}
				map <- :fun {
					node: (fun: _val) withTail: (_tail map: fun)
				}
				foreach <- :self fun {
					fold: 0 with: :idx el{
						fun: idx el
						idx + 1
					}
					self
				}
				| <- :val {
					node: val withTail: self
				}
				. <- :right {
					foldr: right with: :tail val {
						node: val withTail: tail
					}
				}
				reverse <- {
					fold: [] with: :tail val {
						node: val withTail: tail
					}
				}
				join <- :sep {
					_tail fold: (string: _val) with: :acc el {
						acc . sep . el
					}
				}
				contains? <- :val {
					if: _val = val {
						true
					} else: {
						_tail contains?: val
					}
				}

				find <- :pred {
					if: (pred: _val) {
						option value: _val
					} else: {
						_tail find: pred
					}
				}

				string <- {
					(fold: "[\n" with: :acc el {
						acc . "	" . (string: el) . "\n"
					}) . "]"
				}
				print <- {
					print: "[\n"
					foreach: :_ el {
						print: "	" . (string: el) . "\n"
					}
					print: "]"
				}

				jsonEncode <- {
					parts <- map: :el { jsonEncoder encode: el }
					"[" . (parts join: ",") . "]"
				}
			}
		}
	}
}