Built-ins: Maps and more... ;)
This commit is contained in:
parent
a4f4fcba99
commit
d1bbd2e501
9 changed files with 663 additions and 36 deletions
60
examples/builtins_maps_and_more.fun
Executable file
60
examples/builtins_maps_and_more.fun
Executable file
|
|
@ -0,0 +1,60 @@
|
|||
#!/usr/bin/env fun
|
||||
|
||||
// Maps, map/filter/reduce, labeled break/continue (depth), and file I/O
|
||||
|
||||
// Map literal and indexing
|
||||
m = { "a": 1, "b": 2 }
|
||||
print(m) // -> {"a": 1, "b": 2} (order may vary)
|
||||
print(m["a"]) // -> 1
|
||||
m["c"] = 5
|
||||
print(has(m, "c")) // -> 1
|
||||
print(keys(m)) // -> ["a", "b", "c"] (order may vary)
|
||||
print(values(m)) // -> [1, 2, 5] (order may vary)
|
||||
|
||||
// map/filter/reduce
|
||||
nums = [1, 2, 3, 4]
|
||||
|
||||
fun double(x)
|
||||
return x + x
|
||||
|
||||
fun is_odd(x)
|
||||
return x % 2 == 1
|
||||
|
||||
fun sum2(a, b)
|
||||
return a + b
|
||||
|
||||
print(map(nums, double)) // -> [2, 4, 6, 8]
|
||||
print(filter(nums, is_odd)) // -> [1, 3]
|
||||
print(reduce(nums, 0, sum2)) // -> 10
|
||||
|
||||
// labeled break/continue by depth using nested loops
|
||||
for i in range(0, 3)
|
||||
for j in range(0, 5)
|
||||
if j == 1
|
||||
continue 1 // skip to next j
|
||||
if i == 1 && j == 2
|
||||
break 2 // break outer and inner
|
||||
print(i * 10 + j)
|
||||
|
||||
// file I/O (writes then reads)
|
||||
path = "tmp_io_example.txt"
|
||||
ok = write_file(path, "hello-io")
|
||||
print(ok) // -> 1
|
||||
content = read_file(path)
|
||||
print(content) // -> hello-io
|
||||
|
||||
/* Expected output:
|
||||
{"a": 1, "b": 2}
|
||||
1
|
||||
1
|
||||
["a", "b", "c"]
|
||||
[1, 2, 5]
|
||||
[2, 4, 6, 8]
|
||||
[1, 3]
|
||||
10
|
||||
0
|
||||
2
|
||||
3
|
||||
1
|
||||
hello-io
|
||||
*/
|
||||
Loading…
Add table
Add a link
Reference in a new issue