Coder Social home page Coder Social logo

go_chainable's Introduction

go_chainable

NOTE: Requires go1.18

Go-Chainable is a library using generics to mimic the functional .map(x -> y).reduce(x -> y, z) patterns of javascript. List is also inspired by Java's ArrayList.

List

Construct a List

array := []string { "hello", "world" }
list := lists.New[string, any](array)

or get an empty list

list := lists.NewEmpty[string, any]()

First type parameter

First type parameter is the type of the List's values.

"Result" type parameter

The second type parameter for a list is needed only for when calling list.Reduce(). If you won't need to call .Reduce() then set the second type argument as any and forget it.

Map Over A List

import "github.com/neurocollective/go_chainable/lists"

array := []string { 1, 2 }
list := lists.New[int, any](array)
doubled := list.Map(func(val string, index int) int {
	return val * 2
})
fmt.Println(doubled.String()) // -> [2 4]

Reduce A List

import "github.com/neurocollective/go_chainable/lists"

array := []int { 1, 2 }
list := lists.New[int, int](array) // second type passed to New is the "result" type used by reducer as return type
added := list.Reduce(func(accumulator int, val int, index int) int {
	return accumulator + val
}, 0)
fmt.Println(added) // -> 3

Chain Operations Over A List

array := []int { 1, 2 }
list := New[int, int](array)
added := list.Map(func(val int, index int) int {
	return val + 1
}).Reduce(func(accumulator int, val int, index int) int {
	return accumulator + val
}, 0)
fmt.Println(added) // -> 5

Map (still unstable)

import "github.com/neurocollective/go_chainable/lists"

nativeMap := map[string]string {
	"hey": "dude",
	"sup": "brah",
}
theMap := maps.New[string, string, string](nativeMap)

mapped := theMap.Map(func(value string, key string) string {
	return key + "_" + value
})

fmt.Println(mapped.String()) // -> [hey_dude sup_brah]

Current Test Coverage

ok  	github.com/neurocollective/go_chainable/lists	0.001s	coverage: 80.4% of statements
ok  	github.com/neurocollective/go_chainable/maps	0.001s	coverage: 40.0% of statements

go_chainable's People

Contributors

neurocollective avatar

Watchers

James Cloos avatar  avatar

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.