-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpure_functions.rb
More file actions
63 lines (47 loc) · 1.59 KB
/
pure_functions.rb
File metadata and controls
63 lines (47 loc) · 1.59 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
# There is two basic principles to call a function pure:
# 1. It must return the same value if it has the same arguments
# 2. The function evaluation must have zero side effects. No mutation.
# Translating we expect that f(x) = x, or in Ruby:
def f(x)
x
end
# I/O is inherently impure: input operations undermine referential transparency, and output operations create side effects.
# Nevertheless, there is a sense in which function can perform input or output and still be pure,
# if the sequence of operations on the I/O devices is modeled as an argument and a result,
# and I/O operations are taken to fail when the input sequence doesn't describe the operations taken since the program began execution.
# The second point ensures that the only sequence usable as an argument must change with each I/O action;
# the first allows different calls to an I/O function to return different results on account of the sequence arguments having changed.
# The I/O monad is a programming idiom typically used to perform I/O in pure functional languages.
# Pure function examples
def sum(a, b)
a + b
end
def max(a, b)
a > b ? a : b
end
def square(a)
a**a
end
# Impure function examples
@a = 1
def f
# Breaks rule 1. Same return value is not guaranteed.
@a
end
def f
# Breaks rule 1 and 2. Same return value is not guaranteed and it is mutating a variable.
@a += 1
end
def f
# Breaks rule 2. It is mutating a variable.
x = 0
x += 1
x
end
def f
# Breaks rule 2. It is mutating a string.
string = "Hello"
string << " world!"
end
# References:
# https://en.wikipedia.org/wiki/Pure_function