After making the Fibonnaci Generator app work...this one wasn't as hard as I expected...actually I think I'm slowly getting used to Clojure...which is always nice when learning a new language -;)
Here's the source code...
LED_Numbers.clj |
---|
(def leds {"0" (list " _ " "| | " "|_| ") "1" (list " " "| " "| ") "2" (list " _ " " _| " "|_ ") "3" (list "_ " "_| " "_| ") "4" (list " " "|_| " " | ") "5" (list " _ " "|_ " " _| ") "6" (list " _ " "|_ " "|_| ") "7" (list "_ " " | " " | ") "8" (list " _ " "|_| " "|_| ") "9" (list " _ " "|_| " " _| ")}) (defn toList [number] (map str(seq(str number)))) (defn get_led [x n num] (cond (> (count x) 0) (concat (nth (get leds (first x)) n) (get_led (rest x) n num)) (and (= (count x) 0) (< n 2)) (concat "" "\n" (get_led (toList num) (+ 1 n) num)) (and (= (count x) 0) (= n 2)) (concat "" "\n"))) (defn showLED [num] (do (print (apply str (get_led (toList num) 0 num))))(symbol "")) |
Well...let's go back and keep learning -:D
Greetings,
Blag.
Development Culture.
8 comentarios:
This looks like a nice exercise for exploring bits of a programming language.
I'm going to make some suggestions based on what I'd expect to see if I was reviewing this code. I'm try
- Instead of (list " _ " "| | " "|_| ") I'd normally see the literal vector form used [" _ " "| | " "|_| "].
- Names typically follow kebab case. (to-list instead of toList, get-led instead of get_led).
- There are built-ins for some of the conditions you are checking for (pos?, zero?)
- The do shouldn't be needed.
BankOCR is a similar exercise that adds a few more challenges. You might find it interesting. https://web.archive.org/web/20150313112458/http://codingdojo.org/cgi-bin/index.pl?KataBankOCR
Came across your post on the Planet Clojure rss feed. Your solution felt very imperative. Wrote a version that I feel is more idiomatic clojure. Thoughts.
https://gist.github.com/amithgeorge/7be270793576e2ba5b8f61184dabded3
Very nice and entertaining! I like your recursive get_led, but was scratching my head for a while before I realized that the extra params are used to find out which row we are at. For what it's worth I hacked the following:
(defn led-ize [chars]
(->> (range 3)
(map (fn [row]
(transduce (comp (map (fn [c] (get leds c)))
(map (fn [led] (nth led row))))
conj
chars)))
(interpose ["\n"])
(apply concat)))
(defn showLED-2 [num]
(do (print (apply str (led-ize (toList num)))) (symbol "")))
Thanks all for your comments...they are really appreciated -:) Not sure if I'm going to use Clojure in the near future...but I will take all your input in consideration...
Please your comments coming! -:D
Greetings,
Blag.
Development Culture.
Amith:
Yesterday was Sunday...so I just wanted to give a general response -:) Your code is really good and for sure way idiomatic...so thanks a lot for that -:)
I will for sure analyze your code and try to learn from it -:)
Greetings,
Blag.
Development Culture.
Jake:
Yep...a lot of things should have been done better...I agree...the problem for me is that usually I don't spent too much time learning a new language before I move into the next one...so you comments are really welcome...I will take them into account for next time -:)
Greetings,
Blag.
Development Culture.
JF:
Thanks :) As I said...this is the code I use every time I learn a new programming language...and I really don't spent too much time learning...so as soon as I can make it...I simply write the blog without digging deeper on the best options for each language...
Thanks for your code...very cool indeed -;)
Greetings,
Blag.
Development Culture.
Publicar un comentario