Skip to content

Commit e6f1856

Browse files
committed
add Stream.resource/3
1 parent 1ace9a5 commit e6f1856

3 files changed

Lines changed: 129 additions & 5 deletions

File tree

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
\tikzset{
2+
start fun/.style={
3+
fun=\texttt{start\_fun},
4+
function arity=0,
5+
},
6+
}
7+
8+
\begin{scope}
9+
\def\smalllistitemsize{1.5cm}
10+
\tikzset{
11+
every list/.append style={
12+
element size=\smalllistitemsize,
13+
},
14+
}
15+
16+
\matrix[tuple=1, element size=\smalllistitemsize] {
17+
\node (t11) [placeholder=2]; & \node [separator]; &
18+
\node (acc1) {$acc_1$}; \\
19+
};
20+
21+
\matrix at (t11) [list=b1, element=a] {
22+
\node [index=1]; &
23+
\node [index=2]; \\
24+
};
25+
26+
\matrix[tuple=2, right=of tuple 1, element size=\smalllistitemsize] {
27+
\node (t21) [placeholder=3]; & \node [separator]; &
28+
\node (acc2) {$acc_2$}; \\
29+
};
30+
31+
\matrix at (t21) [list=b2, element=a] {
32+
\node [index=3]; &
33+
\node [index=4]; &
34+
\node [index=5]; \\
35+
};
36+
37+
\matrix[tuple=i, right=1 of tuple 2, element size=\smalllistitemsize] {
38+
\node (ti1) [placeholder=2]; & \node [separator]; &
39+
\node (acci) {$acc_i$}; \\
40+
};
41+
42+
\matrix at (ti1) [list=bi, element=a] {
43+
\node [index=n-1]; &
44+
\node [index=n]; \\
45+
};
46+
47+
\matrix [tuple=i+1, right=of tuple i] {
48+
\node {\texttt{:halt}}; & \node [separator]; &
49+
\node (acci+1) {$acc_{i+1}$}; \\
50+
};
51+
\end{scope}
52+
53+
\foreach \i in {1,2,i,i+1}{
54+
\draw [brace] (tuple \i.north west) -- (tuple \i.north east);
55+
\draw [<-] (last brace) -- +(0, .5)
56+
node (fun\i) [fun, anchor=out];
57+
}
58+
59+
\draw [<-] (fun1.in) -- +(0, .5)
60+
node (start fun) [start fun, anchor=out];
61+
62+
\draw [->] (acci+1.south) -- +(0, -.5)
63+
node (after fun) [fun=\texttt{after\_fun}, anchor=in];
64+
65+
\draw [->] (after fun.out) -- +(0, -.5)
66+
node [below] {\_};
67+
68+
\coordinate (top rule) at (start fun);
69+
\coordinate (bottom rule) at ($ (acc1.south) + (0, -1) $);
70+
71+
\draw [->]
72+
(acc1.south)
73+
-- (\currcoord |- bottom rule)
74+
-| ($ (tuple 1.east)!.5!(tuple 2.west) $)
75+
-- (\currcoord |- top rule)
76+
-| (fun2.in);
77+
78+
\draw [->, dotted]
79+
(acc2.south)
80+
-- (\currcoord |- bottom rule)
81+
-| ($ (tuple 2.east)!.5!(tuple i.west) $)
82+
-- (\currcoord |- top rule)
83+
-| (funi.in);
84+
85+
\draw [->]
86+
(acci.south)
87+
-- (\currcoord |- bottom rule)
88+
-| ($ (tuple i.east)!.5!(tuple i+1.west) $)
89+
-- (\currcoord |- top rule)
90+
-| (funi+1.in);
91+
92+
\matrix [list=a, below=2 of $ (list b1)!.5!(list bi) $] {
93+
\node [index=1]; &
94+
\node [index=2]; &
95+
\node [index=3]; &
96+
\node [index=4]; &
97+
\node [index=5]; &
98+
\node [elements between]; &
99+
\node [index=n-1]; &
100+
\node [index=n]; \\
101+
};
102+
103+
\bracetobrace
104+
{b12.south east}{b11.south west}
105+
{a1.north west}{a2.north east}
106+
107+
\bracetobrace
108+
{b25.south east}{b23.south west}
109+
{a3.north west}{a5.north east}
110+
111+
\bracetobrace
112+
{bin.south east}{bin-1.south west}
113+
{an-1.north west}{an.north east}
114+
Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
---
22
title: resource/3
33
url: /Stream/resource/3
4-
draft: true
54
---
65

7-
# `Stream.resource/3`
8-
Brief description and usage of the `Stream`'s `resource/3` function.
6+
Emits a sequence of values for a given resource.
7+
8+
The function begins by calling `start_fun` with no arguments.
9+
10+
The result of that call is passed to `fun`, which returns a tuple containing two elements: a list of data and an accumulator.
11+
12+
`fun` is then called repeatedly with the updated accumulator until it returns `{:halt, acc}`. The final accumulator is then passed to `after_fun`.
913

1014
{{< figure src="images/functions/Stream/resource-3.svg" >}}
11-
{{< figure src="images/functions/Stream/resource-3.2.svg" >}}
12-
{{< figure src="images/functions/Stream/resource-3.3.svg" >}}
15+
16+
A typical use case involves opening a file with `start_fun`, streaming its contents in some way with successive calls to `fun` (one per line), and then closing the file with `after_fun`.
17+

data/signatures.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1113,6 +1113,11 @@
11131113
defmodule Stream do
11141114
def repeatedly(fun)
11151115
end
1116+
- name: resource/3
1117+
signature: |
1118+
defmodule Stream do
1119+
def resource(start_fun, fun, after_fun)
1120+
end
11161121
- name: scan/2
11171122
signature: |
11181123
defmodule Stream do

0 commit comments

Comments
 (0)