Code Monkey home page Code Monkey logo

functools's Introduction

functools

Go

functools is a simple Go library that brings you your favourite functional paradigms without sacrificing type-safety using interface{} or reflect

Made possible by Go 1.18 using the newly introduced generics.

Features

  • Any
  • All
  • Count
  • Filter
  • ForEach
  • Map
  • Reduce
  • ReduceRight
  • Sum
  • Chunk

Installation

go get -u github.com/rakeeb-hossain/functools

Usage

import (
    "github.com/rakeeb-hossain/functools"
    "fmt"
)

type User struct {
	username     string
	hasPortfolio bool
}

var users = []User{
		{"gopher", true},
		{"rakeeb", false},
		{"jack", true}}

func main() {
    // Count users with linked portfolios
    fmt.Printf("num users with linked portfolios: %d", 
        functools.Count(users, func(u User) bool { return u.hasPortfolio }))

    // Print usernames of users with linked portfolios
    functools.ForEach(
        functools.Filter(users, func(u User) bool { return u.hasPortfolio }),
        func(u User) { fmt.Printf("%s has a linked portfolio\n", u.username) })
}

Documentation

https://pkg.go.dev does not yet support Go 1.18 packages that use generics: golang/go#48264

For now, documentation is provided via comments and by running go doc -all from the package directory.

Contributing

Please see CONTRIBUTING

functools's People

Contributors

nchengyeeshen avatar qianxi0410 avatar rakeeb-hossain avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

functools's Issues

Filter: redundant memory allocation

You should not allocate new slice, it can be too expensive, just reuse old one

func Filter[T any](slice []T, predicate func(T) bool) []T {
	res := slice[:0]
	for _, v := range slice {
		if predicate(v) {
			res = append(res, v)
		}
	}
	return res
}

`Chunk` func

Chunk consumes a slice of a generic type and returns a slice composed of multiple chunks of a user-specified size

Vacuously, empty slices return empty regardless of the chunk size.

When the chunk size is zero or negative, return an empty slice

exp:

slice := []int{1, 2, 3, 4}
chunk := Chunk(slice, 2) // []int{{1,2}, {3, 4}}

chunk = Chunk(slice, 3) // []int{{1, 2, 3}, {4}}

chunk = Chunk(slice, 10) // []int{{1, 2, 3, 4}}

chunk = Chunk(slice, 0) // []int{}

ReduceRight

I think ReduceRight is a good addition for completeness sake.

Sometimes the order of evaluation for a reduce changes the result.

e.g.

strConcat := func(a, b string) string { return a + b }

slice := []string{"a", "b", "c"}

Reduce(slice, "", strConcat) // Output: "abc"

ReduceRight(slice, "", strConcat) // Output: "cba"

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.