Prettify some code.
This commit is contained in:
parent
237630054b
commit
b68222dab7
|
@ -1,6 +1,8 @@
|
|||
module Go.Game exposing (verify)
|
||||
import Go.Types exposing (..)
|
||||
import Go.Util exposing (lookup)
|
||||
import List exposing (foldl)
|
||||
import Maybe exposing (andThen)
|
||||
|
||||
-- Make sure it's our turn to play.
|
||||
verifyTurn : Model -> Cell -> Maybe Cell
|
||||
|
@ -24,7 +26,7 @@ verifyBounds model ((x, y), c) = if x >= 0 && x < model.sessionSize && y >= 0 &&
|
|||
|
||||
-- Verify a cell placemenet using a list of verification functions.
|
||||
verifyAll : Cell -> List (Model -> Cell -> Maybe Cell) -> Model -> Maybe Cell
|
||||
verifyAll cell funcs model = List.foldl (\a b -> Maybe.andThen (a model) b) (Just cell) funcs
|
||||
verifyAll cell funcs model = List.foldl (\a b -> andThen (a model) b) (Just cell) funcs
|
||||
|
||||
-- Make sure that a move to the given cell can be made.
|
||||
verify : Cell -> Model -> Maybe Cell
|
||||
|
|
13
Go/Util.elm
13
Go/Util.elm
|
@ -1,27 +1,34 @@
|
|||
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 Tuple.second (List.head (List.filter (\(a, _) -> a == val) list))
|
||||
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 = List.range 0 (n - 1)
|
||||
pairs = \xs i -> List.map (\x -> (i, x)) xs
|
||||
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
|
||||
|
|
|
@ -4,6 +4,7 @@ import Go.Types exposing (..)
|
|||
import Html exposing (Html, div, text, p)
|
||||
import Html.Attributes exposing (class, classList)
|
||||
import Html.Events exposing (onClick)
|
||||
import List exposing (map)
|
||||
|
||||
renderIndex : (Index, Maybe Color) -> Html Msg
|
||||
renderIndex (index, color) =
|
||||
|
@ -21,6 +22,6 @@ renderIndex (index, color) =
|
|||
renderBoard : Int -> Board -> Html Msg
|
||||
renderBoard size board =
|
||||
let
|
||||
cells = List.map (\i -> (lookup i board) |> (pair i)) <| allIndices size
|
||||
cells = map (\i -> (i, lookup i board)) <| allIndices size
|
||||
in
|
||||
div [ class "board" ] <| List.map renderIndex cells
|
||||
div [ class "board" ] <| map renderIndex cells
|
||||
|
|
Loading…
Reference in New Issue
Block a user