-
Notifications
You must be signed in to change notification settings - Fork 0
/
functionpipeline.tiny
50 lines (45 loc) · 1.26 KB
/
functionpipeline.tiny
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
########################################################################
#
# Test using the function pipe operator '|>' to
# find the most common words in a file, uppercase them and then
# display the word and its count
#
########################################################################
println("Processing with function composition:")
fileToProcess = 'Tiny Documentation.html'
time {
readtext(fileToProcess)
|> split r/\s+/
|> matchall r/^[a-z]+$/
|> tohash()
|> sortdescending{it.value}
|> first(10)
|> foreach {
word = it.Key
word = head(it.Key).ToUpper() + tail(word)
println( word.padleft(20) + " : " + it.Value )
}
}
#
# Do the same thing again with method chaining. This
# is not quite equvalent to the function chaining example
# because we need to turn other data structures into TinyLists
# so we can continue using methods.
#
println("Processing with method chaining:")
time {
readtext(fileToProcess)
.aslist()
.split(r/\s+/)
.matchall(r/^[a-z]+$/)
.tohash()
.getenumerator()
.aslist()
.sortdescending{it.value}
.take(10)
.foreach {
word = it.Key
word = head(word).ToUpper() + tail(word)
println( word.padleft(20) + " : " + it.Value )
}
}