From 200c390fd5dc4ff4d523e1d0322ca167571600ce Mon Sep 17 00:00:00 2001 From: sforman Date: Sat, 29 Jul 2023 17:10:45 -0700 Subject: [PATCH] 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'. --- implementations/Elm/src/Joy.elm | 24 +++++++++++++++++++++--- implementations/Elm/src/Main.elm | 4 ++-- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/implementations/Elm/src/Joy.elm b/implementations/Elm/src/Joy.elm index 267a54a..c76bdd6 100644 --- a/implementations/Elm/src/Joy.elm +++ b/implementations/Elm/src/Joy.elm @@ -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 *""") + diff --git a/implementations/Elm/src/Main.elm b/implementations/Elm/src/Main.elm index 3420df1..d21f176 100644 --- a/implementations/Elm/src/Main.elm +++ b/implementations/Elm/src/Main.elm @@ -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 } -- 2.11.0