list

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: 8 Imported by: 0

Documentation

Overview

You can create a `List` from any Go slice with the `FromSlice` function. This module has a bunch of functions to help you work with them!

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func All

func All[T any](isOkay func(T) bool, l List[T]) bool

Determine if all elements satisfy some test.

func Any

func Any[T any](isOkay func(T) bool, ls List[T]) bool

Determine if any elements satisfy some test.

func Foldl

func Foldl[A, B any](f func(A, B) B, acc B, ls List[A]) B

Reduce a list from the left.

Example
Foldl(basics.Add, 0, FromSlice([]basics.Int{1, 2, 3}))                         // 6
Foldl(Cons[basics.Int], Empty[basics.Int](), FromSlice([]basics.Int{1, 2, 3})) // [3,2,1]

func Foldr

func Foldr[A, B any](fn func(A, B) B, acc B, ls List[A]) B

Reduce a list from the right.

Example
Foldr(basics.Add, 0, FromSlice([]basics.Int{1, 2, 3}))                         // 6
Foldr(Cons[basics.Int], Empty[basics.Int](), FromSlice([]basics.Int{1, 2, 3})) // [1,2,3]
func Head[T any](l List[T]) maybe.Maybe[T]

Extract the first element of a list.

Example
Head(FromSlice([]basics.Int{3, 2, 1})) // Just 3

func IsEmpty

func IsEmpty[T any](l List[T]) bool

Determine if a list is empty.

func Length

func Length[T any](ls List[T]) basics.Int

Determine the length of a list.

Example
Length(FromSlice([]basics.Int{1, 2, 3})) // 3

func ListWith

func ListWith[T any, R any](l1 List[T], e func(List[T]) R, ab func(T, List[T]) R) R

func Maximum

func Maximum[T basics.Comparable[T]](xs List[T]) maybe.Maybe[T]

Find the maximum element in a non-empty list.

Example
Maximum(FromSlice([]basics.Int{basics.Int(1), basics.Int(2)})) // Just 2

func Member

func Member[T any](val T, l List[T]) bool

Figure out whether a list contains a value.

Example
Member(9, FromSlice([]basics.Int{1, 2, 3, 4})) // false
Member(4, FromSlice([]basics.Int{1, 2, 3, 4})) // true

func Minimum

func Minimum[T basics.Comparable[T]](xs List[T]) maybe.Maybe[T]

Find the minimum element in a non-empty list.

Example
Minimum(FromSlice([]basics.Int{basics.Int(1), basics.Int(2)})) // Just 1

func Partition

func Partition[A any](pred func(A) bool, list List[A]) Tuple2[List[A], List[A]]

Partition a list based on some test. The first list contains all values that satisfy the test, and the second list contains all the value that do not.

Example
Partition(func(i basics.Int) bool { return i < 3 }, FromSlice([]basics.Int{0, 1, 2, 3, 4, 5})) // ([0,1,2], [3,4,5])}

func Product

func Product[T basics.Number](xs List[T]) T

Get the product of the list elements.

Example
Product(FromSlice([]basics.Int{2, 2, 2})) // 8

func Sum

func Sum[T basics.Number](xs List[T]) T

Get the sum of the list elements.

Example
Sum(FromSlice([]basics.Int{1, 2, 3})) // 6

func Tail

func Tail[T any](l List[T]) maybe.Maybe[List[T]]

Extract the rest of the list.

Example
Tail(FromSlice([]basics.Int{3, 2, 1})) // Just [2,1]

func ToSlice

func ToSlice[T any](xs List[T]) []T

func ToSliceMap

func ToSliceMap[A any, B any](f func(A) B, xs List[A]) []B

func Unzip

func Unzip[A, B any](pairs List[Tuple2[A, B]]) Tuple2[List[A], List[B]]

Decompose a list of tuples into a tuple of lists.

Example
zipper := FromSlice([]tuple.Tuple2[basics.Int, bool]{tuple.Pair[basics.Int](0, true), tuple.Pair[basics.Int](17, false)})
Unzip(zipper) // ([0,17], [true,false])}

Types

type List

type List[T any] interface {
	Cons() *internal.Cons_[T, List[T]]
	Cmp(basics.Comparable[List[T]]) int
	T() List[T]
}

func Append

func Append[T any](xs List[T], ys List[T]) List[T]

Put two lists together.

Example
x1 := FromSlice([]basics.Int{1, 1, 2})
x2 := FromSlice([]basics.Int{3, 5, 8})
Append(x1, x2) // [1,1,2,3,5,8]

func Concat

func Concat[T any](lists List[List[T]]) List[T]

