Fungi provides a great suite of functional stream processing primitives that can be used for a wide range of purposes. Use this library to describe your intent declaratively and produce elegant code that is easy to read and refactor.
Please import our latest stable beta version:
go get github.com/getkalido/fungi@latestVery soon fungi will start popping up all over your codebase! And that is a
good thing. Here are some things it can help you with:
Filterout irrelevant items using a custom validation function.- Apply custom transformations to stream items (
Map). - Select a
Rangeof items you're interested in. Sortitems with a generic comparator.Pageitems efficiently based on page number and size.Loopthrough every item (see alsoForEach).- Collect items into a Go builtin
sliceormap(CollectSlice&CollectMap).
Fungi is very well-tested with a consistent test coverage of over 95%. See for yourself:
git clone git@github.com:getkalido/fungi.git
cd fungi
go test -cover[23th of August 2022 checked out at v1.0.0-beta-4]
PASS
coverage: 97.9% of statements
ok github.com/getkalido/fungi 0.286s
Moreover, our tests can and should be used as examples: they are written with clarity and readability in mind.
Test files have the
_test.gosuffix. Browse through, don't be shy!
Written with generics, fungi gives you the flexibility to apply it to any
iterator that implements the very simple fungi.Stream interface.
Suppose you already have multiple iterable types that fetch elements using a
method called Recv. Here's how you can write a converter function to make them
all comply with the fungi.Stream interface:
// Every one of your iterable receivers follows this generic interface.
type Receiver[T any] interface {
Recv() (T, error)
}
// receiverStream implements fungi.Stream interface.
type receiverStream[T any] struct {
Receiver[T]
}
// Next wraps Recv method of the origincal Receiver.
func (rs receiverStream[T]) Next() (T, error) {
return rs.Recv()
}
// ReceiverStream converts any Receiver into a fungi.Stream.
func ReceiverStream[T any](r Receiver[T]) fungi.Stream[T] {
return receiverStream[T]{r}
}Here's how your code is going to look soon:
func GetNamesOfMyDrinkingBuddies() ([]string, error) {
users := ReceiverStream[*User](GetMyFriends())
over18 := fungi.FilterMap(func(u *User) (name string, isLegal bool) {
return u.Name, u.Age >= 18
})
sortAlphabetically := fungi.Sort(func(a, b string) bool { return a < b })
return fungi.CollectSlice(sortAlphabetically(over18(users)))
}