/funk

Do arithmetic on functions. Treat slices like functions. Do what ever you want.

Primary LanguageGoMIT LicenseMIT

Funk Build Status Coverage Status

Do arithmetic on functions. Treat slices like functions. Do what ever you want.

Add functions together

add1 := func(x float64) float64 {
	return x + 1
}

add2 := func(x float64) float64 {
	return x + 2
}

cmb := funk.Add(mult, div)
cmd(1) == add1(1) + add2(1)
cmd(1) == 5

String functions together

c := a.Then(b)
c(x) == a(b(x))

Convert functions into descrite approxomations

f := func(x float64) float64 {
	// ... do something complicated to x
	return x
}

// Convert f into a curve that is has points between -100 and 100
// With a resolution of 1
c := funk.ToCurve(f, -100, 100, 1)

// Encode to json
buff, _ := json.Marshal(buff)

// Decode
newCurve := funk.Curve{}
json.Unmarshal(buff, &newCurve)

// Convert back to a Funk
newFunk := newCurve.ToFunk()

// Now the new function will approxomate the original
f(x) =~ newFunk(x)

Pipe functions together

This will run each function in it's own goroutine.

a := func(x float64) float64 {
	return x * 2
}

b := func(x float64) float64 {
	return x * 5
}

c := func(x float64) float64 {
	return x / 100
}

d := Pipe(a, b, c)

c(b(a(5))) == d(5)