Concatenate a bunch of lists into a single list:

Example
x1 := FromSlice([]List[basics.Int]{FromSlice([]basics.Int{1, 2}), FromSlice([]basics.Int{3})})
Concat(x1) // [1,2,3]

func ConcatMap

func ConcatMap[A, B any](f func(A) List[B], list List[A]) List[B]

Map a given function onto a list and flatten the resulting lists.

func Cons

func Cons[T any](val T, l List[T]) List[T]

Add an element to the front of a list.

Example
Cons(1, Singleton(2)) // [1,2]

func Drop

func Drop[T any](n basics.Int, list List[T]) List[T]

Drop the first n members of a list.

Example
Drop(2, FromSlice([]basics.Int{1, 2, 3, 4})) // Just [3,4]

func Empty

func Empty[T any]() List[T]

Create a list with no elements

Example
Empty[int]() // []

func Filter

func Filter[T any](isGood func(T) bool, list List[T]) List[T]

Keep elements that satisfy the test.

Example
xs := Range(1, 6)
isEven := func(i basics.Int) bool { return basics.ModBy(2, i) == 0 }

Filter(isEven, xs) // [2,4,6]

func FilterMap

func FilterMap[A, B any](f func(A) maybe.Maybe[B], xs List[A]) List[B]

Filter out certain values. For example, maybe you have a bunch of strings from an untrusted source and you want to turn them into numbers:

func FromSlice

func FromSlice[T any](arr []T) List[T]

Create a List from a Go slice

func FromSliceMap

func FromSliceMap[A any, B any](f func(A) B, arr []A) List[B]

func IndexedMap

func IndexedMap[A, B any](f func(basics.Int, A) B, xs List[A]) List[B]

Same as map but the function is also applied to the index of each element (starting at zero).

Example
IndexedMap(tuple.Pair, FromSlice([]string{"Tom", "Sue", "Bob"})) // [(0, "Tom"),(1, "Sue"),(2, "Bob")]

func Intersperse

func Intersperse[T any](sep T, xs List[T]) List[T]

Places the given value between all members of the given list.

Example
Intersperse("on", FromSlice([]string{"turtles", "turtles", "turtles"})) // ["turtles","on","turtles","on","turtles]

func Map

func Map[A, B any](f func(A) B, xs List[A]) List[B]

Apply a function to every element of a list.

Example
Map(basics.Sqrt, Singleton(basics.Float(4))) // [2]

func Map2

func Map2[A any, B any, result any](f func(A, B) result, xs List[A], ys List[B]) List[result]

Combine two lists, combining them with the given function. If one list is longer, the extra elements are dropped.

func Map3

func Map3[A, B, C, result any](f func(A, B, C) result, xs List[A], ys List[B], zs List[C]) List[result]

func Map4

func Map4[A, B, C, D, result any](f func(A, B, C, D) result, xs List[A], ys List[B], zs List[C], ws List[D]) List[result]

func Map5

func Map5[A, B, C, D, E, result any](f func(A, B, C, D, E) result, vs List[A], ws List[B], xs List[C], ys List[D], zs List[E]) List[result]

func Range

func Range(low basics.Int, hi basics.Int) List[basics.Int]

Create a list of numbers, every element increasing by one. You give the lowest and highest number that should be in the list.

Example
Range(3, 6) // [3,4,5,6]

func Repeat

func Repeat[T any](n basics.Int, val T) List[T]

Create a list with *n* copies of a value.

Example
Repeat(2, "hi") // ["hi", "hi"]

func Reverse

func Reverse[T any](ls List[T]) List[T]

Reverse a list.

Example
Reverse(FromSlice([]basics.Int{1, 2, 3, 4})) // [4,3,2,1]

func Singleton

func Singleton[T any](val T) List[T]

Create a list with only one element.

Example
Singleton(1234) // [1234]
Singleton("hi") // ["hi"]

func Sort

func Sort[T basics.Comparable[T]](xs List[T]) List[T]

Sort values from lowest to highest.

Example
Sort(FromSlice([]basics.Int{basics.Int(3), basics.Int(2), basics.Int(1)})) // [1,2,3]

func SortBy

func SortBy[A basics.Comparable[A]](f func(A) A, xs List[A]) List[A]

Sort values by a derived property.

func SortWith

func SortWith[A any](f func(a A, b A) basics.Order, xs List[A]) List[A]

Sort values with a custom comparison function.

func Take

func Take[T any](n basics.Int, list List[T]) List[T]

Take the first n members of a list.

Example
Take(2, FromSlice([]basics.Int{1, 2, 3, 4})) // Just [1,2]

Jump to

Keyboard shortcuts

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