From: sforman Date: Sun, 30 Jul 2023 00:10:45 +0000 (-0700) Subject: Initialize dict with defs. X-Git-Url: http://git.osdn.net/view?a=commitdiff_plain;h=200c390fd5dc4ff4d523e1d0322ca167571600ce;p=joypy%2FThun.git 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'. --- 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 }