Initialize dict with defs.
Just sqr for now, but it shows that it works. I would like to return Results from add_def but it makes using foldl slightly tricky, not a lot, just slightly, and my brainpower is low at the mo'.
This commit is contained in:
parent
edfd7c526f
commit
200c390fd5
|
|
@ -1,9 +1,9 @@
|
||||||
module Joy exposing (doit, JoyDict)
|
module Joy exposing (doit, JoyDict, initialize)
|
||||||
|
|
||||||
import Bitwise
|
import Bitwise
|
||||||
import Dict exposing (Dict, get)
|
import Dict exposing (Dict, get, insert)
|
||||||
import Result exposing (andThen)
|
import Result exposing (andThen)
|
||||||
import String exposing (replace, words)
|
import String exposing (replace, words, lines)
|
||||||
|
|
||||||
|
|
||||||
type JoyType
|
type JoyType
|
||||||
|
|
@ -457,3 +457,21 @@ doit text dict =
|
||||||
Err msg -> Err msg
|
Err msg -> Err msg
|
||||||
Err msg -> Err msg
|
Err msg -> Err msg
|
||||||
|
|
||||||
|
|
||||||
|
add_def : String -> JoyDict -> JoyDict
|
||||||
|
add_def def dict =
|
||||||
|
case text_to_expression def of
|
||||||
|
Err msg -> dict
|
||||||
|
Ok expr ->
|
||||||
|
case expr of
|
||||||
|
[] -> dict
|
||||||
|
sym :: body ->
|
||||||
|
-- check that name is a symbol
|
||||||
|
case sym of
|
||||||
|
JoySymbol name -> (insert name body dict)
|
||||||
|
_ -> dict
|
||||||
|
|
||||||
|
|
||||||
|
initialize : JoyDict -> JoyDict
|
||||||
|
initialize dict = List.foldl (add_def) dict (lines """sqr dup *""")
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,7 @@ import Html exposing (Html, Attribute, div, input, text)
|
||||||
import Html.Attributes exposing (..)
|
import Html.Attributes exposing (..)
|
||||||
import Html.Events exposing (onInput)
|
import Html.Events exposing (onInput)
|
||||||
|
|
||||||
import Joy exposing (doit, JoyDict)
|
import Joy exposing (doit, JoyDict, initialize)
|
||||||
|
|
||||||
-- MAIN
|
-- MAIN
|
||||||
|
|
||||||
|
|
@ -28,7 +28,7 @@ type alias Model =
|
||||||
|
|
||||||
init : Model
|
init : Model
|
||||||
init =
|
init =
|
||||||
{ content = "", dictionary = Dict.empty }
|
{ content = "", dictionary = initialize Dict.empty }
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue