GoUI/Go/Util.elm

28 lines
722 B
Elm
Raw Normal View History

module Go.Util exposing (..)
import Go.Types exposing (..)
pair : a -> b -> (a, b)
pair a1 a2 = (a1, a2)
-- Search for a value in a list of key-value pairs.
lookup : a -> List (a, b) -> Maybe b
lookup val list = Maybe.map Tuple.second (List.head (List.filter (\(a, _) -> a == val) list))
-- Computes all possible indices on a board of size n.
allIndices : Int -> List Index
allIndices n =
let
vals = List.range 0 (n - 1)
pairs = \xs i -> List.map (\x -> (i, x)) xs
in
List.concatMap (pairs vals) vals
zip : List a -> List b -> List (a, b)
zip xs ys = case (xs, ys) of
(x::xb, y::yb) -> (x, y) :: (zip xb yb)
_ -> []
swap : (a -> b -> c) -> b -> a -> c
swap f vb va = f va vb