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 ¶
- func All[T any](isOkay func(T) bool, l List[T]) bool
- func Any[T any](isOkay func(T) bool, ls List[T]) bool
- func Foldl[A, B any](f func(A, B) B, acc B, ls List[A]) B
- func Foldr[A, B any](fn func(A, B) B, acc B, ls List[A]) B
- func Head[T any](l List[T]) maybe.Maybe[T]
- func IsEmpty[T any](l List[T]) bool
- func Length[T any](ls List[T]) basics.Int
- func ListWith[T any, R any](l1 List[T], e func(List[T]) R, ab func(T, List[T]) R) R
- func Maximum[T basics.Comparable[T]](xs List[T]) maybe.Maybe[T]
- func Member[T any](val T, l List[T]) bool
- func Minimum[T basics.Comparable[T]](xs List[T]) maybe.Maybe[T]
- func Partition[A any](pred func(A) bool, list List[A]) Tuple2[List[A], List[A]]
- func Product[T basics.Number](xs List[T]) T
- func Sum[T basics.Number](xs List[T]) T
- func Tail[T any](l List[T]) maybe.Maybe[List[T]]
- func ToSlice[T any](xs List[T]) []T
- func ToSliceMap[A any, B any](f func(A) B, xs List[A]) []B
- func Unzip[A, B any](pairs List[Tuple2[A, B]]) Tuple2[List[A], List[B]]
- type List
- func Append[T any](xs List[T], ys List[T]) List[T]
- func Concat[T any](lists List[List[T]]) List[T]
- func ConcatMap[A, B any](f func(A) List[B], list List[A]) List[B]
- func Cons[T any](val T, l List[T]) List[T]
- func Drop[T any](n basics.Int, list List[T]) List[T]
- func Empty[T any]() List[T]
- func Filter[T any](isGood func(T) bool, list List[T]) List[T]
- func FilterMap[A, B any](f func(A) maybe.Maybe[B], xs List[A]) List[B]
- func FromSlice[T any](arr []T) List[T]
- func FromSliceMap[A any, B any](f func(A) B, arr []A) List[B]
- func IndexedMap[A, B any](f func(basics.Int, A) B, xs List[A]) List[B]
- func Intersperse[T any](sep T, xs List[T]) List[T]
- func Map[A, B any](f func(A) B, xs List[A]) List[B]
- func Map2[A any, B any, result any](f func(A, B) result, xs List[A], ys List[B]) List[result]
- 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[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[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], ...) List[result]
- func Range(low basics.Int, hi basics.Int) List[basics.Int]
- func Repeat[T any](n basics.Int, val T) List[T]
- func Reverse[T any](ls List[T]) List[T]
- func Singleton[T any](val T) List[T]
- func Sort[T basics.Comparable[T]](xs List[T]) List[T]
- func SortBy[A basics.Comparable[A]](f func(A) A, xs List[A]) List[A]
- func SortWith[A any](f func(a A, b A) basics.Order, xs List[A]) List[A]
- func Take[T any](n basics.Int, list List[T]) List[T]
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Foldl ¶
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 ¶
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 ¶
Extract the first element of a list.
Example ¶
Head(FromSlice([]basics.Int{3, 2, 1})) // Just 3
func Length ¶
Determine the length of a list.
Example ¶
Length(FromSlice([]basics.Int{1, 2, 3})) // 3
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 ¶
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 ¶
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 ¶
Get the product of the list elements.
Example ¶
Product(FromSlice([]basics.Int{2, 2, 2})) // 8
func Tail ¶
Extract the rest of the list.
Example ¶
Tail(FromSlice([]basics.Int{3, 2, 1})) // Just [2,1]
func ToSliceMap ¶
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 ¶
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 ¶
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 Drop ¶
Drop the first n members of a list.
Example ¶
Drop(2, FromSlice([]basics.Int{1, 2, 3, 4})) // Just [3,4]
func Filter ¶
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 ¶
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 FromSliceMap ¶
func IndexedMap ¶
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 ¶
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 ¶
Apply a function to every element of a list.
Example ¶
Map(basics.Sqrt, Singleton(basics.Float(4))) // [2]
func Map2 ¶
Combine two lists, combining them with the given function. If one list is longer, the extra elements are dropped.
func Range ¶
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 Singleton ¶
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.