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:
sforman 2023-07-29 17:10:45 -07:00
parent edfd7c526f
commit 200c390fd5
2 changed files with 23 additions and 5 deletions

View File

@ -1,9 +1,9 @@
module Joy exposing (doit, JoyDict)
module Joy exposing (doit, JoyDict, initialize)
import Bitwise
import Dict exposing (Dict, get)
import Dict exposing (Dict, get, insert)
import Result exposing (andThen)
import String exposing (replace, words)
import String exposing (replace, words, lines)
type JoyType
@ -457,3 +457,21 @@ doit text dict =
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 *""")

View File

@ -7,7 +7,7 @@ import Html exposing (Html, Attribute, div, input, text)
import Html.Attributes exposing (..)
import Html.Events exposing (onInput)
import Joy exposing (doit, JoyDict)
import Joy exposing (doit, JoyDict, initialize)
-- MAIN
@ -28,7 +28,7 @@ type alias Model =
init : Model
init =
{ content = "", dictionary = Dict.empty }
{ content = "", dictionary = initialize Dict.empty }