f

f is a simple library that leverages the new generics in Golang to create a tools for functional style of code.

Pipe

like ‘|>’ in Elixir or Elm.

input := 1
add1 := func(i int) int { return i + 1 }
mul2 := func(i int) int { return i * 2 }
f.Pipe(input, add1, mul2) // 4

Collections

Map

f.Map(func(elem int) int { return elem * 2 }, []int{2, 3, 4, 5}) // [4, 6, 8, 10]

Reduce

f.Reduce(0, func(a, b int) int { return a + b }, []int{1, 2, 3, 4, 5, 6}) // 21

Filter

f.Filter(func(elem int) bool { return elem%2 == 0 }, []int{1, 2, 3, 4, 5, 6}) // [2, 4, 6]

Nth

f.Nth([]int{1, 2, 3, 4, 5}, 2) // 3

Take

f.Take([]int{1, 2, 3, 4, 5}, 2)) // [1, 2]

TakeLast

f.TakeLast([]int{1, 2, 3, 4, 5}, 2)) // [4, 5]

Remove

f.Remove([]int{3, 4, 5}, 0)) // [4, 5]

Head

f.Head([]int{3, 4, 5})) // 3

Tail

f.Tail([]int{3, 4, 5})) // [4, 5]

GitHub

View Github