mirror of
https://github.com/janet-lang/janet
synced 2025-01-12 08:30:26 +00:00
Remove foreach.
This commit is contained in:
parent
6b5c5ab0ad
commit
256aba199f
@ -1,12 +1,10 @@
|
||||
# Get the number of occurences of elements in a set
|
||||
|
||||
(import "examples/iterators.dst")
|
||||
|
||||
(defn frequencies
|
||||
"Get the number of occurences of each value in a sequence."
|
||||
[s]
|
||||
(let [freqs @{}
|
||||
_ (foreach s (fn [x]
|
||||
(let [n (get freqs x)]
|
||||
(put freqs x (if n (+ 1 n) 1)))))]
|
||||
freqs))
|
||||
"Get the number of occurences of each value in a indexed structure."
|
||||
[ind]
|
||||
(def freqs @{})
|
||||
(map (fn [x]
|
||||
(let [n (get freqs x)]
|
||||
(put freqs x (if n (+ 1 n) 1)))) ind)
|
||||
freqs)
|
||||
|
@ -100,6 +100,12 @@
|
||||
(defmacro <<= [x n] (tuple ':= x (tuple << x n)))
|
||||
(defmacro >>>= [x n] (tuple ':= x (tuple >>> x n)))
|
||||
|
||||
(defmacro default
|
||||
"Define a default value for an optional argument.
|
||||
Expands to (def sym (if (= nil sym) val sym))"
|
||||
[sym val]
|
||||
(tuple 'def sym (tuple 'if (tuple = nil sym) val sym)))
|
||||
|
||||
(defmacro comment
|
||||
"Ignores the body of the comment."
|
||||
[])
|
||||
@ -271,11 +277,6 @@ evaluates to true."
|
||||
fal))))))
|
||||
(aux 0))
|
||||
|
||||
(defmacro default
|
||||
"Supplies a default argument when a value is nil."
|
||||
[sym default-value]
|
||||
(tuple 'def sym (tuple 'or sym default-value)))
|
||||
|
||||
(defmacro when-let
|
||||
"Takes the first one or two forms in vector and if true binds
|
||||
all the forms with let and evaluates the body"
|
||||
@ -376,12 +377,6 @@ an indexed type (array, tuple) with a function to produce a value."
|
||||
(:= res (f res (get ind i))))
|
||||
res)
|
||||
|
||||
(defn foreach
|
||||
"Call function f on every value in indexed ind."
|
||||
[f ind]
|
||||
(for [i 0 (length ind)]
|
||||
(f (get ind i))))
|
||||
|
||||
(defn map
|
||||
"Map a function over every element in an array or tuple and return
|
||||
the same type as the input sequence."
|
||||
|
Loading…
Reference in New Issue
Block a user