-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlists-and-recursion-5.exs
56 lines (43 loc) · 1.76 KB
/
lists-and-recursion-5.exs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# Exercise:ListsAndRecursion-5
# Implement the following Enum functions using no library functions
# or list comprehensions: all?, each, filter, split and take. You may need to
# use an if statement to implement filter.
defmodule CustomEnum do
def all?([], _func), do: true
def all?([ head | tail ], func), do: func.(head) and all?(tail, func)
def each([], _func), do: :ok
def each([ head | tail ], func), do: func.(head) && each(tail, func)
def filter([], _func), do: []
def filter([ head | tail ], func), do: (if (func.(head)), do: [head | filter(tail, func)], else: filter(tail, func) )
def split([], _num), do: []
def split(list, num), do: split_helper(list, [], num)
defp split_helper(rest, acc, num) when num == 0 or rest == [], do: {Enum.reverse(acc), rest}
defp split_helper([ head | tail ], acc, num), do: split_helper(tail, [head | acc], num - 1)
def take([], _num), do: []
def take(_, num) when num == 0, do: []
def take([ head | tail ], num) when num > 0, do: [head | take(tail, num - 1)]
def take(list, num) when num < 0, do: take_helper(list, num, 0, length(list))
defp take_helper(list, num, counter, length) when counter - length >= num, do: take(list, abs(num))
defp take_helper([ _head | tail ], num, counter, length), do: take_helper(tail, num, counter + 1, length)
end
list = [1, 2, 3, 4]
IO.inspect CustomEnum.all?(list, &(&1 > 0))
# => true
IO.inspect CustomEnum.each(list, &(IO.puts &1))
# 1
# 2
# 3
# 4
# => :ok
IO.inspect CustomEnum.filter(list, &(&1 > 2))
# => [3, 4]
IO.inspect CustomEnum.split(list, 3)
# => {[1, 2, 3], [4]}
IO.inspect CustomEnum.take(list, 2)
# => [1, 2]
IO.inspect CustomEnum.take(list, -2)
# => [3, 4]
IO.inspect CustomEnum.take(list, 6)
# => [1, 2, 3, 4]
IO.inspect CustomEnum.take(list, -6)
# => [1, 2, 3, 4]