Hi all

Just started to play around with elm and very eager to get more fluid. I 
picked a silly POC scenario of a rule composer: basically you have regex 
rules and want to edit/test them in a form. When applying a regex and 
showing the replace output in an input field, I face an "undefined". Below 
you see the isolated part forcing my observation while applying the rule 
directly in init.

Unfortunately I could really not trace down to the root of this happening. 
Is somebody able to tell me:
- What I am doing wrong? Why does there an "undefined" appear? (wenn 
executing my own "replace" function in the REPL it works fine as expected)
- How do you guys debug such behaviour?

Here my silly example:


module RuleTest exposing (..)


import Html.App
import Html exposing (..)
import Html.Attributes exposing (..)
import Regex


main : Platform.Program Basics.Never
main =
  Html.App.program
    { init = init
    , view = viewRuleTest
    , update = update
    , subscriptions = \_ -> Sub.none
    }


type alias Rule = 
  { regex : String
  , substitution : String
  }


type alias RuleTest = 
  { rule : Rule
  , input : String
  , output : String
  , matched : Bool
  }


type Msg 
  = StartTest String




-- init
init : (RuleTest, Cmd Msg)
init =
  ( apply "reg1" (Rule "reg1" "sub1")
    , Cmd.none
  )


-- update
update : Msg -> RuleTest -> (RuleTest, Cmd Msg)
update msg ruleTest =
  case msg of


    StartTest input ->
      ( apply input ruleTest.rule, Cmd.none )




apply : String -> Rule -> RuleTest
apply input rule =
  let 
    output = replace rule.regex rule.substitute input
    matched = input /= output
    rule = rule
  in
    RuleTest rule input output True


replace : String -> String -> String -> String
replace regex substitute input =
  Regex.replace Regex.All (Regex.regex regex) (\_ -> substitute) input


viewRuleTest : RuleTest -> Html Msg
viewRuleTest ruleTest =
  div []
    [ label [] [ text "Pattern" ]
    , input [ Html.Attributes.value ruleTest.rule.regex, disabled True ] []
    , label [] [ text "Substitution" ]
    , input [ Html.Attributes.value ruleTest.rule.substitution, disabled 
True  ] []
    , label [] [ text "Input" ]
    , input [ Html.Attributes.value ruleTest.input, disabled True  ] []
    , label [] [ text "Output" ]
    , input [ Html.Attributes.value ruleTest.output, disabled True  ] []
    , label [] [ text "matched" ]
    , input [ type' "checkbox", checked ruleTest.matched ] []
    ]


Environment: 
elm repl --version
0.17.1
OSX 10.11.3

{
    "version": "1.0.0",
    "summary": "helpful summary of your project, less than 80 characters",
    "repository": "https://github.com/user/project.git";,
    "license": "BSD3",
    "source-directories": [
        "."
    ],
    "exposed-modules": [],
    "dependencies": {
        "elm-lang/core": "4.0.1 <= v < 5.0.0",
        "elm-lang/html": "1.1.0 <= v < 2.0.0"
    },
    "elm-version": "0.17.1 <= v < 0.18.0"
}


Thanks for any advice!

Best, Andi


-- 
You received this message because you are subscribed to the Google Groups "Elm 
Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to