Skip to content

Commit a0ad266

Browse files
committed
Add Loops
1 parent 6f2b039 commit a0ad266

File tree

4 files changed

+56
-2
lines changed

4 files changed

+56
-2
lines changed

examples.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
Hello World,Values
1+
Hello World,Values,Loops

examples/loops.ink

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
intro := 'In Ink, there isn\'t a <code>for</code> loop construct. Instead, tail recursion is used.
2+
The standard library contains utility functions like <code>each</code>, <code>map</code>, <code>reduce</code>, <code>range</code>, and <code>filter</code>.'
3+
4+
rows := [
5+
{
6+
docs: ''
7+
code: 'std := load(\'../vendor/std\')
8+
log := std.log
9+
filter := std.filter
10+
range := std.range
11+
each := std.each
12+
13+
start := 1
14+
end := 9
15+
step := 1
16+
'
17+
},
18+
{
19+
docs: 'Like Python\'s <code>range</code>, we can create a composite value of the numbers 1 through 8 using <code>std.range</code>.'
20+
code: 'numbers := range(start, end, step)
21+
log(numbers)
22+
'
23+
},
24+
{
25+
docs: 'We can loop over these numbers with <code>std.each</code>.'
26+
code: 'each(numbers, num => log(num))
27+
'
28+
},
29+
{
30+
docs: 'Perhaps we only want the even numbers.'
31+
code: 'evenNumbers := filter(numbers, num => num % 2 :: {
32+
0 -> true
33+
_ -> false
34+
})
35+
each(evenNumbers, num => log(num))
36+
'
37+
},
38+
{
39+
docs: 'Or the sum if they were squared.'
40+
code: 'squares := map(numbers, num => num * num)
41+
sumOfSquares := reduce(squares,
42+
(acc, item) => acc + item, 0
43+
)
44+
log(sumOfSquares)
45+
'
46+
}
47+
]
48+
49+
end := ''

examples/values.ink

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,12 @@ log(~authed)
3434
code: 'exists := ()
3535
log(exists)
3636
'
37-
}
37+
},
38+
{
39+
docs: 'Ink does not have immutable constants. By convention constant variables start with a capital letter. Otherwise, camelCase is used.'
40+
code: 'Language := 'Ink'
41+
'
42+
},
3843
]
3944

4045
end := ''

static/favicon.ico

15 KB
Binary file not shown.

0 commit comments

Comments
 (0)