Created
February 17, 2016 16:06
-
-
Save pulcheri/e7a43e4ac8ad9630a44f to your computer and use it in GitHub Desktop.
Update of this code: http://codereview.stackexchange.com/questions/29823/can-this-be-written-more-concise-and-aligned-with-the-functional-reactive-progra
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Random | |
import Mouse | |
import Graphics.Collage exposing (toForm, move, collage) | |
import Graphics.Element exposing (show) | |
import List exposing (..) | |
import Time | |
type Fruit = Apple | Orange | Banana | Melon | Guava | |
-- For module global declarations, it is often helpful to | |
-- explicitly annotate the types. | |
fruits : List Fruit | |
fruits = [Apple, Orange, Banana, Melon, Guava] | |
-- especially for functions :) | |
fruit : Int -> Fruit | |
fruit n = Maybe.withDefault Apple <| head <| drop n fruits | |
type alias RandModel = (Int, Random.Seed) | |
rr2 : RandModel -> RandModel | |
rr2 m = Random.generate | |
(Random.int 0 (length fruits - 1)) | |
(snd m) | |
rand : Signal Int | |
rand = Signal.map fst <| Signal.foldp (always rr2) (0, Random.initialSeed 0) Mouse.clicks | |
fruitText : Signal Graphics.Collage.Form | |
fruitText = Signal.map (toForm << show << fruit ) rand | |
time = Signal.map | |
(Time.inSeconds << fst) | |
(Time.timestamp (Time.fps 40)) | |
scene dy form = Graphics.Collage.collage 300 300 [move (0, 100 * cos dy) form] | |
main = Signal.map2 scene time fruitText |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment