Skip to content

rjNemo/underscore

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Underscore

License Go version

underscore

underscore is a Go library providing useful functional programming helpers without extending any built-in objects.

It is mostly a port from the underscore.js library based on generics brought by go1.18.

Usage

Please check out the examples to see how to use the library.

package main

import (
	"fmt"
	u "github.com/rjNemo/underscore"
)

func main() {
	numbers := []int{1, 2, 3, 4, 5, 6, 7, 8, 9}
	// filter even numbers from the slice
	isEven := func(n int) bool { return n%2 == 0 }
	evens := u.Filter(numbers, isEven)
	// square every number in the slice
	toSquare := func(n int) int { return n * n }
	squares := u.Map(evens, toSquare)
	// reduce to the sum 
	sum := func(n, acc int) int { return n + acc }
	res := u.Reduce(squares, sum, 0)

	fmt.Println(res) // 110
}

Getting Started

These instructions will get you a copy of the project up and running on your local machine for development and testing purposes.

Prerequisites

You need at least go1.18 for development. The project is shipped with a Dockerfile based on go1.18. If you prefer local development, at the moment the easiest way to do it:

go install golang.org/dl/go1.18beta1@latest
go1.18beta1 download

Installing

First clone the repository

git clone https://github.com/rjNemo/underscore.git

Install dependencies

go mod download

And that's it.

Tests

Building the docker image will run the tests automatically. Otherwise, you can simply run:

go test ./...

Functions

underscore provides 100s of functions that support your favorite functional helpers

Collections

  • map
  • filter
  • reduce
  • each
  • some
  • every
  • find
  • contains (only numerics values at the moment)
  • max
  • min
  • partition

Built With

  • Go - Build fast, reliable, and efficient software at scale

Contributing

Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests to us.

Versioning

We use SemVer for versioning. For the versions available, see the tags on this repository.

Authors

  • Ruidy - Initial work - Ruidy

See also the list of contributors who participated in this project.

License

This project is licensed under the MIT License - see the LICENSE.md file for details

Acknowledgments

This project is largely inspired by Underscore.js library. Check out the original project if you don't already know it.