elm

How to decode tagged union types in Elm?

一笑奈何 提交于 2019-12-23 08:54:15
问题 If I have a certain tagged union type, like Shape here, how would I construct a JSON decoder for it in Elm? type alias Rectangle = { width : Int, height : Int } type alias Circle = { radius: Int } type Shape = ShapeRectangle Rectangle | ShapeCircle Circle 回答1: Given your JSON looks like { "radius" : 10 } or { "width" : 20, "height" : 15} Then this will do the trick import Json.Decode as Json exposing ((:=)) decodeShape : Json.Decoder Shape decodeShape = Json.oneOf [ decodeShapeRectangle ,

Using elm-reactor with Elm embedded in HTML?

╄→гoц情女王★ 提交于 2019-12-23 08:35:30
问题 So I'm trying out Elm and WebRTC together. However for WebRTC I need some interop to javascript. So I created an index.html with the needed script includes, for both WebRTC and main.js. However, I am using elm-reactor. Which is super nice. But there is no main.js. I can create it with elm-make, but then I would have to manually update it. So, is there a way to have elm-reactor work together with embedded elm? Note: I'm using Elm 0.18, latest as of writing. 回答1: If you're willing to hack a bit

Elm - update elements in a list

若如初见. 提交于 2019-12-22 07:04:38
问题 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

Using elm higher order functions for keyboard events

♀尐吖头ヾ 提交于 2019-12-22 04:31:18
问题 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

Using elm higher order functions for keyboard events

无人久伴 提交于 2019-12-22 04:31:16
问题 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

Read in data from JSON file

帅比萌擦擦* 提交于 2019-12-21 05:04:15
问题 Say I have a JSON file located at http://www.randomurl.com/jobs.json, it looks like this: { "jobs": [ { "task" : "turn burgers" , "who" : "Anni" , "place" : "Quick"} , { "task" : "dishes" , "who" : "Bob" , "place" : "McDo"} ]} I've made a decoder: type alias Job = { task : String , who : String , place: String } type alias Jobs = List Job decoder : Decoder Job decoder = Decode.object3 Job (Decode.at ["attributes", "task"] Decode.string) (Decode.at ["attributes", "who"] Decode.string) (Decode

Elm “cannot find module”

余生颓废 提交于 2019-12-20 16:54:46
问题 I'm fairly new in Elm. It's interesting to see a functional language which allows you to develop front-end stuff. Now even if I am following the steps described here nicely, I still have problems with modules. The code is module Main where import Html exposing ( Html ) import Signal main : Signal Html.Html main = Html.text "This should work." |> Signal.constant I have used elm-reactor -a='localhost' to be able to view my output. But I am getting an error, that module 'HTML' cannot be found: I

elm-css: How to give a value to `opacity`

梦想的初衷 提交于 2019-12-20 02:48:49
问题 I am playing around with elm-css . Most of the things work as I expect them. But I am not able to give a correct value to the Css.opacity function. Here is what I have tried: Css.opacity 0.5 which gives the error: Function `opacity` is expecting the argument to be: Css.Number compatible But it is: Float The Css.Number is a type alias in the following form: type alias Number compatible = { compatible | value : String, number : Compatible } But I don't understand how to create a valid value for

Creating custom keyboard controls [Elm]

ⅰ亾dé卋堺 提交于 2019-12-19 09:03:13
问题 I'm trying to create custom keyboard controls for a 4 player game. Right now, the keys are predetermined like this: type Orient = { x:Int, y:Int } type GameInput = { space:Bool, delta:Time, so1:Orient, so2:Orient, so3:Orient, so4:Orient, amount:Int } gameInput : Signal GameInput gameInput = let sampledInput = sampleOn delta <| GameInput <~ Keyboard.space ~ delta ~ Keyboard.arrows ~ Keyboard.wasd ~ Keyboard.directions (toCode 'O') (toCode 'L') (toCode 'K') (toCode 'M') ~ Keyboard.directions

Concise way of updating a nested value inside a record in Elm (0.18)

牧云@^-^@ 提交于 2019-12-18 13:07:16
问题 I am looking for a concise way of updating a nested value inside a record in Elm (0.18). Given the following example: person = { name = "Steven", address = { country = "Spain", city = "Barcelona" } } I can update person.name to "Steve" using the following expression: { person | name = "Steve" } However, I am looking for a way to update a nested value. For instance, I would like to update person.address.city to "Madrid". I tried the following: { person | address.city = "Madrid" } { person |