From a7211c60b890ace4d66d1f4325b23ecb3042c844 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Fri, 27 Oct 2023 15:59:31 +0200 Subject: [PATCH 1/2] Optimize cell? and fix docstrings --- src/javelin/core.clj | 22 ++++++++++------------ src/javelin/core.cljs | 13 ++++++------- src/javelin/core_clj.clj | 9 ++++----- 3 files changed, 20 insertions(+), 24 deletions(-) diff --git a/src/javelin/core.clj b/src/javelin/core.clj index e4c03d7..19e652d 100644 --- a/src/javelin/core.clj +++ b/src/javelin/core.clj @@ -13,8 +13,7 @@ [clojure.walk :refer [prewalk]] [clojure.pprint :as p] [cljs.analyzer :as a] - [clojure.java.io :as io] - [clojure.string :as s])) + [clojure.set :as set])) (declare walk) @@ -48,7 +47,7 @@ [bindings] (let [syms1 (set (extract-syms bindings)) syms2 (set (extract-syms bindings))] - (seq (clojure.set/intersection syms1 syms2)))) + (seq (set/intersection syms1 syms2)))) (defn bind-syms "Given a binding form, returns a seq of the symbols that will be bound. @@ -108,7 +107,6 @@ empty?* #(= 0 (count %)) dot? #(= '. (first %)) try? #(= 'try (first %)) - catch? #(= 'catch (first %)) finally? #(= 'finally (first %)) binding1? #(contains? '#{let* loop*} (first %)) binding2? #(= 'letfn* (first %)) @@ -170,14 +168,14 @@ (let [fname (when (symbol? (first arities)) [(first arities)]) arities (if fname (rest arities) arities) arities (if (vector? (first arities)) [arities] arities) - local (if fname (conj local (first fname)) local)] - (let [mkarity (fn [[bindings & body]] - (let [local (into local (remove #(= '& %) bindings))] - (to-list `([~@bindings] ~@(map #(walk % local) body))))) - arities (map mkarity arities)] - (to-list `(~sym ~@fname ~@arities))))) - - (defn walk-passthru [x local] + local (if fname (conj local (first fname)) local) + mkarity (fn [[bindings & body]] + (let [local (into local (remove #(= '& %) bindings))] + (to-list `([~@bindings] ~@(map #(walk % local) body))))) + arities (map mkarity arities)] + (to-list `(~sym ~@fname ~@arities)))) + + (defn walk-passthru [x _local] (with-let* [s (gensym)] (swap! *pass* assoc s x))) (defn walk-dot [[sym obj meth & more] local] diff --git a/src/javelin/core.cljs b/src/javelin/core.cljs index 6d2e054..4b7c925 100644 --- a/src/javelin/core.cljs +++ b/src/javelin/core.cljs @@ -8,8 +8,7 @@ (ns javelin.core (:require-macros [javelin.core]) - (:require [goog.array :as garray] - [goog.object :as gobj])) + (:require [goog.array :as garray])) ;; helpers ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; @@ -145,26 +144,26 @@ (defn cell? "Returns c if c is a Cell, nil otherwise." [c] - (when (= (type c) Cell) c)) + (instance? Cell c)) (defn formula? - [c] "Returns c if c is a formula cell, nil otherwise." + [c] (when (and (cell? c) (.-thunk c)) c)) (defn lens? - [c] "Returns c if c is a lens, nil otherwise." + [c] (when (and (cell? c) (.-update c)) c)) (defn input? - [c] "Returns c if c is an input cell, nil otherwise." + [c] (when (and (cell? c) (not (formula? c))) c)) (defn constant? - [c] "Returns c if c is a constant formula cell, nil otherwise." + [c] (when (and (cell? c) (.-constant c)) c)) (defn set-cell! diff --git a/src/javelin/core_clj.clj b/src/javelin/core_clj.clj index ce48526..b6d1ef9 100644 --- a/src/javelin/core_clj.clj +++ b/src/javelin/core_clj.clj @@ -1,7 +1,6 @@ (ns javelin.core-clj (:refer-clojure :exclude [accessor]) (:require - [riddley.compiler :refer [locals]] [riddley.walk :refer [walk-exprs]] [clojure.data.priority-map :refer [priority-map]])) @@ -84,11 +83,11 @@ (let [hoist (atom []) local #(symbol (name %)) core? #(= "clojure.core" (namespace %)) - skip? #(or - (not (contains? &env %)) - (contains? specials %) + skip? #(or + (not (contains? &env %)) + (contains? specials %) (core? %)) - walk! #(do (if-not (skip? %) + walk! #(do (if-not (skip? %) (do (swap! hoist conj %) (local %)) %)) walked (walk-exprs symbol? walk! expr) From 15f607b0911f9a6822ca07eebf60a0e217908d45 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Mon, 30 Oct 2023 14:09:30 +0100 Subject: [PATCH 2/2] Return cell --- src/javelin/core.cljs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/javelin/core.cljs b/src/javelin/core.cljs index 4b7c925..454cf1d 100644 --- a/src/javelin/core.cljs +++ b/src/javelin/core.cljs @@ -144,7 +144,7 @@ (defn cell? "Returns c if c is a Cell, nil otherwise." [c] - (instance? Cell c)) + (when (instance? Cell c) c)) (defn formula? "Returns c if c is a formula cell, nil otherwise."