From b68222dab7a766ede897ba9bdc68f262e2194e3d Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Sat, 26 May 2018 13:15:27 -0700 Subject: [PATCH] Prettify some code. --- Go/Game.elm | 4 +++- Go/Util.elm | 13 ++++++++++--- Go/View.elm | 5 +++-- 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/Go/Game.elm b/Go/Game.elm index 1dc65f0..5328cb7 100644 --- a/Go/Game.elm +++ b/Go/Game.elm @@ -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 diff --git a/Go/Util.elm b/Go/Util.elm index d9d9c81..44cccc2 100644 --- a/Go/Util.elm +++ b/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 diff --git a/Go/View.elm b/Go/View.elm index 2e75c1a..f10870d 100644 --- a/Go/View.elm +++ b/Go/View.elm @@ -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