1
0
mirror of https://github.com/janet-lang/janet synced 2025-01-12 16:40:27 +00:00

Remove foreach.

This commit is contained in:
Calvin Rose 2018-05-01 23:38:53 -04:00
parent 6b5c5ab0ad
commit 256aba199f
2 changed files with 13 additions and 20 deletions

View File

@ -1,12 +1,10 @@
# Get the number of occurences of elements in a set # Get the number of occurences of elements in a set
(import "examples/iterators.dst")
(defn frequencies (defn frequencies
"Get the number of occurences of each value in a sequence." "Get the number of occurences of each value in a indexed structure."
[s] [ind]
(let [freqs @{} (def freqs @{})
_ (foreach s (fn [x] (map (fn [x]
(let [n (get freqs x)] (let [n (get freqs x)]
(put freqs x (if n (+ 1 n) 1)))))] (put freqs x (if n (+ 1 n) 1)))) ind)
freqs)) freqs)

View File

@ -100,6 +100,12 @@
(defmacro <<= [x n] (tuple ':= x (tuple << x n))) (defmacro <<= [x n] (tuple ':= x (tuple << x n)))
(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 (defmacro comment
"Ignores the body of the comment." "Ignores the body of the comment."
[]) [])
@ -271,11 +277,6 @@ evaluates to true."
fal)))))) fal))))))
(aux 0)) (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 (defmacro when-let
"Takes the first one or two forms in vector and if true binds "Takes the first one or two forms in vector and if true binds
all the forms with let and evaluates the body" 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 (f res (get ind i))))
res) 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 (defn map
"Map a function over every element in an array or tuple and return "Map a function over every element in an array or tuple and return
the same type as the input sequence." the same type as the input sequence."