Created
February 20, 2015 07:28
-
-
Save ksaua/e0bf867531d420821133 to your computer and use it in GitHub Desktop.
Bank ocr
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
(require '[clojure.string :as str]) | |
(def characters (apply str | |
" _ _ _ _ _ _ _ _ \n" | |
"| | | _| _||_||_ |_ ||_||_|\n" | |
"|_| ||_ _| | _||_| ||_| _|\n" | |
" \n")) | |
(defn chunk3 [line] | |
(->> (partition 3 line) | |
(map #(apply str %1)))) | |
(defn transpose [lst] | |
(apply mapv vector lst)) | |
(def all-numbers | |
(->> characters | |
(str/split-lines) | |
(map chunk3) | |
(transpose) | |
(map-indexed vector) | |
(map (fn[[a b]] [b a])) | |
(into {}))) | |
(defn ocrToNumber [ocr] | |
(->> ocr | |
(str/split-lines) | |
(map chunk3) | |
(transpose) | |
(map #(get all-numbers %1)))) | |
(def toOcr (apply str | |
" _ _ \n" | |
" ||_||_||_ \n" | |
" ||_| ||_|\n" | |
" \n")) | |
(println toOcr) | |
(println (ocrToNumber toOcr)) |
No, because (partition 3 line) returns a seq of lazyseq (of chars):
(map str (partition 3 "abcxyz"))
("clojure.lang.LazySeq@1ecc1" "clojure.lang.LazySeq@245f8")
Partition is a general purpose function for seqs, and strings are just char seqs. I suppose there might exist a better partition function.
Ah, ok. What's kind of incredible is that if I apply the change I mentioned and run the program, I still find that it actually still works as expected. Maybe the stringified versions of the lazy sequences are stable due to caching/memoization. If so, that's pretty impressive!
What version of clojure are you using? I am using 1.5.1 and if I apply your change all I get is (nil nil nil nil).
It's 1.5.1 on OS X.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I was hoping someone would do clojure :)
Can you replace
(map #(apply str %1))
with just(map str)
?