1
0
mirror of https://github.com/janet-lang/janet synced 2024-07-05 03:23:14 +00:00
janet/examples/frequencies.dst

13 lines
307 B
Plaintext
Raw Normal View History

2018-03-14 03:39:49 +00:00
# Get the number of occurences of elements in a set
(import "examples/iterators.dst")
2018-03-14 03:39:49 +00:00
(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)))))]
2018-03-14 03:39:49 +00:00
freqs))