Extended library functions using generics in Go.

Overview

gostdx

Just few extended standard library functions for Golang using generics.

Prerequsites:

  • Go version 1.18 or greater due to generics.

lists

Import

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

fold (aka reduce)

Generic sequential fold/reduce example:

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

concurrent fold

Generic concurrent examples:

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 := lists.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 := lists.FoldC(F, acc, list, concurrency)
fmt.Println("map is:", m.m) // map is: map[k1:v1 k2:v2 k3:v3]
You might also like...
Code Generation for Functional Programming, Concurrency and Generics in Golang

goderive goderive derives mundane golang functions that you do not want to maintain and keeps them up to date. It does this by parsing your go code fo

Experiments with Go generics

generics Quick experiments with Go generics algebra, a generic square root function for float, complex and and rational. future, a concurrent cache ("

Example code for Go generics

go-generics-example Example code for Go generics. Usage $ go build -gcflags=-G=3 Requirements Go 1.17 or later Advertise Go 言語にやってくる Generics は我々に何をも

Collection of unusual generics usecases in Go

Unusual Generics Type parameters or Generics in Go designed to reduce boilerplate for container data types like lists, graphs, etc. and functions like

Package truthy provides truthy condition testing with Go generics
Package truthy provides truthy condition testing with Go generics

Truthy Truthy is a package which uses generics (Go 1.18+) to create useful boolean tests and helper functions. Examples // truthy.Value returns the tr

experimental promises in go1.18 with generics

async go a prototype of "promises" in go1.18. note: this is just an experiment used to test alternate patterns for dealing with asynchronous code in g

Go 1.18 generics use cases and examples

Go 1.18 generics use cases What are generics? See Type Parameters Proposal. How to run the examples? As of today, gotip is the simplest way to run the

Experimenting with golang generics to implement functional favorites like filter, map, && reduce.

funcy Experimenting with golang generics to implement functional favorites like filter, map, && reduce. 2021-12 To run the tests, you need to install

A collection of functional operators for golang with generics

fn fn is a collection of go functional operators with generics Getting Started P

Owner
null
Helper functions for common scenarios, using Go generics.

zeroflucs generics When writing Go code for Go 1.17 or below, we've all written more than our fair share of methods to check "does this slice contain

ZeroFlucs 2 Feb 18, 2022
Higher Order Functions using Golang Generics (Hack Days 2022)

hoff: Higher Order Functions (and Friends) Golang 1.18+ implementations of common methods/data structures using Go Generics Requirements Go 1.18 or ne

Shopify 22 Jan 4, 2023
Go Extended Lib

⚠️ Note: This is still an experiment. Motivation My main motivation was to try out generics in Go. I jumped straight into the usual suspects: Filter a

Guilherme J. Tramontina 6 Sep 4, 2022
Use is a go utility library using go1.18 generics

use use is a go utility library using go1.18 generics created by halpdesk 2022-01-22 use/slice Map updates a slice by applying a function to all membe

halpdesk 0 Jan 22, 2022
Functional tools in Go 1.18 using newly introduced generics

functools functools is a simple Go library that brings you your favourite functi

Rakeeb Hossain 173 Dec 5, 2022
Optional type using Go 1.18 generics.

go.eth-p.dev/goptional Generic Optional (or Go Optional, if you prefer) goptional is a package that provides an implementation of an Optional[T] monad

Ethan P. 0 Apr 2, 2022
Go Library for Competitive Programming with Generics

Go Library for Competitive Programming with Generics Go used to be a difficult language to use for competitive programming. However, with the introduc

Katsuyuki Tateishi 6 Dec 21, 2022
A library that provides Go Generics friendly "optional" features.

go-optional A library that provides Go Generics friendly "optional" features. Synopsis some := optional.Some[int](123) fmt.Printf("%v\n", some.IsSome(

moznion 52 Dec 20, 2022
Utility library that uses Go generics mechanism

golang-generics-util Utility library that explores Go generics (1.18) xsync Sync

Anton Tiurin 4 Dec 11, 2022
Calling functions by name and getting outputs by using reflect package.

Invoker A library to call (invoke) functions by taking names and sample inputs of those functions as parameters. And returns the types and values of o

null 3 Dec 20, 2021