Skip to content

Commit d9420bf

Browse files
committed
fixes #638
1 parent 29b72a6 commit d9420bf

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

xml/chapter3/section5/subsection1.xml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1384,6 +1384,39 @@ function stream_map_2(f, s1, s2) {
13841384
modifying your
13851385
<JAVASCRIPTINLINE>stream_map_2</JAVASCRIPTINLINE>
13861386
such that the result stream employs memoization.
1387+
<SOLUTION>
1388+
<SNIPPET HIDE="yes">
1389+
<NAME>stream_map_2_optimized_example</NAME>
1390+
<JAVASCRIPT>
1391+
const ints1 = integers_from(1);
1392+
const ints0 = integers_from(0);
1393+
const adds = (x, y) => x + y;
1394+
1395+
display(eval_stream(stream_map_2(adds, ints1, ints0), 5));
1396+
display(eval_stream(stream_map_2_optimized(adds, ints1, ints0), 5));
1397+
</JAVASCRIPT>
1398+
</SNIPPET>
1399+
<SNIPPET>
1400+
<NAME>stream_map_2_optimized</NAME>
1401+
<REQUIRES>memo</REQUIRES>
1402+
<EXAMPLE>stream_map_2_optimized_example</EXAMPLE>
1403+
<JAVASCRIPT>
1404+
function stream_map_2(f, s1, s2) {
1405+
return is_null(s1) || is_null(s2)
1406+
? null
1407+
: pair(f(head(s1), head(s2)),
1408+
() => stream_map_2(f, stream_tail(s1), stream_tail(s2)));
1409+
}
1410+
1411+
function stream_map_2_optimized(f, s1, s2) {
1412+
return is_null(s1) || is_null(s2)
1413+
? null
1414+
: pair(f(head(s1), head(s2)),
1415+
memo(() => stream_map_2(f, stream_tail(s1), stream_tail(s2))));
1416+
}
1417+
</JAVASCRIPT>
1418+
</SNIPPET>
1419+
</SOLUTION>
13871420
</EXERCISE>
13881421

13891422
<EXERCISE>

0 commit comments

Comments
 (0)