elm

How to print index of selected option in Elm?

泪湿孤枕 提交于 2019-12-03 11:29:52
I have a <select> HTML element with 3 options and a <p> element. In the <p> element I want to print index of the currently selected item in <select> . E.g. if I select the first option, it should print 0, if I select the second option, it should print 1, and so on. How do I proceed from the minimal code, which is given below? import Html as H exposing (Html) import Maybe import Signal as S exposing (Address, (<~)) type alias Model = { selected : Maybe Int } model = { selected = Nothing } type Action = NoOp | Select Int update action model = case action of NoOp -> model Select n -> { model |

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

旧巷老猫 提交于 2019-12-03 11:13:24
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, though. So what does it mean and what is it for in this case? marcosh Html Msg is just a type parameter,

Understanding generic union types in Elm

房东的猫 提交于 2019-12-03 10:18:11
I'm having some trouble understanding what exactly the Html msg type is, or how it gets used. I found this line of code in VirtualDom.elm, which Html msg seems to be an alias of: type Node msg = Node This looks like a generic union type with one type parameter, msg , and one trivial case that holds no additional information. I'm wondering: How does the div function construct one of these objects? How does an object like this get used? How could an object like this get used? Is there any value for a user to define a type like this, or is Html msg just a magical type to support the Elm compiler

How can I get special characters using elm-html module?

白昼怎懂夜的黑 提交于 2019-12-03 09:58:58
问题 Disclaimer: I'm brand new to Elm I'm fiddling around with the online Elm editor and I've run into an issue. I can't find a way to get certain special characters (copyright, trademark, etc.) to show up. I tried: import Html exposing (text) main = text "©" All that showed up was the actual text © . I also tried to use the unicode character for it \u00A9 but that ended up giving me a syntax error: (line 1, column 16): unexpected "u" expecting space, "&" or escape code The only way I found was to

Array vs List in Elm

。_饼干妹妹 提交于 2019-12-03 09:57:31
I was suprised to learn that Array and List were two different types in Elm: Array List In my case, I have a List Int of length 2,000,000 and I need about 10,000 of them but I don't know in advance which ten thousand. That will be provided by another list. In pseudo-code: x = [ 1,1,0,30,...,255,0,1 ] y = [ 1,4,7,18,36,..., 1334823 , ... 1899876 ] z = [ y[x[0]], y[x[1]], ... ] I am using pseudocode because clearly this isn't Elm syntax (it might be legal JavaScript). Can these array selections be done in List or Array or both? List is a linked list which provides O(n) lookup time based on index

How to achieve behavior of setTimeout in Elm

大憨熊 提交于 2019-12-03 08:31:46
问题 I'm writing a web game in Elm with lot of time-dependent events and I'm looking for a way to schedule an event at a specific time delay. In JavaScript I used setTimeout(f, timeout) , which obviously worked very well, but - for various reasons - I want to avoid JavaScript code and use Elm alone. I'm aware that I can subscribe to Tick at specific interval and recieve clock ticks, but this is not what I want - my delays have no reasonable common denominator (for example, two of the delays are

Does 'foldp' violate FP's no mutable state principle?

狂风中的少年 提交于 2019-12-03 07:10:10
问题 I'm learning about Elm from Seven More Languages in Seven Weeks. The following example confuses me: import Keyboard main = lift asText (foldp (\dir presses -> presses + dir.x) 0 Keyboard.arrows) foldp is defined as: Signal.foldp : (a -> b -> b) -> b -> Signal a -> Signal b It appears to me that: the initial value of the accumulator presses is only 0 on the first evaluation of main after the first evaluation of main it seems that the initial value of presses is whatever the result of function

Using local packages

此生再无相见时 提交于 2019-12-03 05:49:03
问题 I have an Elm package (source + all build artifacts) in a local directory, and I would like to use it from another Elm package, without publishing the library. So my directory setup looks like this: / my-lib/ elm-package.json my-app/ elm-package.json First of all, running elm-package install in the library package's directory doesn't seem to do anything more than just building the package; it doesn't get installed in any global directory as far as I can tell. I've added my-lib to my-app/elm

What is an opaque type in Elm and why is it valuable?

≡放荡痞女 提交于 2019-12-03 03:17:37
I've used types before but don't know what an opaque type is. I've seen it mentioned as well. Is it better to expose an opaque type than a type alias? Nathan Let’s answer this question by first looking at type aliases: A type alias is fully transparent. This means that any other module importing it will have full access to its inner workings. Let’s say we’ve got a User module exposing a User type: module User exposing User type alias User = { userName : String , age : Int } Anyone importing User can manipulate the data, e.g. newUser = { oldUser | age = 25 } . Or do someUser = User "Bill" 27 .

What does the `<<` operator mean in elm?

江枫思渺然 提交于 2019-12-03 03:15:53
问题 In the following code taken from Elm Form Example, line 122, what does the << operator mean? Field.field Field.defaultStyle (Signal.send updateChan << toUpdate) "" content Couldn't find it in Elm syntax reference. Does it mean, when the field changes, instead of sending its content to updateChan , send toUpdate to updateChan ? 回答1: << is a function composition operator, defined in core library Basics. All functions from Basics are imported into Elm projects unqualified. Elm's type system Let