Skip to content

Instantly share code, notes, and snippets.

@MarkyMarkMcDonald
Created December 9, 2017 16:55
Show Gist options
  • Save MarkyMarkMcDonald/117462c27a10ac2ff38ce9440e5f732e to your computer and use it in GitHub Desktop.
Save MarkyMarkMcDonald/117462c27a10ac2ff38ce9440e5f732e to your computer and use it in GitHub Desktop.
-- Read all about this program in the official Elm guide:
-- https://guide.elm-lang.org/architecture/user_input/text_fields.html
import Html exposing (Html, Attribute, beginnerProgram, text, div, input)
import Html.Attributes exposing (..)
import Html.Events exposing (onInput)
import List
import String
import Regex
import Result
import Debug
main =
beginnerProgram { model = {polynomial = [], divisor = []}, view = view, update = update }
-- UPDATE
type Msg = PolynomialChanged String | DivisorChanged String
update event oldState =
case event of
PolynomialChanged newPolynomial ->
{ oldState | polynomial = toExpression newPolynomial }
DivisorChanged newDivisor ->
{ oldState | divisor = toExpression newDivisor }
type alias Term = { constant: Int, exponent: Int }
type alias Expression = List Term
toExpression: String -> Expression
toExpression input =
List.map toTerm (termInputs input)
toTerm: String -> Term
toTerm input =
{constant = extractConstant input, exponent = extractExponent input}
extractConstant input =
if String.contains "x" input
then
let matches = Regex.find (Regex.AtMost 1) (Regex.regex "[-]?\\d+") input in
matches |> List.head |> Maybe.map .match |> Maybe.map (\match -> String.toInt (Debug.log "constant match" match) |> Result.toMaybe) |> Maybe.withDefault (Just 1) |> Maybe.withDefault 1
else
String.toInt input |> Result.withDefault 0
extractExponent input =
if String.contains "x" input
then
let matches = Regex.find (Regex.AtMost 1) (Regex.regex "\\d+$") input in
matches |> List.head |> Maybe.map .match |> Maybe.map (\match -> String.toInt (Debug.log "exponent match" match) |> Result.toMaybe) |> Maybe.withDefault (Just 1) |> Maybe.withDefault 1
else
0
termInputs: String -> List String
termInputs input = String.split " " input
termToString: Term -> String
termToString term =
"constant: " ++ toString term.constant ++ "."
++ " exponent: " ++ toString term.exponent ++ "."
result: Expression -> Expression -> String
result polynomial divisor =
"Result:"
-- VIEW
view state =
div []
[ input [ placeholder "polynomial", onInput PolynomialChanged, myStyle ] []
, input [ placeholder "divisor", onInput DivisorChanged, myStyle ] []
, div [ myStyle ] [ text ("polynomial: " ++ (state.polynomial |> List.map termToString |> String.join ", ")) ]
, div [ myStyle ] [ text ("divisor: " ++ (state.divisor |> List.map termToString |> String.join ", ")) ]
, div [ myStyle ] [ text (result (state.polynomial) (state.divisor)) ]
]
myStyle =
style
[ ("width", "100%")
, ("height", "40px")
, ("padding", "10px 0")
, ("font-size", "2em")
, ("text-align", "center")
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment