Categorygithub.com/rjNemo/underscore
modulepackage
0.6.1
Repository: https://github.com/rjnemo/underscore.git
Documentation: pkg.go.dev

# README

_Underscore

License Go version Go report test coverage

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 Go 1.18.

Usage

šŸ“š Follow this link for the documentation.

Install the library using

go get github.com/rjNemo/[email protected]

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
	evens := u.Filter(numbers, func(n int) bool { return n%2 == 0 })
	// square every number in the slice
	squares := u.Map(evens, func(n int) int { return n * n })
	// reduce to the sum
	res := u.Reduce(squares, func(n, acc int) int { return n + acc }, 0)

	fmt.Println(res) // 120
}

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, navigate to the official download page and install version 1.18 or beyond.

Installing

First clone the repository

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

Install dependencies

go mod download

And that's it.

Tests

To run the unit tests, you can simply run:

make test

Functions

underscore provides many of functions that support your favorite functional helpers

Collections

  • All
  • Any
  • Contains (only numerics values at the moment)
  • Each
  • Filter
  • Flatmap
  • Find
  • Map
  • Max
  • Min
  • Partition
  • Reduce

Pipe

Calling NewPipe will cause all future method calls to return wrapped values. When you've finished the computation, call Value to retrieve the final value.

Methods not returning a slice such as Reduce, All, Any, will break the Chain and return Value instantly.

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.

# Packages

No description provided by the author
No description provided by the author

# Functions

All returns true if all the values in the slice pass the predicate truth test.
Any returns true if any of the values in the slice pass the predicate truth test.
Contains returns true if the value is present in the slice.
Count returns the number of elements in the slice that satisfy the predicate.
Difference Returns a copy of the array with all instances of the values that are not present in the other array.
Drop returns the rest of the elements in a slice.
Each iterates over a slice of elements, yielding each in turn to an action function.
Filter looks through each value in the slice, returning a slice of all the values that pass a truth test (predicate).
Find looks through each value in the slice, returning the first one that passes a truth test (predicate), or the default value for the type and an error if no value passes the test.
Flatmap flatten the input slice element into the new slice.
GroupBy splits a slice into a map[K][]V grouped by the result of the iterator function.
Intersection computes the list of values that are the intersection of all the slices.
Joins two slices together and returns a Tuple of [T, []P], the selectors allow you to pick the keys you want to use from your struct's to join the sets together.
Joins two slices together and returns a []O where O is defined by the output of your projection function The selectors allow you to pick the keys from your structure to use as the join keys While the projection functions allows you to reformat joined datasets (Tuple of [T, []P]) into your own struct or type.
Last returns the last element of the slice.
Map produces a new slice of values by mapping each value in the slice through a transform function.
Max returns the maximum value in the slice.
Min returns the minimum value in the slice.
NewPipe starts a Pipe.
Orders a slice by a field value within a struct, the predicate allows you to pick the fields you want to orderBy.
Partition splits the slice into two slices: one whose elements all satisfy predicate and one whose elements all do not satisfy predicate.
Creates a sequence of numbers, i.e.
Reduce combine a list of values into a single value.
Sum adds elements of the slice.
Sums the values you select from your struct, basically a sort cut instead of having to perform a u.Map followed by a u.Sum.
No description provided by the author
No description provided by the author
No description provided by the author
Zips two slices togther so all the elements of left slice are attached to the corresponding elements of the right slice, i.e.

# Structs

Err is the Result that represents failure.
Ok is the Result that represents success.
No description provided by the author
No description provided by the author

# Interfaces

Result represent the outcome of an operation where failure is possible.