go-generics-example
Example code for Go generics.
Usage
$ go build -gcflags=-G=3
Requirements
Go 1.17 or later
Advertise
Go 言語にやってくる Generics は我々に何をもたらすのか
License
MIT
Author
Yasuhiro Matsumoto (a.k.a. mattn)
Example code for Go generics.
$ go build -gcflags=-G=3
Go 1.17 or later
Go 言語にやってくる Generics は我々に何をもたらすのか
MIT
Yasuhiro Matsumoto (a.k.a. mattn)
Problems caused by slice expansion.
When the first time function InsertAt is called, cased by slice expansion, l.l[0] isn't changed, v is assigned to the old slice not new.
$ ./shuffle
[5 9 3 6 4 10 1 8 7 2]
$ ./shuffle
[5 9 3 6 4 10 1 8 7 2]
$ ./shuffle
[5 9 3 6 4 10 1 8 7 2]
$ ./shuffle
[5 9 3 6 4 10 1 8 7 2]
$ ./shuffle
[2 9 8 7 3 4 1 5 10 6]
$ ./shuffle
[2 1 10 9 4 5 3 7 8 6]
$ ./shuffle
[8 4 6 1 3 10 5 9 2 7]
$ ./shuffle
[8 5 2 7 1 10 6 4 9 3]
I have tested rand.Shuffle
, it makes same results in every runtime when without setting seed, so if current behavior is intentional, please just close this PR 🙏 https://golang.org/pkg/math/rand/#Shuffle
Using docker container run --volume="$(pwd)/:/usr/src" --workdir='/usr/src/' -it 'golang:latest' '/bin/bash'
$ go version
go version go1.16.2 linux/amd64
$ cd add
$ go build -gcflags=-G=3
flag provided but not defined: -G
Using built Go from current master
$ /Users/kachick/repos/go/bin/go version
go version devel +e0fae78e1d Sat Mar 20 17:08:03 2021 +0000 darwin/amd64
$ cd add
$ /Users/kachick/repos/go/bin/go build -gcflags=-G=3
$ ls
add main.go
So can we use generics from 1.17? 🤔
I wanted to try my hand at Go generics and find myself using these function often to assert if every value or some values of a slice match a predicate.
Replaces type lists with type sets: https://github.com/golang/go/issues/45346
Type lists produce compiler errors with the latest gotip
: use generalized embedding syntax instead of a type list
Normal Order Evaluation is an evaluation strategy in which an expression is evaluated by repeatedly evaluating its leftmost outermost reducible expression. This means that a function's arguments are not evaluated before the function is applied.
In a word, left or right value should be evaluated when it is applied
So this code should be some kind of the following form:
package main
import (
"fmt"
)
func ternaryOp[T any](s bool, t func() T, f func() T) T {
if s {
return t()
}
return f()
}
func main() {
fmt.Println(ternaryOp(4 < 5, func() string {
fmt.Println("left evaluated")
return "less"
}, func() string {
fmt.Println("right evaluated")
return "greater"
}))
}
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
f f is a simple library that leverages the new generics in Golang to create a tools for functional style of code. Pipe like '|>' in Elixir or Elm. inp
generics Quick experiments with Go generics algebra, a generic square root function for float, complex and and rational. future, a concurrent cache ("
Unusual Generics Type parameters or Generics in Go designed to reduce boilerplate for container data types like lists, graphs, etc. and functions like
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
Go Library for Competitive Programming with Generics Go used to be a difficult language to use for competitive programming. However, with the introduc
Just few extended standard library functions for Golang using generics.
go-optional A library that provides Go Generics friendly "optional" features. Synopsis some := optional.Some[int](123) fmt.Printf("%v\n", some.IsSome(
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 What are generics? See Type Parameters Proposal. How to run the examples? As of today, gotip is the simplest way to run the
functools functools is a simple Go library that brings you your favourite functi
funcy Experimenting with golang generics to implement functional favorites like filter, map, && reduce. 2021-12 To run the tests, you need to install
fn fn is a collection of go functional operators with generics Getting Started P
This is a collection of various sorts implemnted both as []int only and as const
golang-generics-util Utility library that explores Go generics (1.18) xsync Sync
cache CDN-like, middleware memory cache for Go applications with integrated shielding and Go 1.18 Generics. Usage package main import ( "context" "
genfuncs import "github.com/nwillc/genfuncs" Package genfuncs implements various functions utilizing Go's Generics to help avoid writing boilerplate c
Go Generics the Hard Way This repository is a hands-on approach for getting started with Go generics: Prerequisites: how to install the prerequisites
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