Skip to content

Instantly share code, notes, and snippets.

@ksaua
Created February 20, 2015 07:28
Show Gist options
  • Save ksaua/e0bf867531d420821133 to your computer and use it in GitHub Desktop.
Save ksaua/e0bf867531d420821133 to your computer and use it in GitHub Desktop.
Bank ocr
(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))
@abingham
Copy link

I was hoping someone would do clojure :)

Can you replace (map #(apply str %1)) with just (map str)?

@ksaua
Copy link
Author

ksaua commented Feb 20, 2015

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.

@abingham
Copy link

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!

@ksaua
Copy link
Author

ksaua commented Feb 20, 2015

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).

@abingham
Copy link

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