Giter VIP home page Giter VIP logo

gostdx's Introduction

gostdx

Just few extended standard library functions for Golang using generics.

Prerequsites:

  • Go version 1.18 or greater for generics for maps and slices
  • Go version 1.23 or greater due to cons rangefunc (will work with 1.22 GOEXPERIMENT=rangefunc).

usage

slices

Expand for slices examples
import "github.com/unix1/gostdx/slices"

fold

Generic sequential fold:

list := []int{1, 2, 3, 4, 5}
sumFunc := func(elem, sum int) int { return sum + elem }
sum := slices.Fold(sumFunc, 0, list)
fmt.Println("sum is:", sum) // sum is 15

concurrent fold

Generic concurrent fold:

lock-free

acc := int64(0)
concurrency := 5
list := []int64{1, 2, 3, 4, 5}
sumFunc := func(elem int64, acc *int64) *int64 {
    atomic.AddInt64(acc, elem)
    return acc
}
sum := slices.FoldC(sumFunc, &acc, list, concurrency)
fmt.Println("sum is:", *sum) // sum is 15

with locks

Folds a list of tuples to a map

type tuple struct {
    v1 string
    v2 string
}

type mapAcc struct {
    sync.Mutex
    m map[string]string
}

acc = &mapAcc{m: map[string]string{}}
concurrency := 2
list := []tuple{{"k1", "v1"}, {"k2", "v2"}, {"k3", "v3"}}
F := func(e tuple, acc *mapAcc) *mapAcc {
    acc.Lock()
    defer acc.Unlock()
    acc.m[e.v1] = e.v2
    return acc
}
m := slices.FoldC(F, acc, list, concurrency)
fmt.Println("map is:", m.m) // map is: map[k1:v1 k2:v2 k3:v3]

maps

Expand for maps examples
import "github.com/unix1/gostdx/maps"

fold

Generic sequential fold:

m := map[int]int{1: 10, 2: 20, 3: 30}
sumFunc := func(k int, v int, acc int) int { return acc + k*v }
sum := maps.Fold(sumFunc, 0, m)
fmt.Println("sum of k*v is", sum) // sum of k*v is 140

concurrent fold

Generic concurrent fold:

acc := int64(0)
concurrency := 3
m := map[int64]int64{1: 10, 2: 20, 3: 30}
sumFunc := func(k int64, v int64, acc *int64) *int64 {
    atomic.AddInt64(acc, k*v)
    return acc
}
sum := maps.FoldC(sumFunc, &acc, m, concurrency)
fmt.Println("sum of k*v is", *sum) // sum of k*v is 140

cons

Expand for cons examples

Generic cons:

import . "github.com/unix1/gostdx/cons"

c := List(1, 2, 3)
fmt.Println("Car(c):", Car(c))                    // 1
fmt.Println("Car(Cdr(c))", Car(Cdr(c)))           // 2
fmt.Println("Car(Cdr(Cdr(c)))", Car(Cdr(Cdr(c)))) // 3
fmt.Println("Cdr(Cdr(Cdr(c)))", Cdr(Cdr(Cdr(c)))) // nil

Generic cons iteration via rangefunc:

import . "github.com/unix1/gostdx/cons"

c := List(1, 2, 3)
var s []int
for v := range Each(c) {
    s = append(s, v)
}
fmt.Println(s) // [1 2 3]

gostdx's People

Contributors

unix1 avatar

Stargazers

 avatar

Watchers

 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.