aboutsummaryrefslogtreecommitdiff
path: root/challenge-131/tyler-wardhaugh/clojure/src/tw/weekly/c131/t1.clj
blob: a14599bc54482166e50dfe369997f51cb2d7a638 (plain)
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
(ns tw.weekly.c131.t1
  (:require [clojure.edn :as edn]
            [clojure.pprint :refer [cl-format]]))

;;;
; Task description for TASK #1 › Consecutive Arrays
;;;
(def DEFAULT-INPUT [[1 2 3 6 7 8 9]])

(defn partition-by-prev
  "Similar to partition-by, partition coll when f returns a new value, where f expects the previous
  element and the current one."
  [f coll]
  (let [source (->> (map f coll (rest coll))
                    (reductions not= true)
                    (map list coll))
        xf (comp (partition-by second)
                 (map (partial map first)))]
    (sequence xf source)))

(defn split-consecutive
  "Partition coll into contiguous groups of integers increasing by 1."
  [coll]
  (partition-by-prev #(not= %1 (dec %2)) coll))

(defn -main
  "Run Task 1 with a given input N, defaulting to the first example from the
  task description."
  [& args]
  (let [[N] (or (some->> args (map edn/read-string)) DEFAULT-INPUT)]
    (cl-format true "(~{[~{~a~^, ~}]~^, ~})~%" (split-consecutive N))))