Skip to content

Instantly share code, notes, and snippets.

@lukasjoc
Created September 4, 2021 13:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lukasjoc/8fd34905587eb3daf32a7e72b63fe24a to your computer and use it in GitHub Desktop.
Save lukasjoc/8fd34905587eb3daf32a7e72b63fe24a to your computer and use it in GitHub Desktop.
Try T type in go
package main
import (
"fmt"
)
// filter ...
func filter[T any](slice []T, filter func(i int, x T) bool) (o []T) {
for i, x := range slice {
if filter(i, x) {
o = append(o, x)
}
}
return o
}
// mapInto ...
func mapInto[T comparable](slice []T, mappingFunc func(i int, x T) map[T]T) (o []map[T]T) {
for i, x := range slice {
mappedX := mappingFunc(i, x)
o = append(o, mappedX)
}
return o
}
// mapIntoSlice ...
func mapIntoSlice[T any](slice []T, mapper func(i int, x T) T) (o []T) {
for i, x := range slice {
mappedX := mapper(i, x)
o = append(o, mappedX)
}
return o
}
// concat concatenates the first slice into the second slice
func concat[T any](from []T, into []T) []T {
for _, x := range from {
into = append(into, x)
}
return into
}
// reduce ...
// TODO: make it work on maps and nD slices
func reduce[T any](slice []T, reducer func(acc []T, curr []T, index int, orig []T) []T) (o []T) {
var x T
var i T
for i, x = range slice {
o = reducer(o, x, i, slice)
// o = append(o, reduced)
}
return o
}
func main() {
// Filtering
// Ints --
sameInts := []int{1, -1, 10, 1000}
// _ the index is really cool but just works for ints ;((
x := filter(sameInts, func(_, x int) bool { return x >= 0 })
fmt.Println("x >= 0", x)
i := filter(sameInts, func(i int, x int) bool { return i >= 1 })
fmt.Println("i >= 1", i)
ineg := filter(sameInts, func(_, x int) bool { return x < 0 })
fmt.Println("i < 0", ineg)
ix := filter(sameInts, func(i int, x int) bool { return i >= 1 && x%2 == 0 })
fmt.Println("i >= 1 && x%2 == 0", ix)
// Floats --
sameFloats := []float64{1.0, 2.0, 3.125, 3.141596}
fx := filter(sameFloats, func(i int, x float64) bool { return x > 1.0 && i == 0 })
fmt.Println("x >= 1.0", fx)
// Strings, Runes -
sameRunes := []rune{'a', 'e', 'i', 'o', 'z'}
notv := filter(sameRunes, func(i int, x rune) bool { return x == 'z' })
fmt.Println("x == 'z'", string(notv))
v := filter(sameRunes, func(i int, x rune) bool { return x == 'a' || x == 'e' || x == 'i' || x == 'o' || x == 'u' })
fmt.Println("x == 'a' || x == 'e' || x == 'i' || x == 'o' || x == 'u' ", string(v))
// Mapping
orig := []int{1, 1, 1}
// [1,1,1].map((i, x) => i+x) -> [1, 2, 3]
fmt.Println("mapIntoSlice => ", orig, mapIntoSlice(orig, func(i int, x int) int { return i + x }))
// [0, 1, 2].map((_, x) => ({[_]: _+x}))
fmt.Println("mapInto => ", orig, mapInto(orig, func(i int, x int) map[int]int { return map[int]int{i: i + x} }))
// Reducing
// var flattened = [[0, 1], [2, 3], [4, 5]].reduce((acc, curr) => acc.concat(curr), [])
nested := [][]int{[]int{0, 1}, []int{2, 3}, []int{4, 5}}
// n := []int{1, 2, 3}
// a := reduce(nested [][]int, func(acc []int, curr []int, i int, a[]int) []int { return concat(curr, acc)})
a := reduce(nested, func(acc []int, curr []int, i int, a [][]int) []int {
//acc = append(acc, curr)
acc = concat(acc, curr)
return acc
})
fmt.Println("reduce =>", a)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment