Skip to content

Instantly share code, notes, and snippets.

@Apanatshka
Forked from deflexor/app.elm
Created October 14, 2015 07:33
Show Gist options
  • Save Apanatshka/a31da128352c968a2e23 to your computer and use it in GitHub Desktop.
Save Apanatshka/a31da128352c968a2e23 to your computer and use it in GitHub Desktop.
Elm-lang app that needs speed boost
import Html exposing (Html)
import Debug
import Array exposing (Array)
import Html.Attributes as HtmlA -- exposing (..)
import Html.Events as HtmlE --exposing (on, onClick, targetChecked)
import Signal exposing (Address)
import StartApp.Simple as StartApp
main =
StartApp.start { model = initialModel, view = view, update = update }
-- MODEL
type alias Model = Array (Array Bool)
w = 400
h = 400
initialModel : Model
initialModel = Array.repeat w <| Array.repeat h False
-- UPDATE
type Action
= Hi Int Int
update : Action -> Model -> Model
update action model =
case action of
Hi rowNo colNo ->
let
row =
Array.get rowNo model
|> Maybe.withDefault (Debug.crash "derp")
newval =
if Array.get colNo row == Just True
then False
else True
newrow = Array.set colNo newval row
in
Array.set rowNo newrow model
-- VIEW
view : Address Action -> Model -> Html
view address model =
Html.table
[ HtmlA.style [("border-collapse", "collapse")] ]
( model
|> Array.indexedMap (rowHtml address)
|> Array.toList
)
rowHtml : Address Action -> Int -> Array Bool -> Html
rowHtml address rowNo row =
Html.tr
[]
( row
|> Array.indexedMap (cellHtml address rowNo)
|> Array.toList
)
cellHtml : Address Action -> Int -> Int -> Bool -> Html
cellHtml address rowNo colNo cell =
let
bg =
if cell
then "blue"
else "white"
in
Html.td
[ HtmlE.onClick address (Hi rowNo colNo)
, HtmlA.style [("background", bg)] ]
[ Html.text "*" ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment