-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.clj
46 lines (36 loc) · 966 Bytes
/
utils.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
(ns aoc.utils
(:require
[clojure.java.io :as io]
[clojure.string :as s]))
(defn read-resource [i]
(slurp (io/resource i)))
(defn read-lines [i]
(line-seq (io/reader (io/resource i))))
(defn split-by
([pred]
(comp (drop-while pred)
(partition-by pred)
(take-nth 2)))
([pred coll]
(sequence (split-by pred) coll)))
(defn read-paras [i]
(split-by empty? (read-lines i)))
(defn read-ints [i]
(map #(Integer/parseInt %) (read-lines i)))
(defn read-longs [i]
(map #(Long/parseLong %) (read-lines i)))
(defn seq-peek
"Calls callback after n entries in s are evaluated."
[n callback s]
(let [call-and-return
(fn [iter data]
(if (zero? (rem iter n)) (callback iter data) nil)
data)]
(map call-and-return
(iterate inc 1) s)))
(defn unlines [ss]
(s/join "\n" ss))
(defn uncommas [s]
(s/split s #","))
(defn sum-lines [input f]
(transduce f + 0 (read-lines input)))