Skip to content

Commit 9e92883

Browse files
committed
feat: update test_programs/f6/3.lox
Modify example to demonstrate function application N times
1 parent 8671026 commit 9e92883

File tree

3 files changed

+67
-56
lines changed

3 files changed

+67
-56
lines changed

internal/test_helpers/fixtures/pass_functions

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@ Debug = true
55
[stage-9] [test-1] Writing contents to ./test.lox:
66
[stage-9] [test-1] [test.lox] print clock() + 23;
77
[stage-9] [test-1] $ ./your_program.sh run test.lox
8-
[your_program] 1.735294236379E9
9-
[stage-9] [test-1] ✓ 1735294236.379000
8+
[your_program] 1.738748645714E9
9+
[stage-9] [test-1] ✓ 1738748645.714000
1010
[stage-9] [test-1] ✓ 1 line(s) match on stdout
1111
[stage-9] [test-1] ✓ Received exit code 0.
1212
[stage-9] [test-2] Running test case: 2
1313
[stage-9] [test-2] Writing contents to ./test.lox:
1414
[stage-9] [test-2] [test.lox] print clock() / 1000;
1515
[stage-9] [test-2] $ ./your_program.sh run test.lox
16-
[your_program] 1735294.213473
17-
[stage-9] [test-2] ✓ 1735294.213473
16+
[your_program] 1738748.622779
17+
[stage-9] [test-2] ✓ 1738748.622779
1818
[stage-9] [test-2] ✓ 1 line(s) match on stdout
1919
[stage-9] [test-2] ✓ Received exit code 0.
2020
[stage-9] [test-3] Running test case: 3
@@ -321,29 +321,33 @@ Debug = true
321321
[stage-4] [test-2] ✓ Received exit code 0.
322322
[stage-4] [test-3] Running test case: 3
323323
[stage-4] [test-3] Writing contents to ./test.lox:
324-
[stage-4] [test-3] [test.lox] // This program creates a function that returns another function
325-
[stage-4] [test-3] [test.lox] // and uses it to apply a function twice to a value
326-
[stage-4] [test-3] [test.lox] fun makeAdder(x) {
327-
[stage-4] [test-3] [test.lox] fun add(y) {
328-
[stage-4] [test-3] [test.lox] return x + y;
329-
[stage-4] [test-3] [test.lox] }
330-
[stage-4] [test-3] [test.lox] return add;
324+
[stage-4] [test-3] [test.lox] fun square(x) {
325+
[stage-4] [test-3] [test.lox] return x * x;
331326
[stage-4] [test-3] [test.lox] }
332327
[stage-4] [test-3] [test.lox]
333-
[stage-4] [test-3] [test.lox] fun applyTwice(f, x) {
334-
[stage-4] [test-3] [test.lox] return f(f(x));
328+
[stage-4] [test-3] [test.lox] // This higher-order function applies a
329+
[stage-4] [test-3] [test.lox] // function N times to a starting value x.
330+
[stage-4] [test-3] [test.lox] fun applyTimesN(N, f, x) {
331+
[stage-4] [test-3] [test.lox] var i = 0;
332+
[stage-4] [test-3] [test.lox] while (i < N) {
333+
[stage-4] [test-3] [test.lox] x = f(x);
334+
[stage-4] [test-3] [test.lox] i = i + 1;
335+
[stage-4] [test-3] [test.lox] }
336+
[stage-4] [test-3] [test.lox] return x;
335337
[stage-4] [test-3] [test.lox] }
336338
[stage-4] [test-3] [test.lox]
337-
[stage-4] [test-3] [test.lox] var addx = makeAdder(2);
338-
[stage-4] [test-3] [test.lox] var addy = makeAdder(2);
339-
[stage-4] [test-3] [test.lox]
340-
[stage-4] [test-3] [test.lox] print applyTwice(addx, 2);
341-
[stage-4] [test-3] [test.lox] print applyTwice(addy, 2);
339+
[stage-4] [test-3] [test.lox] // 2 is squared once
340+
[stage-4] [test-3] [test.lox] print applyTimesN(1, square, 2);
341+
[stage-4] [test-3] [test.lox] // 2 is squared twice
342+
[stage-4] [test-3] [test.lox] print applyTimesN(2, square, 2);
343+
[stage-4] [test-3] [test.lox] // 2 is squared thrice
344+
[stage-4] [test-3] [test.lox] print applyTimesN(3, square, 2);
342345
[stage-4] [test-3] [test.lox]
343346
[stage-4] [test-3] $ ./your_program.sh run test.lox
344-
[your_program] 6
345-
[your_program] 6
346-
[stage-4] [test-3] ✓ 2 line(s) match on stdout
347+
[your_program] 4
348+
[your_program] 16
349+
[your_program] 256
350+
[stage-4] [test-3] ✓ 3 line(s) match on stdout
347351
[stage-4] [test-3] ✓ Received exit code 0.
348352
[stage-4] [test-4] Running test case: 4
349353
[stage-4] [test-4] Writing contents to ./test.lox:

internal/test_helpers/fixtures/pass_functions_final

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@ Debug = true
55
[stage-9] [test-1] Writing contents to ./test.lox:
66
[stage-9] [test-1] [test.lox] print clock() + 23;
77
[stage-9] [test-1] $ ./your_program.sh run test.lox
8-
[your_program] 1.735294241101E9
9-
[stage-9] [test-1] ✓ 1735294241.101000
8+
[your_program] 1.738748649606E9
9+
[stage-9] [test-1] ✓ 1738748649.606000
1010
[stage-9] [test-1] ✓ 1 line(s) match on stdout
1111
[stage-9] [test-1] ✓ Received exit code 0.
1212
[stage-9] [test-2] Running test case: 2
1313
[stage-9] [test-2] Writing contents to ./test.lox:
1414
[stage-9] [test-2] [test.lox] print clock() / 1000;
1515
[stage-9] [test-2] $ ./your_program.sh run test.lox
16-
[your_program] 1735294.218204
17-
[stage-9] [test-2] ✓ 1735294.218204
16+
[your_program] 1738748.6266700001
17+
[stage-9] [test-2] ✓ 1738748.626670
1818
[stage-9] [test-2] ✓ 1 line(s) match on stdout
1919
[stage-9] [test-2] ✓ Received exit code 0.
2020
[stage-9] [test-3] Running test case: 3
@@ -321,29 +321,33 @@ Debug = true
321321
[stage-4] [test-2] ✓ Received exit code 0.
322322
[stage-4] [test-3] Running test case: 3
323323
[stage-4] [test-3] Writing contents to ./test.lox:
324-
[stage-4] [test-3] [test.lox] // This program creates a function that returns another function
325-
[stage-4] [test-3] [test.lox] // and uses it to apply a function twice to a value
326-
[stage-4] [test-3] [test.lox] fun makeAdder(x) {
327-
[stage-4] [test-3] [test.lox] fun add(y) {
328-
[stage-4] [test-3] [test.lox] return x + y;
329-
[stage-4] [test-3] [test.lox] }
330-
[stage-4] [test-3] [test.lox] return add;
324+
[stage-4] [test-3] [test.lox] fun square(x) {
325+
[stage-4] [test-3] [test.lox] return x * x;
331326
[stage-4] [test-3] [test.lox] }
332327
[stage-4] [test-3] [test.lox]
333-
[stage-4] [test-3] [test.lox] fun applyTwice(f, x) {
334-
[stage-4] [test-3] [test.lox] return f(f(x));
328+
[stage-4] [test-3] [test.lox] // This higher-order function applies a
329+
[stage-4] [test-3] [test.lox] // function N times to a starting value x.
330+
[stage-4] [test-3] [test.lox] fun applyTimesN(N, f, x) {
331+
[stage-4] [test-3] [test.lox] var i = 0;
332+
[stage-4] [test-3] [test.lox] while (i < N) {
333+
[stage-4] [test-3] [test.lox] x = f(x);
334+
[stage-4] [test-3] [test.lox] i = i + 1;
335+
[stage-4] [test-3] [test.lox] }
336+
[stage-4] [test-3] [test.lox] return x;
335337
[stage-4] [test-3] [test.lox] }
336338
[stage-4] [test-3] [test.lox]
337-
[stage-4] [test-3] [test.lox] var addx = makeAdder(2);
338-
[stage-4] [test-3] [test.lox] var addy = makeAdder(2);
339-
[stage-4] [test-3] [test.lox]
340-
[stage-4] [test-3] [test.lox] print applyTwice(addx, 2);
341-
[stage-4] [test-3] [test.lox] print applyTwice(addy, 2);
339+
[stage-4] [test-3] [test.lox] // 2 is squared once
340+
[stage-4] [test-3] [test.lox] print applyTimesN(1, square, 2);
341+
[stage-4] [test-3] [test.lox] // 2 is squared twice
342+
[stage-4] [test-3] [test.lox] print applyTimesN(2, square, 2);
343+
[stage-4] [test-3] [test.lox] // 2 is squared thrice
344+
[stage-4] [test-3] [test.lox] print applyTimesN(3, square, 2);
342345
[stage-4] [test-3] [test.lox]
343346
[stage-4] [test-3] $ ./your_program.sh run test.lox
344-
[your_program] 6
345-
[your_program] 6
346-
[stage-4] [test-3] ✓ 2 line(s) match on stdout
347+
[your_program] 4
348+
[your_program] 16
349+
[your_program] 256
350+
[stage-4] [test-3] ✓ 3 line(s) match on stdout
347351
[stage-4] [test-3] ✓ Received exit code 0.
348352
[stage-4] [test-4] Running test case: 4
349353
[stage-4] [test-4] Writing contents to ./test.lox:

test_programs/f6/3.lox

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,24 @@
11
---
22
expected_error_type: none
33
---
4-
// This program creates a function that returns another function
5-
// and uses it to apply a function twice to a value
6-
fun makeAdder(x) {
7-
fun add(y) {
8-
return x + y;
9-
}
10-
return add;
4+
fun square(x) {
5+
return x * x;
116
}
127

13-
fun applyTwice(f, x) {
14-
return f(f(x));
8+
// This higher-order function applies a
9+
// function N times to a starting value x.
10+
fun applyTimesN(N, f, x) {
11+
var i = 0;
12+
while (i < N) {
13+
x = f(x);
14+
i = i + 1;
15+
}
16+
return x;
1517
}
1618

17-
var addx = makeAdder(<<RANDOM_DIGIT>>);
18-
var addy = makeAdder(<<RANDOM_DIGIT>>);
19-
20-
print applyTwice(addx, <<RANDOM_DIGIT>>);
21-
print applyTwice(addy, <<RANDOM_DIGIT>>);
19+
// <<RANDOM_DIGIT>> is squared once
20+
print applyTimesN(1, square, <<RANDOM_DIGIT>>);
21+
// <<RANDOM_DIGIT>> is squared twice
22+
print applyTimesN(2, square, <<RANDOM_DIGIT>>);
23+
// <<RANDOM_DIGIT>> is squared thrice
24+
print applyTimesN(3, square, <<RANDOM_DIGIT>>);

0 commit comments

Comments
 (0)