Skip to content

Instantly share code, notes, and snippets.

@rintaun
Created April 23, 2013 18:06
Show Gist options
  • Save rintaun/5445933 to your computer and use it in GitHub Desktop.
Save rintaun/5445933 to your computer and use it in GitHub Desktop.
head = ->(list) { list.call {|h, _, _| h } }
tail = ->(list) { list.call {|_, t, _| t } }
is_empty = ->(list) { list.call {|_, _, e| e } }
empty = ->(&selector) do
selector[nil, nil, true]
end
prepend = ->(el, list) do
->(&selector) { selector[el, list, false] }
end
map = ->(list, &fn) do
is_empty[list] ? empty : prepend[fn[head[list]], map[tail[list], &fn]]
end
filter = ->(list, &fn) do
return empty if is_empty[list]
out = filter[tail[list], &fn] || empty
prepend[head[list], out] if fn[head[list]]
end
append = ->(el, list) do
return prepend[el, empty] if is_empty[list]
prepend[head[list], append[el, tail[list]]]
end
last = ->(list) do
return head[list] if is_empty[tail[list]]
last[tail[list]]
end
list = prepend["a", prepend["b", prepend["c", empty]]]
list = append["d", list]
map.call(list) {|i| p i }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment