Last active
December 8, 2019 09:31
-
-
Save ni-ko-o-kin/95ae359bd0ac13a5ef9036fea1dfce2c to your computer and use it in GitHub Desktop.
elm-find-by-type-and-value
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
module Main exposing (main) | |
import Browser | |
import Html exposing (Html, div, text) | |
type alias Model = | |
{} | |
type Msg | |
= NoOp | |
type Component | |
= Name String | |
| Speed Float | |
update : Msg -> Model -> Model | |
update msg model = | |
model | |
find1 : Component -> List Component -> Maybe Component | |
find1 component list = | |
let | |
go cur acc = | |
case acc of | |
Just comp -> | |
Just comp | |
Nothing -> | |
case ( cur, component ) of | |
( Name curName, Name componentName ) -> | |
if curName == componentName then | |
Just (Name curName) | |
else | |
Nothing | |
( Speed curSpeed, Speed componentSpeed ) -> | |
if curSpeed == componentSpeed then | |
Just (Speed curSpeed) | |
else | |
Nothing | |
( _, _ ) -> | |
Nothing | |
in | |
List.foldl go Nothing list | |
find2 : Component -> List Component -> Maybe Component | |
find2 component list = | |
let | |
go cur = | |
case ( cur, component ) of | |
( Name curName, Name componentName ) -> | |
curName == componentName | |
( Speed curSpeed, Speed componentSpeed ) -> | |
curSpeed == componentSpeed | |
( _, _ ) -> | |
False | |
in | |
list | |
|> List.filter go | |
|> List.head | |
toString : Maybe Component -> String | |
toString comp = | |
case comp of | |
Nothing -> | |
"not found" | |
Just c -> | |
case c of | |
Name name -> | |
name | |
Speed speed -> | |
String.fromFloat speed | |
view : Model -> Html Msg | |
view model = | |
let | |
list = | |
[ Name "Foo", Speed 1.0, Name "Yoda" ] | |
in | |
list | |
|> find2 (Name "Yoda") | |
|> toString | |
|> text | |
main : Program () Model Msg | |
main = | |
Browser.sandbox | |
{ init = {} | |
, view = view | |
, update = update | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment