This file contains hidden or 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
(defn group-ips [ips] | |
(if (> (count ips) 1) | |
(let [ips (->> ips distinct sort-ips) | |
to-array (fn [ip-str] (str/split ip-str #"\."))] | |
(reduce | |
(fn [acc el] | |
(condp #(re-matches %1 %2) (last acc) | |
#"^$" [el] | |
#"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}" | |
(let [arr-l (map parse-int (to-array (last acc))) |
This file contains hidden or 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
(defn sort-ips [ips] | |
(sort (fn [left right] | |
(let [left (read-string (str/replace left #"\." "")) | |
right (read-string (str/replace right #"\." ""))] | |
(< left right))) | |
ips)) |
This file contains hidden or 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
(defn distinct-by | |
"Returns a lazy sequence of the elements of coll, removing any elements that | |
return duplicate values when passed to a function f." | |
[f coll] | |
(let [step (fn step [xs seen] | |
(lazy-seq | |
((fn [[x :as xs] seen] | |
(when-let [s (seq xs)] | |
(let [fx (f x)] | |
(if (contains? seen fx) |
This file contains hidden or 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 regex-char-esc-smap | |
(let [esc-chars "()&^%$#!?*."] | |
(zipmap esc-chars | |
(map #(str "\\" %) esc-chars)))) | |
(defn str-to-pattern | |
[string] | |
(->> string | |
(replace regex-char-esc-smap) | |
(reduce str) |