mirror of
https://github.com/janet-lang/janet
synced 2025-06-16 21:44:17 +00:00
Replace switch with case.
This commit is contained in:
parent
1f37919f39
commit
f28172a996
@ -1,22 +0,0 @@
|
|||||||
|
|
||||||
(defn put-in [coll keys val]
|
|
||||||
(defn assoc [the-coll n]
|
|
||||||
(if-let [current-key (get keys n)
|
|
||||||
current-val (get the-coll current-key)]
|
|
||||||
(put the-coll current-key (assoc current-val (inc n)))
|
|
||||||
val))
|
|
||||||
(assoc coll 0))
|
|
||||||
|
|
||||||
|
|
||||||
(defn update-in [coll keys an-fn]
|
|
||||||
(def new-keys (array-slice coll 0 -2) )
|
|
||||||
(def last-key (get (array-slice coll -1 -2) 0))
|
|
||||||
(defn assoc [the-coll n]
|
|
||||||
(if-let [current-key (get keys n)
|
|
||||||
current-val (get the-coll current-key)]
|
|
||||||
(put the-coll current-key (assoc current-val (inc n)))
|
|
||||||
( update the-coll last-key an-fn )))
|
|
||||||
(assoc coll new-keys 0))
|
|
||||||
|
|
||||||
|
|
||||||
;; (defn update-in-test [ ] (update-in @{:a "x" :b {:y {"pipa" 3}}} [:b :y "pipa"] type))
|
|
@ -110,7 +110,7 @@ static const DstInstructionDef dst_ops[] = {
|
|||||||
{"lds", DOP_LOAD_SELF},
|
{"lds", DOP_LOAD_SELF},
|
||||||
{"ldt", DOP_LOAD_TRUE},
|
{"ldt", DOP_LOAD_TRUE},
|
||||||
{"ldu", DOP_LOAD_UPVALUE},
|
{"ldu", DOP_LOAD_UPVALUE},
|
||||||
{"length", DOP_LENGTH},
|
{"len", DOP_LENGTH},
|
||||||
{"lt", DOP_LESS_THAN},
|
{"lt", DOP_LESS_THAN},
|
||||||
{"lti", DOP_LESS_THAN_INTEGER},
|
{"lti", DOP_LESS_THAN_INTEGER},
|
||||||
{"ltim", DOP_LESS_THAN_IMMEDIATE},
|
{"ltim", DOP_LESS_THAN_IMMEDIATE},
|
||||||
@ -118,10 +118,10 @@ static const DstInstructionDef dst_ops[] = {
|
|||||||
{"lter", DOP_LESS_THAN_EQUAL_REAL},
|
{"lter", DOP_LESS_THAN_EQUAL_REAL},
|
||||||
{"mkarr", DOP_MAKE_ARRAY},
|
{"mkarr", DOP_MAKE_ARRAY},
|
||||||
{"mkbuf", DOP_MAKE_BUFFER},
|
{"mkbuf", DOP_MAKE_BUFFER},
|
||||||
{"mktab", DOP_MAKE_TABLE},
|
|
||||||
{"mktup", DOP_MAKE_TUPLE},
|
|
||||||
{"mkstr", DOP_MAKE_STRING},
|
{"mkstr", DOP_MAKE_STRING},
|
||||||
{"mkstu", DOP_MAKE_STRUCT},
|
{"mkstu", DOP_MAKE_STRUCT},
|
||||||
|
{"mktab", DOP_MAKE_TABLE},
|
||||||
|
{"mktup", DOP_MAKE_TUPLE},
|
||||||
{"movf", DOP_MOVE_FAR},
|
{"movf", DOP_MOVE_FAR},
|
||||||
{"movn", DOP_MOVE_NEAR},
|
{"movn", DOP_MOVE_NEAR},
|
||||||
{"mul", DOP_MULTIPLY},
|
{"mul", DOP_MULTIPLY},
|
||||||
|
@ -178,7 +178,7 @@
|
|||||||
(def last (- (length args) 1))
|
(def last (- (length args) 1))
|
||||||
(apply1 f (array.concat (array.slice args 0 -2) (get args last))))
|
(apply1 f (array.concat (array.slice args 0 -2) (get args last))))
|
||||||
|
|
||||||
(defmacro switch
|
(defmacro case
|
||||||
"Select the body that equals the dispatch value. When pairs
|
"Select the body that equals the dispatch value. When pairs
|
||||||
has an odd number of arguments, the last is the default expression.
|
has an odd number of arguments, the last is the default expression.
|
||||||
If no match is found, returns nil"
|
If no match is found, returns nil"
|
||||||
@ -230,7 +230,7 @@
|
|||||||
(+ i 2) object
|
(+ i 2) object
|
||||||
} head)
|
} head)
|
||||||
(if (keyword? bindings)
|
(if (keyword? bindings)
|
||||||
(switch
|
(case
|
||||||
bindings
|
bindings
|
||||||
:while (do
|
:while (do
|
||||||
(array.push preds verb)
|
(array.push preds verb)
|
||||||
@ -238,7 +238,7 @@
|
|||||||
:let (tuple 'let verb (doone (+ i 2)))
|
:let (tuple 'let verb (doone (+ i 2)))
|
||||||
:when (tuple 'if verb (doone (+ i 2)))
|
:when (tuple 'if verb (doone (+ i 2)))
|
||||||
(error ("unexpected loop predicate: " verb)))
|
(error ("unexpected loop predicate: " verb)))
|
||||||
(switch verb
|
(case verb
|
||||||
:iterate (do
|
:iterate (do
|
||||||
(def preds @['and (tuple ':= bindings object)])
|
(def preds @['and (tuple ':= bindings object)])
|
||||||
(def subloop (doone (+ i 3) preds))
|
(def subloop (doone (+ i 3) preds))
|
||||||
@ -373,7 +373,7 @@
|
|||||||
"Takes multiple functions and returns a function that is the composition
|
"Takes multiple functions and returns a function that is the composition
|
||||||
of those functions."
|
of those functions."
|
||||||
[& functions]
|
[& functions]
|
||||||
(switch (length functions)
|
(case (length functions)
|
||||||
0 nil
|
0 nil
|
||||||
1 (get functions 0)
|
1 (get functions 0)
|
||||||
2 (let [[f g] functions] (fn [x] (f (g x))))
|
2 (let [[f g] functions] (fn [x] (f (g x))))
|
||||||
@ -475,7 +475,7 @@
|
|||||||
(if (< l limit) (:= limit l)))
|
(if (< l limit) (:= limit l)))
|
||||||
(def [i1 i2 i3 i4] inds)
|
(def [i1 i2 i3 i4] inds)
|
||||||
(def res (array.new limit))
|
(def res (array.new limit))
|
||||||
(switch ninds
|
(case ninds
|
||||||
1 (loop [i :range [0 limit]] (array.push res (f (get i1 i))))
|
1 (loop [i :range [0 limit]] (array.push res (f (get i1 i))))
|
||||||
2 (loop [i :range [0 limit]] (array.push res (f (get i1 i) (get i2 i))))
|
2 (loop [i :range [0 limit]] (array.push res (f (get i1 i) (get i2 i))))
|
||||||
3 (loop [i :range [0 limit]] (array.push res (f (get i1 i) (get i2 i) (get i3 i))))
|
3 (loop [i :range [0 limit]] (array.push res (f (get i1 i) (get i2 i) (get i3 i))))
|
||||||
@ -497,7 +497,7 @@
|
|||||||
(def l (length (get inds i)))
|
(def l (length (get inds i)))
|
||||||
(if (< l limit) (:= limit l)))
|
(if (< l limit) (:= limit l)))
|
||||||
(def [i1 i2 i3 i4] inds)
|
(def [i1 i2 i3 i4] inds)
|
||||||
(switch ninds
|
(case ninds
|
||||||
1 (loop [i :range [0 limit]] (f (get i1 i)))
|
1 (loop [i :range [0 limit]] (f (get i1 i)))
|
||||||
2 (loop [i :range [0 limit]] (f (get i1 i) (get i2 i)))
|
2 (loop [i :range [0 limit]] (f (get i1 i) (get i2 i)))
|
||||||
3 (loop [i :range [0 limit]] (f (get i1 i) (get i2 i) (get i3 i)))
|
3 (loop [i :range [0 limit]] (f (get i1 i) (get i2 i) (get i3 i)))
|
||||||
@ -659,7 +659,7 @@
|
|||||||
(defn reverse
|
(defn reverse
|
||||||
"Reverses order of elements in a given array or tuple"
|
"Reverses order of elements in a given array or tuple"
|
||||||
[t]
|
[t]
|
||||||
((switch (type t)
|
((case (type t)
|
||||||
:tuple tuple.reverse
|
:tuple tuple.reverse
|
||||||
:array array.reverse) t))
|
:array array.reverse) t))
|
||||||
|
|
||||||
@ -844,7 +844,7 @@
|
|||||||
newt)
|
newt)
|
||||||
|
|
||||||
(defn expand-bindings [x]
|
(defn expand-bindings [x]
|
||||||
(switch (type x)
|
(case (type x)
|
||||||
:array (map expand-bindings x)
|
:array (map expand-bindings x)
|
||||||
:tuple (apply1 tuple (map expand-bindings x))
|
:tuple (apply1 tuple (map expand-bindings x))
|
||||||
:table (dotable x expand-bindings)
|
:table (dotable x expand-bindings)
|
||||||
@ -888,7 +888,7 @@
|
|||||||
m? (apply1 m (tuple.slice t 1))
|
m? (apply1 m (tuple.slice t 1))
|
||||||
(apply1 tuple (map macroexpand-1 t))))
|
(apply1 tuple (map macroexpand-1 t))))
|
||||||
|
|
||||||
(def ret (switch (type x)
|
(def ret (case (type x)
|
||||||
:tuple (dotup x)
|
:tuple (dotup x)
|
||||||
:array (map macroexpand-1 x)
|
:array (map macroexpand-1 x)
|
||||||
:struct (table.to-struct (dotable x macroexpand-1))
|
:struct (table.to-struct (dotable x macroexpand-1))
|
||||||
@ -912,7 +912,7 @@
|
|||||||
(def tx (type x))
|
(def tx (type x))
|
||||||
(or
|
(or
|
||||||
(not= tx (type y))
|
(not= tx (type y))
|
||||||
(switch tx
|
(case tx
|
||||||
:tuple (or (not= (length x) (length y)) (some? (map deep-not= x y)))
|
:tuple (or (not= (length x) (length y)) (some? (map deep-not= x y)))
|
||||||
:array (or (not= (length x) (length y)) (some? (map deep-not= x y)))
|
:array (or (not= (length x) (length y)) (some? (map deep-not= x y)))
|
||||||
:struct (deep-not= (pairs x) (pairs y))
|
:struct (deep-not= (pairs x) (pairs y))
|
||||||
@ -987,12 +987,12 @@
|
|||||||
(def vals
|
(def vals
|
||||||
(coro
|
(coro
|
||||||
(while going
|
(while going
|
||||||
(switch (parser.status p)
|
(case (parser.status p)
|
||||||
:full (yield (parser.produce p))
|
:full (yield (parser.produce p))
|
||||||
:error (do
|
:error (do
|
||||||
(def (line col) (parser.where p))
|
(def (line col) (parser.where p))
|
||||||
(onerr where "parse" (string (parser.error p) " on line " line ", column " col)))
|
(onerr where "parse" (string (parser.error p) " on line " line ", column " col)))
|
||||||
(switch (fiber.status chars)
|
(case (fiber.status chars)
|
||||||
:new (parser.byte p (resume chars))
|
:new (parser.byte p (resume chars))
|
||||||
:pending (parser.byte p (resume chars))
|
:pending (parser.byte p (resume chars))
|
||||||
(:= going false))))
|
(:= going false))))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user