Merge pull request #109 from jolt-lang/refactor-phs-split
Refactor: extract PersistentHashSet from phm.janet (jolt-bvek)
This commit is contained in:
commit
b06855db1f
6 changed files with 47 additions and 41 deletions
|
|
@ -13,6 +13,7 @@
|
|||
(use ./evaluator)
|
||||
(import ./reader :as r)
|
||||
(import ./phm :as phm)
|
||||
(import ./phs :as phs)
|
||||
(import ./pv :as pv)
|
||||
|
||||
# The IR is portable data; reading its representation is a host-layer concern.
|
||||
|
|
@ -584,7 +585,7 @@
|
|||
# then the persistent set is constructed — mirrors compiler.janet's emit-set-expr.
|
||||
(defn- emit-set [ctx node]
|
||||
(def items (map |(emit ctx $) (vview (node :items))))
|
||||
(tuple/slice (array/concat @[phm/make-phs] items)))
|
||||
(tuple/slice (array/concat @[phs/make-phs] items)))
|
||||
|
||||
(set emit
|
||||
(fn emit [ctx raw]
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@
|
|||
|
||||
(use ./types)
|
||||
(use ./phm)
|
||||
(use ./phs)
|
||||
(use ./lazyseq)
|
||||
(use ./regex)
|
||||
(use ./config)
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@
|
|||
|
||||
(use ./types)
|
||||
(use ./phm)
|
||||
(use ./phs)
|
||||
(use ./lazyseq)
|
||||
(use ./pv)
|
||||
(use ./plist)
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@
|
|||
(use ./api)
|
||||
(use ./types)
|
||||
(use ./phm)
|
||||
(use ./phs)
|
||||
(use ./lazyseq)
|
||||
(use ./pv)
|
||||
(use ./plist)
|
||||
|
|
|
|||
|
|
@ -174,43 +174,3 @@
|
|||
(while (< i n) (set m (phm-assoc m (kvs i) (kvs (+ i 1)))) (+= i 2)))
|
||||
m)
|
||||
|
||||
# ============================================================
|
||||
# PersistentHashSet — backed by PersistentHashMap
|
||||
# ============================================================
|
||||
|
||||
(defn set?
|
||||
"Check if x is a PersistentHashSet."
|
||||
[x]
|
||||
(and (table? x) (= :jolt/set (x :jolt/type))))
|
||||
|
||||
(defn make-phs [& xs]
|
||||
"Create a PersistentHashSet from items."
|
||||
(var m (make-phm))
|
||||
(each x xs (set m (phm-assoc m x true)))
|
||||
@{:jolt/type :jolt/set :phm m :cnt (phm-count m)})
|
||||
|
||||
(defn phs-conj [s & xs]
|
||||
(var m (s :phm))
|
||||
(each x xs (set m (phm-assoc m x true)))
|
||||
@{:jolt/type :jolt/set :phm m :cnt (phm-count m)})
|
||||
|
||||
(defn phs-disj [s & xs]
|
||||
(var m (s :phm))
|
||||
(each x xs (set m (phm-dissoc m x)))
|
||||
@{:jolt/type :jolt/set :phm m :cnt (phm-count m)})
|
||||
|
||||
(defn phs-contains? [s x]
|
||||
(phm-contains? (s :phm) x))
|
||||
|
||||
(defn phs-count [s]
|
||||
(s :cnt))
|
||||
|
||||
(defn phs-empty? [s]
|
||||
(= 0 (s :cnt)))
|
||||
|
||||
(defn phs-seq [s]
|
||||
(tuple ;(keys (phm-to-struct (s :phm)))))
|
||||
|
||||
(defn phs-get [s x &opt default]
|
||||
(default default nil)
|
||||
(if (phm-contains? (s :phm) x) x default))
|
||||
|
|
|
|||
42
src/jolt/phs.janet
Normal file
42
src/jolt/phs.janet
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
# PersistentHashSet — a set backed by a PersistentHashMap (members are keys
|
||||
# mapped to true). Extracted from phm.janet (jolt-bvek) so phm.janet is purely
|
||||
# the hash map; the set is a thin layer over it.
|
||||
|
||||
(use ./phm)
|
||||
|
||||
(defn set?
|
||||
"Check if x is a PersistentHashSet."
|
||||
[x]
|
||||
(and (table? x) (= :jolt/set (x :jolt/type))))
|
||||
|
||||
(defn make-phs [& xs]
|
||||
"Create a PersistentHashSet from items."
|
||||
(var m (make-phm))
|
||||
(each x xs (set m (phm-assoc m x true)))
|
||||
@{:jolt/type :jolt/set :phm m :cnt (phm-count m)})
|
||||
|
||||
(defn phs-conj [s & xs]
|
||||
(var m (s :phm))
|
||||
(each x xs (set m (phm-assoc m x true)))
|
||||
@{:jolt/type :jolt/set :phm m :cnt (phm-count m)})
|
||||
|
||||
(defn phs-disj [s & xs]
|
||||
(var m (s :phm))
|
||||
(each x xs (set m (phm-dissoc m x)))
|
||||
@{:jolt/type :jolt/set :phm m :cnt (phm-count m)})
|
||||
|
||||
(defn phs-contains? [s x]
|
||||
(phm-contains? (s :phm) x))
|
||||
|
||||
(defn phs-count [s]
|
||||
(s :cnt))
|
||||
|
||||
(defn phs-empty? [s]
|
||||
(= 0 (s :cnt)))
|
||||
|
||||
(defn phs-seq [s]
|
||||
(tuple ;(keys (phm-to-struct (s :phm)))))
|
||||
|
||||
(defn phs-get [s x &opt default]
|
||||
(default default nil)
|
||||
(if (phm-contains? (s :phm) x) x default))
|
||||
Loading…
Add table
Add a link
Reference in a new issue