OSDN Git Service

Initialize dict with defs.
authorsforman <sforman@hushmail.com>
Sun, 30 Jul 2023 00:10:45 +0000 (17:10 -0700)
committersforman <sforman@hushmail.com>
Sun, 30 Jul 2023 00:10:45 +0000 (17:10 -0700)
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'.

implementations/Elm/src/Joy.elm
implementations/Elm/src/Main.elm

index 267a54a..c76bdd6 100644 (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 *""")
+
index 3420df1..d21f176 100644 (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 }