Documentation
¶
Overview ¶
Package basics has a heap of useful functions inspired by the Elm basics module.
Index ¶
- func Add[T Number](a, b T) T
- func Always[A, B any](a A, b B) A
- func ComposeL[A, B, C any](g func(B) C, f func(A) B) func(A) C
- func Eq[T any](x, y T) bool
- func Ge[T Comparable[T]](x T, y T) bool
- func Gt[T Comparable[T]](x T, y T) bool
- func Identity[A any](x A) A
- func Le[T Comparable[T]](x T, y T) bool
- func Lt[T Comparable[T]](x T, y T) bool
- func Max[T Comparable[T]](x T, y T) T
- func Min[T Comparable[T]](x T, y T) T
- func Mul[T Number](a, b T) T
- func Negate[A Number](n A) A
- func Not(pred bool) bool
- func Sub[T Number](a, b T) T
- type Appendable
- type Comparable
- type EQ
- type Float
- type GT
- type Int
- type LT
- type Number
- type Order
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Add ¶
func Add[T Number](a, b T) T
Add two numbers. The number type variable means this operation can be specialized to any Number type.
Example ¶
Add(Int(2), Int(3)) // 5
func Always ¶
func Always[A, B any](a A, b B) A
Create a function that always returns the same value. Useful with functions like map
func ComposeL ¶
func ComposeL[A, B, C any](g func(B) C, f func(A) B) func(A) C
Function composition, passing results along to the left direction.
Example ¶
composed := ComposeL(func(i Int) Int { return i + 1 }, Identity)
composed(1) // 2
func Identity ¶
func Identity[A any](x A) A
Given a value, returns exactly the same value. This is called the identity function.
Example ¶
Identity(Float(4)) // 4
func Max ¶
func Max[T Comparable[T]](x T, y T) T
Find the larger of two comparables.
Example ¶
Max(Int(42), Int(12345678)) // 12345678
func Min ¶
func Min[T Comparable[T]](x T, y T) T
Find the smaller of two comparables.
Example ¶
Min(Int(42), Int(12345678)) // 42
func Mul ¶
func Mul[T Number](a, b T) T
Multiply numbers like `2 * 3 == 6`.
Example ¶
Mul(Int(2), Int(3)) // 6
Types ¶
type Appendable ¶
type Appendable[T any] interface { App(Appendable[T]) Appendable[T] T() T }
func Append ¶
func Append[T any](a Appendable[T], b Appendable[T]) Appendable[T]
Put two appendable things together. This includes strings and lists.
type Comparable ¶
type Comparable[T any] interface { Cmp(Comparable[T]) int T() T }
type Float ¶
type Float float32
func Sqrt ¶
Take the square root of a number.
Example ¶
Sqrt(Float(4)) // 2 Sqrt(Float(9)) // 3 Sqrt(Float(16)) // 4 Sqrt(Float(25)) // 5
type Int ¶
type Int int
func ModBy ¶
Perform modular arithmetic. A common trick is to use (n mod 2) to detect even and odd numbers:
Example ¶
ModBy(Int(2), Int(2)) // 0
type Order ¶
type Order interface {
// contains filtered or unexported methods
}
Represents the relative ordering of two things. The relations are less than, equal to, and greater than.
func Compare ¶
func Compare[T Comparable[T]](x T, y T) Order
Compare any two comparable values. Comparable values include String, Char, Int, Float, or a list or tuple containing comparable values. These are also the only values that work as Dict keys or Set members.
Example ¶
Compare(Int(3), Int(4)) // Order => LT{}