From c17932571945021ddc57794609add1920f78636e Mon Sep 17 00:00:00 2001 From: Scott Richmond Date: Thu, 1 Jun 2023 13:11:06 -0600 Subject: [PATCH] Improve prelude --- src/ludus/interpreter.clj | 4 +++- src/ludus/prelude.clj | 31 ++++++++++++++++++++++++++++--- 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/src/ludus/interpreter.clj b/src/ludus/interpreter.clj index 4bca9da..cf2ac8c 100644 --- a/src/ludus/interpreter.clj +++ b/src/ludus/interpreter.clj @@ -963,7 +963,9 @@ (do (def source " - + let xs = [1, 2, 3] + let ys = #{:a 1, :b 2} + get (:c, ys) ") (println "") diff --git a/src/ludus/prelude.clj b/src/ludus/prelude.clj index bf10de1..b21d07f 100644 --- a/src/ludus/prelude.clj +++ b/src/ludus/prelude.clj @@ -92,12 +92,36 @@ (def get- {:name "get" ::data/type ::data/clj - :body get}) + :body (fn + ([key, map] + (if (map? map) + (get map key) + nil)) + ([key, map, default] + (if (map? map) + (get map key default) + default)))}) (def draw {:name "draw" ::data/type ::data/clj :body d/ludus-draw}) +(def first- {:name "first" + ::data/type ::data/clj + :body (fn [v] (second v))}) + +(def rest- {:name "rest" + ::data/type ::data/clj + :body (fn [v] + (into [::data/list] (nthrest v 2)))}) + +(def nth- {:name "nth" + ::data/type ::data/clj + :body (fn [i, xs] + (if (contains? xs (inc i)) + (nth xs (inc i)) + nil))}) + (defn get-type [value] (let [t (type value)] (cond @@ -149,8 +173,6 @@ (def prelude { "id" id - "foo" :foo - "bar" :bar "eq" eq "add" add "print" print- @@ -172,4 +194,7 @@ "draw" draw "type" type- "clj" clj + "first" first- + "rest" rest- + "nth" nth- }) \ No newline at end of file