generics

command module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 4, 2022 License: MIT Imports: 4 Imported by: 0

README

generics

JavaScript style generics package.

For usage see main.go

Design choices

index vs pointers

Where JavaScript does this:

const things = [{id: 1},{id: 2}]
things.find(e => e.id === 2)

We would do this in Go, passing a pointer to Thing to avoid a potentially expensive copy:

things := []Thing{{id: 1}, {id: 2}}
slices.Find(things, func(e *Thing) bool { return e.id == 2 })

However, I think this may be confusing to JS developers if the array was a primitive:

arr := []int{1, 2}
slices.Find(arr, func(e *int) bool { return *e == 2 })

Therefore, in a lot of places the function receives the index to the array rather than a pointer.

Chainability

Chaining operations doesn't work well in Go unless we introduce a higher level concept like a Stream. I think we've all seen the Java Streams and that's just not as easy to use as a simple function.

You can avoid making a ton of variables by reusing where possible:

things := []int{1, 2, 3, 4}
things = slices.Filter(things, func(i int) bool { return things[i]%2 == 0 })
things = slices.Map(things, func(i int) int { return i + 1 })

Obviously if Map changes the type this won't work.

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL