Skip to content

Instantly share code, notes, and snippets.

@bouk
Created September 24, 2014 23:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bouk/9850cdb187cbbd192463 to your computer and use it in GitHub Desktop.
Save bouk/9850cdb187cbbd192463 to your computer and use it in GitHub Desktop.
type FilterFunction func(T) bool
type Function func(T) T
type Function2 func(T, T) T
func combine(a Function, b Function) Function {
return func(arg T) T {
return a(b(arg))
}
}
func Compose(first Function, f ...Function) Function {
for _, v := range f {
first = combine(first, v)
}
return first
}
func Map(f Function, input []T) []T {
result := make []T, len(input))
for i, element := range input {
result[i] = f(element)
}
return result
}
func Reduce(f Function2, input []T) (accum T) {
if len(input) == 0 {
return accum
}
accum = input[0]
for _, v := range input[1:] {
accum = f(accum, v)
}
return
}
func Filter(f FilterFunction, input []T) (result []T) {
for _, v := range input {
if f(v) {
result = append(result, v)
}
}
return
}
func TakeWhile(f FilterFunction, input []T) (result []T) {
for _, v := range input {
if !f(v) {
break
}
result = append(result, v)
}
return
}
func DropWhile(f FilterFunction, input []T) (result []T) {
take := false
for _, v := range input {
if take = take || f(v); take {
result = append(result, v)
}
}
return
}
func ZipWith(f Function2, inputa []T, inputb []T) (result []T) {
upto := len(inputa)
if len(inputb) < upto {
upto = len(inputb)
}
result = make []T, upto)
for i := 0; i < upto; i++ {
result[i] = f(inputa[i], inputb[i])
}
return
}
@gyscos
Copy link

gyscos commented Feb 22, 2015

line 76: missing a parenthesis?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment