Skip to content
/ gopy Public

makes Go (>=1.18) feels more like Python with generic functions

License

Notifications You must be signed in to change notification settings

sonlamho/gopy

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

39 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

gopy

Github Actions Go Reference

Features

  • Minimal, no bloat, pure functions (no side-effects), sensible function signatures (following Python mostly), no dependencies, pure Go!
  • Basic functional tools: Map, Filter, Reduce, Reversed
  • Reducing functions: Sum, Min, Max, All, Any
  • Variadic version of some functions: VarSum, VarMin, VarMax, VarAll, VarAny

Example Usage

Before using gopy in your project, get the package: go get github.com/sonlamho/gopy@latest

Sample code below:

package main

import (
	"fmt"
	"github.com/sonlamho/gopy"
)

var Print = fmt.Println

func main() {
	seq := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
	add_1half := func(x int) float64 { return float64(x) + 0.5 }
	is_even := func(x int) bool { return x%2 == 0 }

	Print(seq)
	Print(gopy.Map(add_1half, seq))
	Print(gopy.Filter(is_even, seq))
	Print(gopy.Sum(seq))
	Print("---")
	Print(gopy.Min(seq))  // -> 1
	Print(gopy.VarMin(100, 50, 9, 42))  // -> 9
	Print(gopy.VarMin(0.5, 0.7))  // -> 0.5
}

Output:

[1 2 3 4 5 6 7 8 9 10]
[1.5 2.5 3.5 4.5 5.5 6.5 7.5 8.5 9.5 10.5]
[2 4 6 8 10]
55
---
1
9
0.5

Future Plan:

  • Same operations Map, Filter, etc. but on iterables instead of slices.