/
traversable.ex
243 lines (195 loc) · 6.33 KB
/
traversable.ex
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
import TypeClass
defclass Witchcraft.Traversable do
@moduledoc """
Walk through a data structure from left to right,
running some action on each element in turn.
Similar to applicatives, it can be used to do things like collecting some effect
while performing other actions.
## Type Class
An instance of `Witchcraft.Traversable` must also implement `Witchcraft.Foldable`
and `Witchcraft.Functor`, and define `Witchcraft.Foldable.right_fold/3`.
[right_fold/3] Foldable Functor [map/2]
↓ ↓
Traversable
[right_fold/3]
"""
alias __MODULE__
alias Witchcraft.Foldable
extend Witchcraft.Foldable
extend Witchcraft.Functor
use Witchcraft.Internal, deps: [Witchcraft.Foldable, Witchcraft.Functor]
use Witchcraft.Applicative
use Witchcraft.Foldable, except: [equal?: 2]
use Quark
@type t :: any()
@type link :: (any() -> Traversable.t())
where do
@doc """
Convert elements to actions, and then evaluate the actions from left-to-right,
and accumulate the results.
For a version without accumulation, see `then_traverse/2`.
## Examples
iex> traverse([1, 2, 3], fn x -> {x, x * 2, x * 10} end)
{6, 12, [10, 20, 30]}
iex> traverse({1, 2, 3}, fn x -> [x] end)
[{1, 2, 3}]
iex> traverse({1, 2, 3}, fn x -> [x, x * 5, x * 10] end)
[
{1, 2, 3},
{1, 2, 15},
{1, 2, 30}
]
iex> traverse([1, 2, 3], fn x -> [x, x * 5, x * 10] end)
[
#
[1, 2, 3], [1, 2, 15], [1, 2, 30],
[1, 10, 3], [1, 10, 15], [1, 10, 30],
[1, 20, 3], [1, 20, 15], [1, 20, 30],
#
[5, 2, 3], [5, 2, 15], [5, 2, 30],
[5, 10, 3], [5, 10, 15], [5, 10, 30],
[5, 20, 3], [5, 20, 15], [5, 20, 30],
#
[10, 2, 3], [10, 2, 15], [10, 2, 30],
[10, 10, 3], [10, 10, 15], [10, 10, 30],
[10, 20, 3], [10, 20, 15], [10, 20, 30]
]
traverse([1, 2, 3], fn x -> %Algae.Maybe.Just{just: x} end)
#=> %Algae.Maybe.Just{just: [1, 2, 3]}
traverse(%Algae.Maybe.Just{just: 4}, fn x -> [x, x * 10] end)
#=> [
# %Algae.Maybe.Just{just: 4},
# %Algae.Maybe.Just{just: 40}
# ]
traverse([1, 2, 3], fn x ->
if is_even(x) do
%Algae.Maybe.Just{just: x}
else
%Algae.Maybe.Nothing{}
end
end)
#=> %Algae.Maybe.Nothing{}
"""
@spec traverse(Traversable.t(), Traversable.link()) :: Traversable.t()
def traverse(data, link)
end
properties do
def naturality(data) do
a = generate(data)
f = fn x -> [x] end
g = &Quark.id/1
a
|> Traversable.traverse(f)
|> g.()
|> equal?(Traversable.traverse(a, fn x -> x |> f.() |> g.() end))
end
def identity(data) do
a = generate(data)
a
|> Traversable.traverse(fn x -> {x} end)
|> equal?({a})
end
def composition(_data) do
# traverse (Compose . fmap g . f) = Compose . fmap (traverse g) . traverse f
# MAINTAINER COMMENT
# ==================
#
# I cannot get this working. After throwing 1.5 days at this *one* prop,
# I'm walking away for now. If someone has an implementation that works,
# it would be greatly appreciated.
#
# I've implemented %Endo{} and %Compose{}, but Compose's Foldable refused
# to work because there is no Foldable instance for functions (AFAIK).
#
# I believe that the Hakell version works because of the lazy
# recursive call to foldMap in `Foldable f, Foldable g => Compose (f g)`,
# which in Elixir is trying to get dispatched on the partial function.
#
# It's very possible that there's a simple solution that I'm just not seeing
# after staring at the problem for too long
#
# PRs gladly accepted here: https://github.com/expede/witchcraft/compare
#
# ~ Brooklyn Zelenka, @expede
true
end
end
@doc """
`traverse/2` with arguments reversed.
## Examples
iex> fn x -> {x, x * 2, x * 10} end |> through([1, 2, 3])
{6, 12, [10, 20, 30]}
iex> fn x -> [x] end |> through({1, 2, 3})
[{1, 2, 3}]
iex> fn x -> [x, x * 5, x * 10] end |> through({1, 2, 3})
[
{1, 2, 3},
{1, 2, 15},
{1, 2, 30}
]
iex> fn x -> [x, x * 5, x * 10] end |> through([1, 2, 3])
[
#
[1, 2, 3], [1, 2, 15], [1, 2, 30],
[1, 10, 3], [1, 10, 15], [1, 10, 30],
[1, 20, 3], [1, 20, 15], [1, 20, 30],
#
[5, 2, 3], [5, 2, 15], [5, 2, 30],
[5, 10, 3], [5, 10, 15], [5, 10, 30],
[5, 20, 3], [5, 20, 15], [5, 20, 30],
#
[10, 2, 3], [10, 2, 15], [10, 2, 30],
[10, 10, 3], [10, 10, 15], [10, 10, 30],
[10, 20, 3], [10, 20, 15], [10, 20, 30]
]
"""
@spec through(Traversable.link(), Traversable.t()) :: Traversable.t()
def through(link, traversable), do: traverse(traversable, link)
@doc """
Run each action/effect in sequence (from left to right),
and accumulate values along the way.
## Examples
iex> sequence([{1, 2, 3}, {4, 5, 6}])
{5, 7, [3, 6]}
iex> [
...> [1, 2, 3],
...> [4, 5, 6]
...> ]
...> |> sequence()
[
[1, 4],
[1, 5],
[1, 6],
[2, 4],
[2, 5],
[2, 6],
[3, 4],
[3, 5],
[3, 6]
]
"""
@spec sequence(Traversable.t()) :: Foldable.t()
def sequence(traversable), do: traverse(traversable, &Quark.id/1)
end
definst Witchcraft.Traversable, for: Tuple do
use Witchcraft.Functor
def traverse(tuple, link) do
last_index = tuple_size(tuple) - 1
tuple
|> elem(last_index)
|> link.()
~> fn last -> put_elem(tuple, last_index, last) end
end
end
definst Witchcraft.Traversable, for: List do
use Witchcraft.Applicative
use Witchcraft.Foldable
def traverse([], link), do: of(link.([]), [])
def traverse(list = [head | _], link) do
right_fold(list, of(link.(head), []), fn x, acc ->
# credo:disable-for-lines:2 Credo.Check.Refactor.PipeChainStart
fn link_head, link_tail -> [link_head | link_tail] end
|> over(link.(x), acc)
end)
end
end