Skip to content
/ fu Public

A collection of functional utilities written in Golang.

Notifications You must be signed in to change notification settings

samwho/fu

Repository files navigation

fu: functional utilities

If you miss your maps, reducers and filters from other languages, and don't mind manually unwrapping interface{}s, this library is for you.

Mapping

ctx := context.Background()
ns := []interface{}{1, 2, 3, 4, 5}
ms, err := fu.MapFn(ctx, ns, func(ctx context.Context, i interface{}) (interface{}, error){
  n, ok := i.(int)
  if !ok {
    return errors.New("invalid type")
  }
  return n + 1
})
if err != nil {
  return err
}
fmt.Printf("%v\n", ms) // []interface{}{2, 3, 4, 5, 6}

Or as a shorthand:

ctx := context.Background()
ns := []interface{}{1, 2, 3, 4, 5}
ms, err := fu.Map(ctx, ns, fu.Add(1))
if err != nil {
  return err
}
fmt.Printf("%v\n", ms) // []interface{}{2, 3, 4, 5, 6}

And if you want to do stuff in parallel:

ctx := context.Background()
ns := []interface{}{1, 2, 3, 4, 5}
ms, err := fu.ParallelMap(ctx, ns, fu.Add(1))
if err != nil {
  return err
}
fmt.Printf("%v\n", ms) // []interface{}{2, 3, 4, 5, 6}

About

A collection of functional utilities written in Golang.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages