Last active
March 9, 2021 17:09
-
-
Save damesek/d6a371ce56983e77872cd7a574f548bb to your computer and use it in GitHub Desktop.
Vec version of frequencies, plus vec to map transform
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(def data [1 2 3 4 3 4 3 4 5 6 7]) | |
(defn -vec-frequencies [] | |
(fn [acc v] | |
(let [index (.indexOf acc v) | |
curr-count (if (pos? index) | |
(first (get acc (inc index))) | |
0)] | |
(if (zero? curr-count) | |
(conj acc v [1]) | |
(assoc acc (inc index) [(inc curr-count)]))))) | |
(reduce (-vec-frequencies) | |
[] | |
data) | |
=> [1 [1] 2 [1] 3 [3] 4 [3] 5 [1] 6 [1] 7 [1]] | |
(defn -transform-to-map [] | |
(fn [acc v] | |
(let [index (.indexOf dat v) | |
next-value (get dat (inc index))] | |
(if (number? v) | |
(conj acc {v (first next-value)}) | |
acc)))) | |
(reduce (-transform-to-map) | |
{} | |
(reduce (-vec-frequencies) | |
[] | |
data)) | |
=> {1 1, 2 1, 3 3, 4 3, 5 1, 6 1, 7 1} | |
;; with two function | |
(defn -Rvec-frequencies [data] | |
(reduce | |
(fn [acc v] | |
(let [index (.indexOf acc v) | |
curr-count (if (pos? index) | |
(first (get acc (inc index))) | |
0)] | |
(if (zero? curr-count) | |
(conj acc v [1]) | |
(assoc acc (inc index) [(inc curr-count)])) | |
) | |
) | |
[] | |
data)) | |
(defn -Rtransform-to-map [dat] | |
(reduce | |
(fn [acc v] | |
(let [index (.indexOf dat v) | |
next-value (get dat (inc index))] | |
(if (number? v) | |
(conj acc {v (first next-value)}) | |
acc))) | |
{} | |
dat)) | |
(->> data | |
(-Rvec-frequencies) | |
(-Rtransform-to-map)) | |
;; IndexOf (I think not so better than .indexOf, just thinking about) | |
(defn indexOf-all [coll search-item] | |
(reduce | |
(fn [{:keys [curr idx] :as acc} v] | |
; (println curr idx v search-item) | |
(if-not (= search-item v) | |
(assoc acc :idx (inc idx)) | |
(assoc acc :curr (conj curr idx) | |
:idx (inc idx)))) | |
{:curr [] :idx 0} | |
coll)) | |
(defn indexOf-first [coll search-item] | |
(reduce | |
(fn [{:keys [curr idx] :as acc} v] | |
; (println curr idx v search-item) | |
(if-not (= search-item v) | |
(assoc acc :idx (inc idx)) | |
(reduced idx))) | |
{:curr [] :idx 0} | |
coll)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@Rasparov pointed from Slack: weak point > .indexOf is slow at large numbers ->O(n)(true)
Maybe better Boyer Moore algorithms for this IndexOf approach.
https://github.com/rm-hull/boyer-moore-search/blob/master/src/boyer_moore/core.clj