elm

Handling records with shared substructure in Elm

六月ゝ 毕业季﹏ 提交于 2019-12-05 10:31:15
I have some record types structured essentially like this: type Body x = { x | pos: (Int,Int) } type Bubble = Body { radius: Int } type Box = Body { width: Int, height: Int } Now I would like to have a mixed list of any of these and perform some operation on the Body part, but still special case handling Box and Bubble other times. For example, having (implementations elided): mv: (Int,Int) -> Body a -> Body a bubble: Bubble box: Box I would like to map (mv (1,1)) [box,bubble] but this fails because Elm deems the types in the list incompatible. Now I could wrap the Box es and Bubble s in an

How do I get a list item by index in elm?

梦想与她 提交于 2019-12-05 09:12:52
问题 I got a list, and now I want the nth item. In Haskell I would use !! , but I can't find an elm variant of that. 回答1: There is no equivalent of this in Elm. You could of course implement it yourself. (Note: This is not a "total" function, so it creates an exception when the index is out of range). infixl 9 !! (!!) : [a] -> Int -> a xs !! n = head (drop n xs) A better way would be to define a total function, using the Maybe data type. infixl 9 !! (!!) : [a] -> Int -> Maybe a xs !! n = if | n <

Elm - update elements in a list

Deadly 提交于 2019-12-05 08:53:06
I just started programming in Elm and am stuck at something: I would like to have a method that can update fields of elements in a list at a certain index. My signature would look like this: updateElement : List (ID, Task) -> Int -> List (ID, Task) with: type alias Task = { description : String, focus : Bool} In this case I would like to set the boolean (focus) of the task at the index given to true and all the others tasks in the list to false. I already tried with arrays in Elm but then I have to work with Maybe and don't think that is a good solution. I suppose I will have to work with 'map

Decode a JSON tuple to Elm tuple

柔情痞子 提交于 2019-12-05 08:28:26
My JSON looks like the following { "resp": [ [1, "things"] , [2, "more things"] , [3, "even more things"] ] } the problem is that I can't parse the JSON tuples into Elm tuples: decodeThings : Decoder (List (Int, String)) decodeThings = field "resp" <| list <| map2 (,) int string It compiles, but when ran, it throws BadPayload "Expecting an Int at _.resp[2] but instead got [3, \"even more things\"] For some reason it reads [3, "even more things"] as only one thing and not as tuple in JSON format. How can I parse my JSON into a List (Int, String) ? Chad Gilbert You need a decoder which turns a

How to convert from String to Int in Json.Decoder

左心房为你撑大大i 提交于 2019-12-05 05:33:55
Here's my decoder: decodeData : Json.Decoder (Id, String) decodeData = Json.at ["data", "0"] <| Json.object2 (,) ("id" := Json.int) ("label" := Json.string) The id should logically be Int however my backend sends it as String (e.g. we get "1" instead of 1 ). How can I cast the decoded value to Int ? ... and to answer myself :) I found the solution in this Flickr example decodeData : Json.Decoder (Id, String) decodeData = let number = Json.oneOf [ Json.int, Json.customDecoder Json.string String.toInt ] in Json.at ["data", "0"] <| Json.object2 (,) ("id" := number) ("label" := Json.string) In Elm

Using elm higher order functions for keyboard events

对着背影说爱祢 提交于 2019-12-05 03:34:41
I am trying to create a higher order function to create functions to capture only a specific key code. The code is inspired on Evan's "onEnter" function from his todomvc implementation which captures only the enter function. onKeyCode : Int -> Msg -> Attribute Msg onKeyCode keycode msg = let captureKey code = if code == keycode then msg else NoOp in on "keydown" (Json.map captureKey keyCode) onEnter = onKeyCode 13 onEsc = onKeyCode 27 And now I want to add that to an input component in the viewer: input [ class "edit" , id ("todo-" ++ toString item.uid) , value item.message , onInput

Elm - Turn Msg into Cmd Msg

ぃ、小莉子 提交于 2019-12-05 00:17:48
I'm trying to modify a simple app from the elm-lang tutorial to first update the model, then trigger another update. update msg model = case msg of MorePlease -> (model, getRandomGif model.topic) NewGif (Ok newUrl) -> ( { model | gifUrl = newUrl }, Cmd.none) NewGif (Err _) -> (model, Cmd.none) -- my addition NewTopic newTopic -> ({ model | topic = newTopic}, MorePlease) This fails in the compiler because the NewTopic branch: The 3rd branch has this type: ( { gifUrl : String, topic : String }, Cmd Msg ) But the 4th is: ( { gifUrl : String, topic : String }, Msg ) So my Msg needs to be type Cmd

Elm: signal for two keyboard keys together?

三世轮回 提交于 2019-12-05 00:16:25
问题 How would one create a signal for alt + o or any other pair of keys in Elm? Is there a built-in way for doing this, or do I have to create something myself? I'm very new to Elm, so any additional explanation is always welcome. 回答1: I figured it out myself: Signal.map2 (&&) Keyboard.alt (Keyboard.isDown <| Char.toCode 'O') This creates a single Signal Bool that's true when both are down, otherwise false. 回答2: Yes there is a built-in way in elm to handle keyboard inputs The module is keyboard

What does a function with 2 values on the right side mean? (Model -> Html msg)

↘锁芯ラ 提交于 2019-12-04 17:37:02
问题 I have encountered that in the guide: viewValidation : Model -> Html msg viewValidation model = let (color, message) = if model.password == model.passwordAgain then ("green", "OK") else ("red", "Passwords do not match!") in div [ style [("color", color)] ] [ text message ] So this is a function, which takes the Model . Html msg usually looks to me like we are calling the function Html with the argument msg . msg doesn't seem to play any role in any other part of the viewValidation function,

Decode Json Array with objects in Elm

二次信任 提交于 2019-12-04 16:35:58
问题 I recently tried to get data from server with Elm's Http module and i'm stuck with decoding json to custom types in Elm. My JSON looks like that: [{ "id": 1, "name": "John", "address": { "city": "London", "street": "A Street", "id": 1 } }, { "id": 2, "name": "Bob", "address": { "city": "New York", "street": "Another Street", "id": 1 } }] Which should be decoded to: type alias Person = { id : Int, name: String, address: Address } type alias Address = { id: Int, city: String, street: String }