function

package
v0.0.10 Latest Latest
Warning

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

Go to latest
Published: Apr 22, 2022 License: MIT Imports: 0 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Apply

func Apply[A, B any](ab func(A) B, a A) B
Example
package main

import (
	"fmt"

	"github.com/calebcase/base/data/function"
)

func main() {
	increment := func(x int) int {
		return x + 1
	}

	v := function.Apply(increment, 2)
	fmt.Printf("%v %T", v, v)

}
Output:

3 int

func Compose

func Compose[A, B, C any](bc func(B) C, ab func(A) B) func(A) C
Example
package main

import (
	"fmt"

	"github.com/calebcase/base/data/function"
)

func main() {
	i2s := func(x int) string {
		return fmt.Sprint(x)
	}
	f2i := func(y float32) int {
		return int(y)
	}

	v := function.Compose(i2s, f2i)(3.14)
	fmt.Printf("%v %T", v, v)

}
Output:

3 string

func Const

func Const[A, B any](a A, _ B) A
Example
package main

import (
	"fmt"

	"github.com/calebcase/base/data/function"
)

func main() {
	fmt.Println(function.Const(1, true))

}
Output:

1

func Flip

func Flip[A, B, C any](abc func(A, B) C) func(B, A) C
Example
package main

import (
	"fmt"

	"github.com/calebcase/base/data/function"
)

func main() {
	cat := func(a, b string) string {
		return a + "\n" + b
	}

	v := function.Flip(cat)("world", "hello")
	fmt.Printf("%v\n%T", v, v)

}
Output:

hello
world
string

func Id

func Id[A any](a A) A
Example
package main

import (
	"fmt"

	"github.com/calebcase/base/data/function"
)

func main() {
	fmt.Println(function.Id(3))
	fmt.Println(function.Id("hello"))

}
Output:

3
hello

func On

func On[A, B, C any](b func(B, B) C, u func(A) B) func(A, A) C
Example
package main

import (
	"fmt"

	"github.com/calebcase/base/data/function"
)

func main() {
	concat := func(a, b string) string {
		return a + b
	}

	i2s := func(x int) string {
		return fmt.Sprint(x)
	}

	v := function.On(concat, i2s)(4, 2)
	fmt.Printf("%v %T", v, v)

}
Output:

42 string

Types

This section is empty.

Jump to

Keyboard shortcuts

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