miércoles, 1 de abril de 2015

LED is my new Hello World - Clojure Time

The more I learn Clojure...the more I like it...and I'm liking it so much that I couldn't help myself and start working on my beloved "LED_Numbers" application...

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

Wanna see it in action? Of course you want to -:)


Well...let's go back and keep learning -:D

Greetings,

Blag.
Development Culture.

8 comentarios:

Jake McCrary dijo...

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

Unknown dijo...

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

JF's Blogs dijo...

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

JF's Blogs dijo...
Este comentario ha sido eliminado por el autor.
Alvaro "Blag" Tejada Galindo dijo...

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.

Alvaro "Blag" Tejada Galindo dijo...

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.

Alvaro "Blag" Tejada Galindo dijo...

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.

Alvaro "Blag" Tejada Galindo dijo...

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.