Repository navigation
Expand file tree
/
Copy path24_first_class_funcs.rc
More file actions
76 lines (60 loc) · 1.56 KB
/
Copy path24_first_class_funcs.rc
File metadata and controls
76 lines (60 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File Version: 1.1.0
# /examples/24_first_class_funcs.rc
# Execution: ./target/release/rustcript examples/24_first_class_funcs.rc
print '--- 24 FIRST-CLASS FUNCTIONS ---'
# 1. Basic Assignment
print '1. Assigning Function to Variable...'
function add a b [
res a + b
return res
]
function subtract a b [
res a - b
return res
]
# Assign 'add' function to 'math_op' variable
math_op = add
print ' math_op is now: {math_op}'
# Call via variable
result = math_op(10, 5)
print ' math_op(10, 5) = {result}'
# Reassign
math_op = subtract
print ' math_op is now: {math_op}'
result = math_op(10, 5)
print ' math_op(10, 5) = {result}'
# 2. Passing Functions as Arguments
print ' '
print '2. Higher-Order Functions...'
function process_numbers list op_func [
print ' Processing list with {op_func}...'
new_list = {}
foreach n in list [
# Call the passed function
val = op_func(n, 2)
method new_list.push(val)
]
return new_list
]
data = {1, 2, 3, 4}
print ' Data: {data}'
# Pass 'add' (adds 2 to everything)
res1 = process_numbers(data, add)
print ' Result (Add 2): {res1}'
# Pass 'subtract' (subtracts 2 from everything)
res2 = process_numbers(data, subtract)
print ' Result (Sub 2): {res2}'
# 3. Functions in Data Structures
print ' '
print '3. Functions in Arrays/Maps...'
# In Vector
funcs = {add, subtract}
op = funcs[0]
res = op(100, 100)
print ' funcs[0](100, 100) = {res}'
# In Map
api = {'sum': add, 'diff': subtract}
op = api.diff
res = op(50, 20)
print ' api.diff(50, 20) = {res}'
print '--- END ---'