basics

package
v0.0.0-...-cc6b36f Latest Latest
Warning

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

Go to latest
Published: Feb 11, 2025 License: BSD-3-Clause Imports: 3 Imported by: 0

Documentation

Overview

Package basics has a heap of useful functions inspired by the Elm basics module.

Index

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 Eq

func Eq[T any](x, y T) bool

Check if values are structurally the same.

Example
Eq("Hello", "Hello") // true

func Ge

func Ge[T Comparable[T]](x T, y T) bool

>=

Example
Ge(Int(2), Int(2)) // true

func Gt

func Gt[T Comparable[T]](x T, y T) bool

>

Example
Gt(Int(2), Int(3)) // false

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 Le

func Le[T Comparable[T]](x T, y T) bool

<=

Example
Le(Int(2), Int(2)) // true

func Lt

func Lt[T Comparable[T]](x T, y T) bool

<

Example
Lt(Int(2), Int(3)) // true

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

func Negate

func Negate[A Number](n A) A

Negate a number.

Example
Negate(Int(42)) // -42

func Not

func Not(pred bool) bool

Negate a boolean value.

Example
Not(true)  // false
Not(false) // true

func Sub

func Sub[T Number](a, b T) T

Subtract numbers like 4 - 3 == 1.

Example
Sub(Int(4), Int(3)) // 1

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 EQ

type EQ struct {
	// contains filtered or unexported fields
}

type Float

type Float float32

func Fdiv

func Fdiv(a Float, b Float) Float

Floating-point division:

Example
Fdiv(Float(10), Float(4)) // 2.5

func Sqrt

func Sqrt(n Float) Float

Take the square root of a number.

Example
Sqrt(Float(4))  // 2
Sqrt(Float(9))  // 3
Sqrt(Float(16)) // 4
Sqrt(Float(25)) // 5

func ToFloat

func ToFloat[T Int](x T) Float

ToFloat - Convert an integer into a float. Useful when mixing Int and Float values.

Example
ToFloat(1) // 1.0

func (Float) Cmp

func (i Float) Cmp(y Comparable[Float]) int

func (Float) T

func (i Float) T() Float

type GT

type GT struct {
	// contains filtered or unexported fields
}

type Int

type Int int

func Ceiling

func Ceiling(x Float) Int

Ceiling function, rounding up.

Example
Ceiling(1.0) // 1

func Floor

func Floor(x Float) Int

Floor function, rounding down.

Example
Floor(1.0) // 1

func ModBy

func ModBy(modulus Int, x Int) Int

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

func Round

func Round(x Float) Int

Round a number to the nearest integer.

Example
Round(1.0) // 1

func Truncate

func Truncate(x Float) Int

Truncate a number, rounding towards zero

Example
Truncate(1.0) // 1

func (Int) Cmp

func (i Int) Cmp(y Comparable[Int]) int

func (Int) T

func (i Int) T() Int

type LT

type LT struct {
	// contains filtered or unexported fields
}

type Number

type Number interface {
	Int | Float
}

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{}

Jump to

Keyboard shortcuts

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