Implement filter
I struggled a bit on this one, it took me two months!!!! jk My assumption was that filter with an empty list does nothing, but actually, it returns an empty list. Second misunderstanding was that filter with tail doesn't need to be wrapped into another list. But hey, in the end I learned something!
This commit is contained in:
parent
ab927374b9
commit
1be7896f19
|
@ -30,7 +30,21 @@ defmodule MyEnum do
|
|||
:ok
|
||||
end
|
||||
|
||||
# Filters the enumerable, i.e. returns only those elements for which fun returns a truthy value.
|
||||
def filter([], _fun) do
|
||||
# do nothing -> This is wrong. Return an empty array instead.
|
||||
[]
|
||||
end
|
||||
|
||||
# filter using if ... do ... else ... end
|
||||
def filter([head|tail], fun) do
|
||||
if fun.(head) do
|
||||
[head|filter(tail, fun)]
|
||||
else
|
||||
filter(tail, fun)
|
||||
end
|
||||
end
|
||||
|
||||
# split
|
||||
# take
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue