GoUI/Go/Util.elm

35 lines
912 B
Elm

module Go.Util exposing (..)
import Go.Types exposing (..)
import List exposing (map, head, filter, range)
import Tuple exposing (second, first)
-- Creates a pair of two elements.
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 second
<| head
<| filter ((\a -> a == val) << first) list
-- Computes all possible indices on a board of size n.
allIndices : Int -> List Index
allIndices n =
let
vals = range 0 (n - 1)
pairs = \xs i -> map (pair i) xs
in
List.concatMap (pairs vals) vals
-- Computes a zip of two lists.
zip : List a -> List b -> List (a, b)
zip xs ys = case (xs, ys) of
(x::xb, y::yb) -> (x, y) :: (zip xb yb)
_ -> []
-- Swaps the arguments of a two-argument function.
swap : (a -> b -> c) -> b -> a -> c
swap f vb va = f va vb