mirror of
https://github.com/janet-lang/janet
synced 2025-02-03 02:39:09 +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)
|
||||||
|
@ -400,6 +400,27 @@ the same type as the input sequence."
|
|||||||
(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
|
||||||
|
Loading…
Reference in New Issue
Block a user