Learning Functional Programming in Go
上QQ阅读APP看书,第一时间看更新

Processing Gleam collections

Let's see how Gleam processes collections. The input we'll use is a collection of lines that comprises words in the /etc/paths file:

$ cat /etc/paths
/usr/local/bin
/usr/bin
/bin
/usr/sbin
/sbin

Gleam reads the file content as lines and feeds each line into the flow. From this, it creates the stream through which the functions Map and Reduce are called to count the number of occurrences of each word:

package main
import (
"os"
"github.com/chrislusf/gleam/flow"
)
func main() {
flow.New().TextFile("/etc/paths").Partition(2).FlatMap(`
function(line)
return line:gmatch("%w+")
end
`).Map(`
function(word)
return word, 1
end
`).ReduceBy(`
function(x, y)
return x + y
end
`).Fprintf(os.Stdout, "%s,%dn").Run()
}

Here's the output of this:

bin,3
local,1
sbin,2
usr,3

Recent update:

The LuaJit+Go approach has been ditched because of complexity to add new features to Gleam. Gleam is now using pure Go everywhere. Definitely check it out!