Last active
August 29, 2015 14:12
-
-
Save trptcolin/a573561ac9262092f254 to your computer and use it in GitHub Desktop.
Sorry for all the ->>; they're too much fun not to use.
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
;; test/foobar/core_test.clj | |
(ns foobar.core-test | |
(:require [clojure.test :refer :all] | |
[foobar.core :as foobar])) | |
(deftest formats-intervals-properly | |
(is (= "1" (foobar/format-interval [1]))) | |
(is (= "1-2" (foobar/format-interval [1 2]))) | |
(is (= "1-3" (foobar/format-interval [1 2 3])))) | |
(deftest test-intervals-for-pred | |
(is (= [[2] [4 5 6] [8] [10 11 12]] | |
(foobar/intervals-for-pred foobar/is-one? "00101110101110")))) | |
(deftest test-format-one-intervals | |
(is (= ["3" "5-7" "9" "11-13"] | |
(foobar/format-one-intervals "00101110101110")))) | |
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
;; src/foobar/core.clj | |
(ns foobar.core | |
(require [clojure.string :as str] | |
[clojure.test :refer :all])) | |
(defn format-interval [xs] | |
(let [[a z] ((juxt first last) xs)] | |
(str a (when (not= a z) (str "-" z))))) | |
(defn intervals-for-pred [pred xs] | |
(->> xs | |
(map pred) | |
(map-indexed vector) | |
(partition-by last) | |
(filter (comp last first)) | |
(map #(map first %)))) | |
(defn is-one? [c] (= \1 c)) | |
(defn format-one-intervals [s] | |
(->> s | |
(intervals-for-pred is-one?) | |
(map (comp format-interval | |
(partial map inc))))) | |
(defn -main [input-bitstring & args] | |
(->> input-bitstring | |
format-one-intervals | |
(str/join ", ") | |
println)) | |
;; colin:foobar/ $ lein run -m foobar.core 001011101011100 | |
;; => 3, 5-7, 9, 11-13 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment