Created
August 28, 2015 00:21
-
-
Save TheSeamau5/f648e5d6d501e5445f2e to your computer and use it in GitHub Desktop.
This file contains hidden or 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 Signal exposing (Address) | |
import Html exposing (Html, div, ul, li) | |
type alias Component action state effect view | |
= state | |
-> (action -> (state, List effect), view) | |
type alias HtmlComponent action state effect | |
= Component action state effect (Address action -> Html) | |
type alias ListComponent action state effect | |
= List (HtmlComponent action state effect) | |
-> HtmlComponent (action, Int) (List state) (effect, Int) | |
list : ListComponent action state effect | |
list components states = | |
let | |
update (action, index) = | |
case (nth index components, nth index states) of | |
(Just component, Just state) -> | |
let | |
updateChild action state = | |
fst (component state) action | |
(nextState, effects) = | |
updateChild action state | |
nextStates = | |
setN index nextState states | |
in | |
(nextStates, List.map (\x -> (x, index)) effects) | |
_ -> | |
(states, []) | |
view address = | |
let | |
viewN index state = | |
case nth index components of | |
Just component -> | |
let | |
nthAddress = | |
Signal.forwardTo address (\x -> (x, index)) | |
viewChild = | |
snd (component state) | |
in | |
li | |
[] | |
[ viewChild nthAddress ] | |
_ -> | |
div [] [] | |
in | |
ul | |
[] | |
( List.indexedMap viewN states ) | |
in | |
(update, view) | |
--- | |
nth : Int -> List a -> Maybe a | |
nth n list = | |
case list of | |
[] -> | |
Nothing | |
x :: xs -> | |
if n < 0 | |
then | |
Nothing | |
else if n == 0 | |
then | |
Just x | |
else | |
nth (n - 1) xs | |
setN : Int -> a -> List a -> List a | |
setN n a = | |
updateN n (always a) | |
updateN : Int -> (a -> a) -> List a -> List a | |
updateN n f list = | |
List.indexedMap (\index x -> if n == index then f x else x) list | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment