Last active
October 19, 2016 00:33
-
-
Save StevenJL/e33fda8f74af024b93da to your computer and use it in GitHub Desktop.
Writing foldr and foldl in elixir
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Looks like List.foldr and List.foldl are delegated to erlang: | |
# https://github.com/elixir-lang/elixir/blob/v1.0.3/lib/elixir/lib/list.ex#L116L118 | |
# But if we wanted to implement these in elixir, seems like we can only write fold1, but not foldr | |
# Question: Can we define `foldr` without using high-level functions (ie. List.reverse) | |
# and using a recursive approach similar to `foldl`? I really can't think how but would be | |
# very interested if someone can show me a solution (if it exists). | |
defmodule MyList do | |
def foldl([], acc, _) do | |
acc | |
end | |
def foldl([head|tail], acc, func) do | |
foldl(tail, func.(head, acc), func) | |
end | |
def foldr() do | |
end | |
end | |
ExUnit.start | |
defmodule MyListTest do | |
alias MyList, as: L | |
use ExUnit.Case | |
test "it folds a list from right to left" do | |
list = ["e", "l", "i", "x", "i", "r"] | |
assert L.foldl(list, "", &(&1 <> &2)) == "rixile" | |
end | |
test "it folds a list from left to right" do | |
list = ["e", "l", "i", "x", "i", "r"] | |
assert L.foldr(list, "", &(&1 <> &2)) == "elixir" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
And then to demonstrate we are actually folding from the right 'MyList.foldr([1,2,3,4],1,&MyList.dbg/2)' yields: