@@ -1384,6 +1384,39 @@ function stream_map_2(f, s1, s2) {
1384
1384
modifying your
1385
1385
<JAVASCRIPTINLINE >stream_map_2</JAVASCRIPTINLINE >
1386
1386
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 >
1387
1420
</EXERCISE >
1388
1421
1389
1422
<EXERCISE >
0 commit comments