mirror of
https://github.com/janet-lang/janet
synced 2025-01-23 21:56:52 +00:00
Add each.
This commit is contained in:
parent
256aba199f
commit
238cec8f32
@ -2,7 +2,7 @@
|
|||||||
(import "examples/iterators.dst")
|
(import "examples/iterators.dst")
|
||||||
|
|
||||||
(defn sum3
|
(defn sum3
|
||||||
"Solve the 3SUM problem O(n^2) time."
|
"Solve the 3SUM problem in O(n^2) time."
|
||||||
[s]
|
[s]
|
||||||
(def tab @{})
|
(def tab @{})
|
||||||
(def solutions @{})
|
(def solutions @{})
|
||||||
|
@ -4,7 +4,7 @@
|
|||||||
"Get the number of occurences of each value in a indexed structure."
|
"Get the number of occurences of each value in a indexed structure."
|
||||||
[ind]
|
[ind]
|
||||||
(def freqs @{})
|
(def freqs @{})
|
||||||
(map (fn [x]
|
(each (fn [x]
|
||||||
(let [n (get freqs x)]
|
(let [n (get freqs x)]
|
||||||
(put freqs x (if n (+ 1 n) 1)))) ind)
|
(put freqs x (if n (+ 1 n) 1)))) ind)
|
||||||
freqs)
|
freqs)
|
||||||
|
@ -20,7 +20,7 @@
|
|||||||
(def t (type ith))
|
(def t (type ith))
|
||||||
(def tuple? (= t :tuple))
|
(def tuple? (= t :tuple))
|
||||||
(def array? (= t :array))
|
(def array? (= t :array))
|
||||||
(if (if tuple? tuple? array?) i
|
(if (if tuple? tuple? array?) i
|
||||||
(if (< i len) (recur (+ i 1))))))
|
(if (< i len) (recur (+ i 1))))))
|
||||||
(def start (fstart 0))
|
(def start (fstart 0))
|
||||||
(def fnbody (tuple-prepend (tuple-prepend (tuple-slice more start) name) 'fn))
|
(def fnbody (tuple-prepend (tuple-prepend (tuple-slice more start) name) 'fn))
|
||||||
@ -31,7 +31,7 @@
|
|||||||
"Define a macro."
|
"Define a macro."
|
||||||
(do
|
(do
|
||||||
(def defn* (get (get _env 'defn) :value))
|
(def defn* (get (get _env 'defn) :value))
|
||||||
(fn [name & more]
|
(fn [name & more]
|
||||||
(apply1 defn* (array-concat
|
(apply1 defn* (array-concat
|
||||||
@[name :macro] more)))))
|
@[name :macro] more)))))
|
||||||
|
|
||||||
@ -68,8 +68,8 @@
|
|||||||
(defn indexed? [x]
|
(defn indexed? [x]
|
||||||
(def t (type x))
|
(def t (type x))
|
||||||
(if (= t :array) true (= t :tuple)))
|
(if (= t :array) true (= t :tuple)))
|
||||||
(defn function? [x]
|
(defn function? [x]
|
||||||
(def t (type x))
|
(def t (type x))
|
||||||
(if (= t :function) true (= t :cfunction)))
|
(if (= t :function) true (= t :cfunction)))
|
||||||
(defn true? [x] (= (type x) true))
|
(defn true? [x] (= (type x) true))
|
||||||
(defn false? [x] (= (type x) false))
|
(defn false? [x] (= (type x) false))
|
||||||
@ -200,7 +200,7 @@ value."
|
|||||||
(array-concat accum body)
|
(array-concat accum body)
|
||||||
(apply1 tuple accum))
|
(apply1 tuple accum))
|
||||||
|
|
||||||
(defmacro for
|
(defmacro for
|
||||||
"An imperative for loop over an integer range. Use with caution and discretion."
|
"An imperative for loop over an integer range. Use with caution and discretion."
|
||||||
[head & body]
|
[head & body]
|
||||||
(def [sym start end _inc] (ast-unwrap1 head))
|
(def [sym start end _inc] (ast-unwrap1 head))
|
||||||
@ -262,7 +262,7 @@ evaluates to true."
|
|||||||
(do
|
(do
|
||||||
(def atm (atomic? (ast-unwrap1 bl)))
|
(def atm (atomic? (ast-unwrap1 bl)))
|
||||||
(def sym (if atm bl (gensym)))
|
(def sym (if atm bl (gensym)))
|
||||||
(if atm
|
(if atm
|
||||||
# Simple binding
|
# Simple binding
|
||||||
(tuple 'do
|
(tuple 'do
|
||||||
(tuple 'def sym br)
|
(tuple 'def sym br)
|
||||||
@ -271,7 +271,7 @@ evaluates to true."
|
|||||||
(tuple 'do
|
(tuple 'do
|
||||||
(tuple 'def sym br)
|
(tuple 'def sym br)
|
||||||
(tuple 'if sym
|
(tuple 'if sym
|
||||||
(tuple 'do
|
(tuple 'do
|
||||||
(tuple 'def bl sym)
|
(tuple 'def bl sym)
|
||||||
(aux (+ 2 i)))
|
(aux (+ 2 i)))
|
||||||
fal))))))
|
fal))))))
|
||||||
@ -330,7 +330,7 @@ Returns nil if args is empty."
|
|||||||
###
|
###
|
||||||
###
|
###
|
||||||
|
|
||||||
(def sort
|
(def sort
|
||||||
"Sort an array in-place. Uses quicksort and is not a stable sort."
|
"Sort an array in-place. Uses quicksort and is not a stable sort."
|
||||||
(do
|
(do
|
||||||
|
|
||||||
@ -394,14 +394,35 @@ the same type as the input sequence."
|
|||||||
2 (for [i 0 limit] (array-push res (f (get i1 i) (get i2 i))))
|
2 (for [i 0 limit] (array-push res (f (get i1 i) (get i2 i))))
|
||||||
3 (for [i 0 limit] (array-push res (f (get i1 i) (get i2 i) (get i3 i))))
|
3 (for [i 0 limit] (array-push res (f (get i1 i) (get i2 i) (get i3 i))))
|
||||||
4 (for [i 0 limit] (array-push res (f (get i1 i) (get i2 i) (get i3 i) (get i4 i))))
|
4 (for [i 0 limit] (array-push res (f (get i1 i) (get i2 i) (get i3 i) (get i4 i))))
|
||||||
(for [i 0 limit]
|
(for [i 0 limit]
|
||||||
(def args @[])
|
(def args @[])
|
||||||
(for [j 0 ninds] (array-push args (get (get inds j) i)))
|
(for [j 0 ninds] (array-push args (get (get inds j) i)))
|
||||||
(array-push res (apply1 f args))))
|
(array-push res (apply1 f args))))
|
||||||
res)
|
res)
|
||||||
|
|
||||||
|
(defn each
|
||||||
|
"Map a function over every element in an array or tuple but do not
|
||||||
|
return a new indexed type."
|
||||||
|
[f & inds]
|
||||||
|
(def ninds (length inds))
|
||||||
|
(if (= 0 ninds) (error "expected at least 1 indexed collection."))
|
||||||
|
(var limit (length (get inds 0)))
|
||||||
|
(for [i 0 ninds]
|
||||||
|
(def l (length (get inds i)))
|
||||||
|
(if (< l limit) (:= limit l)))
|
||||||
|
(def [i1 i2 i3 i4] inds)
|
||||||
|
(switch ninds
|
||||||
|
1 (for [i 0 limit] (f (get i1 i)))
|
||||||
|
2 (for [i 0 limit] (f (get i1 i) (get i2 i)))
|
||||||
|
3 (for [i 0 limit] (f (get i1 i) (get i2 i) (get i3 i)))
|
||||||
|
4 (for [i 0 limit] (f (get i1 i) (get i2 i) (get i3 i) (get i4 i)))
|
||||||
|
(for [i 0 limit]
|
||||||
|
(def args @[])
|
||||||
|
(for [j 0 ninds] (array-push args (get (get inds j) i)))
|
||||||
|
(apply1 f args))))
|
||||||
|
|
||||||
(defn mapcat
|
(defn mapcat
|
||||||
"Map a function over every element in an array or tuple and
|
"Map a function over every element in an array or tuple and
|
||||||
use array to concatenate the results. Returns the same
|
use array to concatenate the results. Returns the same
|
||||||
type as the input sequence."
|
type as the input sequence."
|
||||||
[f ind t]
|
[f ind t]
|
||||||
@ -517,7 +538,7 @@ in the same manner, and so on. Useful for expressing pipelines of data."
|
|||||||
(apply1 tuple parts))
|
(apply1 tuple parts))
|
||||||
(reduce fop x forms))
|
(reduce fop x forms))
|
||||||
|
|
||||||
(defn partial
|
(defn partial
|
||||||
"Partial function application."
|
"Partial function application."
|
||||||
[f & more]
|
[f & more]
|
||||||
(if (zero? (length more)) f
|
(if (zero? (length more)) f
|
||||||
@ -628,7 +649,7 @@ in the same manner, and so on. Useful for expressing pipelines of data."
|
|||||||
###
|
###
|
||||||
###
|
###
|
||||||
|
|
||||||
(defn pp
|
(defn pp
|
||||||
"Pretty print a value. Displays values inside collections, and is safe
|
"Pretty print a value. Displays values inside collections, and is safe
|
||||||
to call on any table. Does not print table prototype information."
|
to call on any table. Does not print table prototype information."
|
||||||
[x]
|
[x]
|
||||||
@ -706,7 +727,7 @@ to call on any table. Does not print table prototype information."
|
|||||||
:table (fn [y] (do-ds y "@{" "}" true pp-dict))
|
:table (fn [y] (do-ds y "@{" "}" true pp-dict))
|
||||||
:struct (fn [y] (do-ds y "{" "}" false pp-dict))
|
:struct (fn [y] (do-ds y "{" "}" false pp-dict))
|
||||||
})
|
})
|
||||||
|
|
||||||
(:= recur (fn [y]
|
(:= recur (fn [y]
|
||||||
(def p (get printers (type y)))
|
(def p (get printers (type y)))
|
||||||
(if p
|
(if p
|
||||||
@ -731,7 +752,7 @@ to call on any table. Does not print table prototype information."
|
|||||||
(defn doarray [a]
|
(defn doarray [a]
|
||||||
(def len (length a))
|
(def len (length a))
|
||||||
(def newa @[])
|
(def newa @[])
|
||||||
(for [i 0 len]
|
(for [i 0 len]
|
||||||
(array-push newa (macroexpand1 (get a i))))
|
(array-push newa (macroexpand1 (get a i))))
|
||||||
newa)
|
newa)
|
||||||
|
|
||||||
@ -851,7 +872,7 @@ onvalue."
|
|||||||
|
|
||||||
# Fiber stream of values
|
# Fiber stream of values
|
||||||
(def vals (coro
|
(def vals (coro
|
||||||
(def p (parser 1))
|
(def p (parser 1))
|
||||||
(while going
|
(while going
|
||||||
(switch (parser-status p)
|
(switch (parser-status p)
|
||||||
:full (yield (parser-produce p))
|
:full (yield (parser-produce p))
|
||||||
|
Loading…
Reference in New Issue
Block a user