Skip to content

Instantly share code, notes, and snippets.

@stoft
Last active November 30, 2015 22:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stoft/b4eef971d1f6367c3909 to your computer and use it in GitHub Desktop.
Save stoft/b4eef971d1f6367c3909 to your computer and use it in GitHub Desktop.
Displays 10 boxes, one of them randomly switching off every cycle (200ms).
module DiscoBoxen where
import Array exposing (fromList, get)
import Color exposing (Color, black, white, green, red, blue, yellow, brown, purple, orange)
import Graphics.Element exposing (Element, spacer, color, flow, right, show)
import List exposing (foldr, (::))
import Random exposing (generate, initialSeed)
import Time exposing (every, millisecond, second)
drawSquare : Int -> Int -> Color -> Element
drawSquare x y c =
spacer x y |> color c
drawSquares : Int -> List Element
drawSquares r =
foldr
(\x acc -> (::)
(if r == x then
drawSquare 10 10 white
else
drawSquare 10 10 black)
acc)
[]
[1..10]
getColor n =
if n % 2 == 0 then
black
else
white
getRandomBoxNum n =
let doGenerate seed =
generate (Random.int 1 10) (initialSeed (round seed)) |> fst
in
Signal.map doGenerate (every (n * millisecond))
{-getRandomBoxNum seed =
generate (Random.int 1 10) (initialSeed (round seed)) |> fst
main =
Signal.map (flow right) (Signal.map drawSquares (Signal.map getRandomBoxNum (every (200 * millisecond))))
-}
main =
Signal.map (flow right) (Signal.map drawSquares (getRandomBoxNum 200))
@ccapndave
Copy link

Just a stylistic comment - since you set things up in the let for the expression in the in, I would personally do:

getRandomBoxNum n =
  let doGenerate seed =
    generate (Random.int 1 10) (initialSeed (round seed)) |> fst
  in 
    Signal.map doGenerate (every (n * millisecond))

@stoft
Copy link
Author

stoft commented Nov 30, 2015

@ccapndave ty, updated with your comment 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment