Skip to content

Instantly share code, notes, and snippets.

@ni-ko-o-kin ni-ko-o-kin/Main.elm
Last active Dec 8, 2019

Embed
What would you like to do?
elm-find-by-type-and-value
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
You can’t perform that action at this time.