jueves, 16 de octubre de 2014

Realm of Racket - Book Review

I just finished reading Realm of Racket a book about Racket -:)


The book has 316 pages...so it's a big book...

It starts with the basics of Rackets but very quickly jumps into creating games with Racket...which is something really cool...specially if you want to build games with Racket...

The book comes with some nice and funny comic strips telling us the story of Chad and how using Racket he manage to solve some interesting challenges...

One thing that I didn't like too much about the book...is that I believe that some games are too long and even complex which kind of defeat the purpose of being a beginners book...

Anyway...the book comes with a lot of useful information and will make you be right on track with your Racket learning -;)

You will learn about Recusion, Lambdas, Loops, GameUI and Networking...





Racket is a powerful but strange programming language...so this book will help you to get more comfortable...

If you want to learn Racket in a fun way and built some nice games in the process...then this book is for you -:)

Greetings,

Blag.
Development Culture.

miércoles, 15 de octubre de 2014

LED is my new Hello World - Racket Time

If you thought I was going to miss the change to build an LED Number Generator in Racket...you got it all wrong -;)

I have to admit...it wasn't easy...Racket is nice but weird...takes some time to get used to it...but when things work...they just work -;)

Here's the source code...

LED.rkt
#lang racket
(define (showLED num)
  (display(get_led(toList num num 0 1) 1 num)))

(define (toList num num_o start end)
  (cond [(number? num) (toList(number->string num) (number->string num_o) start end)]
        [(and (string? num) (> (string-length num_o) 1)) (append (cons(substring num start end) '())
                                                                (toList num (substring num_o 1 (string-length num_o)) 
                                                                        (add1 start)(add1 end)))]
        [(and (string? num) (= (string-length num_o) 1)) (append (cons(substring num_o 0 1) '()))]))

(define (get_led x n num)
  (cond [(> (length x) 0) (append (cons(make_led_digit (string->number(first x)) n) '()) (get_led(rest x) n num))]
        [(and (= (length x) 0) (< n 3)) (append '("\n") (get_led(toList num num 0 1) (add1 n) num) )]
        [(and (= (length x) 0) (>= n 3)) append '()]))

(define (make_led_digit a b)
  (cond [(and (= a 0)(= b 1)) " _  "]
        [(and (= a 0)(= b 2)) "| | "]
        [(and (= a 0)(= b 3)) "|_| "]
        [(and (= a 1)(= b 1)) "  "]
        [(and (= a 1)(= b 2)) "| "]
        [(and (= a 1)(= b 3)) "| "]
        [(and (= a 2)(= b 1)) " _  "]
        [(and (= a 2)(= b 2)) " _| "]
        [(and (= a 2)(= b 3)) "|_  "]
        [(and (= a 3)(= b 1)) "_  "]
        [(and (= a 3)(= b 2)) "_| "]
        [(and (= a 3)(= b 3)) "_| "]
        [(and (= a 4)(= b 1)) "    "]
        [(and (= a 4)(= b 2)) "|_| "]
        [(and (= a 4)(= b 3)) "  | "]
        [(and (= a 5)(= b 1)) " _  "]
        [(and (= a 5)(= b 2)) "|_  "]
        [(and (= a 5)(= b 3)) " _| "]        
        [(and (= a 6)(= b 1)) " _  "]
        [(and (= a 6)(= b 2)) "|_  "]
        [(and (= a 6)(= b 3)) "|_| "]
        [(and (= a 7)(= b 1)) "_   "]
        [(and (= a 7)(= b 2)) " |  "]
        [(and (= a 7)(= b 3)) " |  "]
        [(and (= a 8)(= b 1)) " _  "]
        [(and (= a 8)(= b 2)) "|_| "]
        [(and (= a 8)(= b 3)) "|_| "]
        [(and (= a 9)(= b 1)) " _  "]
        [(and (= a 9)(= b 2)) "|_| "]
        [(and (= a 9)(= b 3)) " _| "]))

Here's how it looks like when we run it...


Well...gotta keep learning...so...see you soon -;)

Greetings,

Blag.
Development Culture.

lunes, 13 de octubre de 2014

My first post on Racket

In my eternal quest for weird programming languages...I came out finding Racket...which is basically the new name of "PLT-Scheme"...from the Lisp family...


Racket can be installed on Windows, Mac and Linux...so it's pretty cool -:) And also comes with a nice IDE called DrRacket...


I gotta say...Racket is like nothing I have ever seen before...that's why I love it -:D

And here comes the most weird thing of all...Racket is...
  • Functional
  • Procedural
  • Modular
  • Object-Oriented
  • Logical
  • Reflective
  • Meta

Crazy, huh? Right now I'm reading an awesome book called "Realm of Racket"...


And after reading the first 61 pages (yep...no more) I was able to translate my Fibonacci Haskell app into Racket...so here goes the code...

Fibonacci.rkt
#lang racket
(define (showFib num)
  (fib num 0 1))

(define (fib num a b)
  ( cond [(and (> a 0) (> num 1)) (append (cons (+ a b) '()) (fib (sub1 num) (+ a b) a))]
         [(= a 0) (append (append (cons a (cons b '())) (cons (+ a b) '())) (fib(sub1 num)(+ a b) b))]
         [(= 1 num) (append '())]))


Now...if we run it...we're going to see this...


Cool, huh? Works just as expected -:) Couldn't be happier -;)

Greetings,

Blag.
Development Culture.

Robot Turtles - Game Review

Being a geek dad...I couldn't pass the opportunity to buy this game for my daughter -;)


Robot Turtles it's an awesome board game that aims to teach the fundamentals of  programming to little kids.

The game itself is simple...we have a board and place to place our turtle (the game is up to 4 players), we need to place a gem that the turtle needs to reach and here is the fun comes in...

We have cards that indicates the movements that the turtle needs to perform...move forward, move to the left or move to the right.

The kid needs to stack the cards in order to make the turtle move around the board and finally reach the gem in order to win the game.

That's the most basic and simple setup...more complex involves crates, ice blocks and even ice towers...the ice ones can be blasted with a special laser beam card, while the crate can be moved provided that there's enough space left so the crate can be set on a empty slot...

Once you get some movements that can be repeated...like...

Move forward 4 times, then turn left, move forward 4 times again...

The "Move forward 4 times" can be replace by a function card so in would end like...

Function Card, then turn left, Function card...

We also have some rock towers, that can't be blasted away or move, so the kids needs to go around it....


If something goes wrong...the kid can use a "bug" card to undo the last play and rebuild it...which in the end means...repeat the action with a new set of instructions...

The game itself is really nice...and once the kids get comfortable with the mechanics of the game, we can setup really complex scenarios where they need to think how to get the gem in the shortest time using the less possible cards...


I would say...what could be better to teach our young one the joy of programming by playing a nice board game at the same time?

If you got the chance...go ahead and but it...it's really lots of fun -:D

Greetings,

Blag.
Development Culture.

martes, 23 de septiembre de 2014

Web scrapping with Haskell and PhatomJS

Some time ago I wrote a blog called Web scrapping with Julia and PhantomJS...today...I wanted to do the same but using Haskell instead...

The concept is the same...we create a PhantomJS script that will read a "user" Twitter page and get the hashtags of the first 5 pages...here's the PhantomJS script...

Hashtags.js
var system = require('system');

var webpage = require('webpage').create();
webpage.viewportSize = { width: 1280, height: 800 };
webpage.scrollPosition = { top: 0, left: 0 };

var userid = system.args[1];
var profileUrl = "http://www.twitter.com/" + userid;

webpage.open(profileUrl, function(status) {
 if (status === 'fail') {
  console.error('webpage did not open successfully');
  phantom.exit(1);
 }
 var i = 0,
 top,
 queryFn = function() {
  return document.body.scrollHeight;
 };
 setInterval(function() {
  top = webpage.evaluate(queryFn);
  i++;
   
  webpage.scrollPosition = { top: top + 1, left: 0 };

  if (i >= 5) {
   var twitter = webpage.evaluate(function () {
    var twitter = [];
    forEach = Array.prototype.forEach;
    var tweets = document.querySelectorAll('[data-query-source="hashtag_click"]');
    forEach.call(tweets, function(el) {
     twitter.push(el.innerText);
    });
    return twitter;
   });

   twitter.forEach(function(t) {
    console.log(t);
   });

   phantom.exit();
  }
}, 3000);
});

If we run the script we're going to see the following output...


Now...what I want to do with this information...is to send it to Haskell...and get the most used hashtags...so I will summarize them and then get rid of the ones that only appear less than 5 times...

Let's see the Haskell code...

hashtags.hs
import System.Process
import Data.List

hashTags :: String -> IO()
hashTags(user) = do
 let x = readProcess "phantomjs" ["Hashtags.js",user] []
 y <- x
 mapM_ print $ sortBy sortGT $ count y

count :: String -> [(String,Int)]
count xs = filter ((>=5).snd) $ 
     map(\ws -> (head ws, length ws)) $ 
           group $ sort $ words xs

sortGT :: (Ord a, Ord a1) => (a1, a) -> (a1, a) -> Ordering
sortGT (a1, b1) (a2, b2)
  | b1 < b2 = GT
  | b1 > b2 = LT
  | b1 == b2 = compare a1 a2

When we run this code...we're going to have this output...


The nice thing about this app is that we can pass any username as parameter and the result is going to nicely ordered and filtered...another reason to love Haskell -;)

Greetings,

Blag.
Development Culture.

miércoles, 17 de septiembre de 2014

Learn You a Haskell for Great Good! - Book review

Finally! I have finished reading Learn You a Haskell for Great Good! and here's my review -;)

After learning Erlang, I decided it was a good idea to get hardcore and learn an even more "Pure" functional programming language...Haskell was of course my best shot...

This book is just pretty awesome...it has 404 pages...so it's a big book...

The examples are really nice and easy to follow...of course...once you get your head around Haskell and it's weird way of doing things...after a few headaches and curses...you found yourself loving the language...

This book will teach you about recursion, pattern matching, guards, monoids, and a handful of things that you couldn't find on a imperative language...



If you want to learn Haskell...this book is definitely for you...written with lots of humor and great examples it makes learning Haskell a real joy...I would recommend it a 100%...

If you haven't ever try a functional language...I will recommend you to do so...you will not regret it...you will become smart and a better developer...sure...you will get frustrated a thousand of times...but there's a price to pay when it comes to becoming better at something...

Haskell will help you to become better and this book will guide you through the hard path...

Greetings,

Blag.
Development Culture.

martes, 2 de septiembre de 2014

Decimal to Romans - Haskell Style

As promised...here's my Haskell take on Decimal to Romans...I got say...it took me considerably less time to build it Haskell that it took me to build it Erlang...but for sure...I had already done it in Erlang...so I had some sort of advantage -;)

Anyway...this was so much fun to do...and couldn't be happier with the overall process...Haskell being so pure is really a joy to work with...

Too much talk...here's the source code...

Roman_Numerals.hs
showRomans :: Int -> IO()
showRomans(num) = do
 putStr $ concat $ get_roman num 0

get_roman :: Int -> Int -> [[Char]]
get_roman num ctr
 | num >= roman = make_roman(roman) ++ get_roman(num - roman) ctr
 | num < roman && num > 0 = get_roman(num) (ctr+1)
 | num <= 0 = ["\n"]
 where roman = roman_keys [] !! ctr

make_roman :: Int -> [[Char]]
make_roman(1) = ["I"]; make_roman(4) = ["IV"]; make_roman(5) = ["V"];
make_roman(9) = ["IX"]; make_roman(10) = ["X"]; make_roman(40) = ["XL"];
make_roman(50) = ["L"]; make_roman(90) = ["XC"]; make_roman(100) = ["C"];
make_roman(400) = ["CD"]; make_roman(500) = ["D"]; make_roman(900) = ["CM"];
make_roman(1000) = ["M"]

roman_keys :: [Int] -> [Int]
roman_keys keys = [1000,900,500,400,100,90,50,40,10,9,5,4,1]

As usual...here's the screenshot...


After so many blogs in just a few days...I think I deserve a break, so I can keep reading the Haskell book...as I promised a review of it...

Greetings,

Blag.
Development Culture.

viernes, 29 de agosto de 2014

LED is my new Hello World - ABAP Time

It's been a long time since I did any ABAP development...so just for the good all times I decided to build the LED Number application on ABAP as well... -;)

I'm might be a little bit rusty...but I think I still remember most of the all tricks -:P

This goes out to all my ABAP friends...there are plenty -:)

ZLED
REPORT  zled.

TYPES: BEGIN OF ty_lines,
       line(1) TYPE c,
       index(1) TYPE c,
       map TYPE string,
       END OF ty_lines.

DATA: s_number TYPE string,
      counter TYPE i,
      num_counter TYPE i,
      line1 TYPE string,
      line2 TYPE string,
      line3 TYPE string.

DATA: t_lines TYPE STANDARD TABLE OF ty_lines.
FIELD-SYMBOLS: <fs_lines> LIKE LINE OF t_lines.

SELECTION-SCREEN BEGIN OF BLOCK params.
PARAMETERS: p_number TYPE i.
SELECTION-SCREEN END OF BLOCK params.

START-OF-SELECTION.
  PERFORM load_data.
  num_counter = 0.
  s_number = p_number.
  counter = strlen( s_number ) - 1.
  DO counter TIMES.
    READ TABLE t_lines ASSIGNING <fs_lines>
    WITH KEY line = 1
             index = s_number+num_counter(1).
    CONCATENATE line1 <fs_lines>-map INTO line1 SEPARATED BY space.
    READ TABLE t_lines ASSIGNING <fs_lines>
    WITH KEY line = 2
             index = s_number+num_counter(1).
    CONCATENATE line2 <fs_lines>-map INTO line2 SEPARATED BY space.
    READ TABLE t_lines ASSIGNING <fs_lines>
    WITH KEY line = 3
             index = s_number+num_counter(1).
    CONCATENATE line3 <fs_lines>-map INTO line3 SEPARATED BY space.
    num_counter = num_counter + 1.
  ENDDO.
  REPLACE ALL OCCURRENCES OF '%' IN line1 WITH ` ` IN CHARACTER MODE.
  REPLACE ALL OCCURRENCES OF '%' IN line2 WITH ` ` IN CHARACTER MODE.
  REPLACE ALL OCCURRENCES OF '%' IN line3 WITH ` ` IN CHARACTER MODE.
  WRITE:/ line1.
  WRITE:/ line2.
  WRITE:/ line3.

*&---------------------------------------------------------------------*
*&      Form  LOAD_DATA
*&---------------------------------------------------------------------*
FORM load_data.

  PERFORM add_lines USING '1' '0' '%_%%'.
  PERFORM add_lines USING '2' '0' '| |%'.
  PERFORM add_lines USING '3' '0' '|_|%'.
  PERFORM add_lines USING '1' '1' '%%'.
  PERFORM add_lines USING '2' '1' '|%'.
  PERFORM add_lines USING '3' '1' '|%'.
  PERFORM add_lines USING '1' '2' '%_%%'.
  PERFORM add_lines USING '2' '2' ' _|%'.
  PERFORM add_lines USING '3' '2' '|_%%'.
  PERFORM add_lines USING '1' '3' '_%%'.
  PERFORM add_lines USING '2' '3' '_|%'.
  PERFORM add_lines USING '3' '3' '_|%'.
  PERFORM add_lines USING '1' '4' '%%%%'.
  PERFORM add_lines USING '2' '4' '|_|%'.
  PERFORM add_lines USING '3' '4' '  |%'.
  PERFORM add_lines USING '1' '5' '%_%%'.
  PERFORM add_lines USING '2' '5' '|_ %'.
  PERFORM add_lines USING '3' '5' ' _|%'.
  PERFORM add_lines USING '1' '6' '%_%%'.
  PERFORM add_lines USING '2' '6' '|_%%'.
  PERFORM add_lines USING '3' '6' '|_|%'.
  PERFORM add_lines USING '1' '7' '_%%'.
  PERFORM add_lines USING '2' '7' '%|%'.
  PERFORM add_lines USING '3' '7' '%|%'.
  PERFORM add_lines USING '1' '8' '%_%%'.
  PERFORM add_lines USING '2' '8' '|_|%'.
  PERFORM add_lines USING '3' '8' '|_|%'.
  PERFORM add_lines USING '1' '9' '%_%%'.
  PERFORM add_lines USING '2' '9' '|_|%'.
  PERFORM add_lines USING '3' '9' ' _|%'.

ENDFORM.                    " LOAD_DATA

*&---------------------------------------------------------------------*
*&      Form  add_lines
*&---------------------------------------------------------------------*
FORM add_lines USING p_line p_index p_map.
  APPEND INITIAL LINE TO t_lines ASSIGNING <fs_lines>.
  <fs_lines>-line = p_line.
  <fs_lines>-index = p_index.
  <fs_lines>-map = p_map.
ENDFORM.                    "add_lines

Pics or it didn't happen -:)





Greetings,

Blag.
Development Culture.



LED is my new Hello World - R Time

Yep...as I have said many times before, the LED Number application has become my new Hello World...whenever I learn a new programming language I try to build this, because it compromises several language constructs and makes you learn more by trying to figure out how to build it again...

Yesterday I blog about how to build it using Haskell...and...I'm still learning Haskell...so I thought..."It's cool that I'm using this for my new programming languages...but what about the old ones?"

I suddenly realized that I had never wrote this using R...and that's really sad...I love R -:)

Anyway...here it goes -;)

LED.R
line<-c(1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3)
index<-c(0,0,0,1,1,1,2,2,2,3,3,3,4,4,4,5,5,5,6,6,6,7,7,7,8,8,8,9,9,9)
map<-c("  _ ","| | ","|_| ","  ","| ","| ","  _ "," _| ","|_  ",
       " _","_| ","_| ","    ","|_| ","  | ","  _ ","|_  "," _| ",
       "  _ ","|_  ","|_| "," _  "," |  "," |  ","  _ ","|_| ","|_| ",
       "  _ ","|_| "," _| ")

Lines<-cbind(line,index,map)
DF_Lines<-data.frame(Lines,stringsAsFactors=F)

line1<-""
line2<-""
line3<-""

p_number<-readline("Enter a number?: ")
l_number<-strsplit(p_number,"")
for(i in 1:nchar(p_number)){
  num_map<-DF_Lines[DF_Lines$index == as.numeric(l_number[[1]][i]),]
  line1<-paste(line1,num_map$map[1])
  line2<-paste(line2,num_map$map[2])
  line3<-paste(line3,num_map$map[3])
}
lines = paste(line1,"\n",line2,"\n",line3,"\n")
cat(lines)

Check the picture -;)


Greetings,

Blag.
Development Culture.

jueves, 28 de agosto de 2014

My first post on Haskell

So...as I get into functional programming thanks to Erlang...I decided to deep dive into Haskell...a pure functional programming language...

Of course...I'm reading a book for this...so when I finished it...which will take some time as I'm planning to start reading from the very first page again...soon you will read my review on Learn You a Haskell for Great Good...

Now...Haskell is hard...the hardest programming language I have ever dared to learn...and here's a couple of reasons why...
  • Variables are immutable...once assigned they cannot be re-assigned.
  • Functions only accept one parameter and one parameter only.
  • Functions can only call one function at a time.
  • There are no loops.
  • No side effects...everything is pure...a function will always return the same output

There's a really nice picture from XKCD explaining Haskell...


Anyway...Haskell makes you really forget everything you ever learn and make you learn everything again...for a developer like me...that's pure happiness -:)

So...I have done with already Ruby, Python, Julia and Erlang...I wanted my first Haskell application to be a LED Numbers application...so here goes the code...hang to you seat and watch this...BTW...I'm sure the code can be optimized...but I'm just a newbie...and my application works...so who cares about it -;)


LED.hs
showLED :: Int -> IO()
showLED(num) = do
 putStr(concat(addN(get_led(digits num) 1 num) (length(show num)) (length(show num))))

get_led :: [Int] -> Int -> Int -> [[Char]]
get_led (x:xs) n num = make_led_digit(x,n) ++ get_led(xs) n num
get_led [] n num = get_led2(digits num) (n+1) num

get_led2 :: [Int] -> Int -> Int -> [[Char]]
get_led2 (x:xs) n num = make_led_digit(x,n) ++ get_led2(xs) n num 
get_led2 [] n num = get_led3(digits num) (n+1) num

get_led3 :: [Int] -> Int -> Int -> [[Char]]
get_led3 (x:xs) n num = make_led_digit(x,n) ++ get_led3(xs) n num
get_led3 [] n num = []

digits :: Int -> [Int]
digits = map (read . (:[])) . show

addN :: [String] -> Int -> Int -> [String]
addN (x:xs) n ni
 | n > 0 = [x] ++ addN xs (n-1) ni
 | n == 0 = ["\n"] ++ [x] ++ addN xs (ni-1) ni
addN [] _ _ = [] ++ ["\n"]

make_led_digit :: (Int, Int) -> [[Char]]
make_led_digit(0,1) = [" _  "]; make_led_digit(0,2) = ["| | "]; make_led_digit(0,3) = ["|_| "];
make_led_digit(1,1) = ["  "];   make_led_digit(1,2) = ["| "];   make_led_digit(1,3) = ["| "];
make_led_digit(2,1) = [" _  "]; make_led_digit(2,2) = [" _| "]; make_led_digit(2,3) = ["|_  "];
make_led_digit(3,1) = ["_  "];  make_led_digit(3,2) = ["_| "];  make_led_digit(3,3) = ["_| "];
make_led_digit(4,1) = ["    "]; make_led_digit(4,2) = ["|_| "]; make_led_digit(4,3) = ["  | "];
make_led_digit(5,1) = [" _  "]; make_led_digit(5,2) = ["|_  "]; make_led_digit(5,3) = [" _| "];
make_led_digit(6,1) = [" _  "]; make_led_digit(6,2) = ["|_  "]; make_led_digit(6,3) = ["|_| "];
make_led_digit(7,1) = ["_   "]; make_led_digit(7,2) = [" |  "]; make_led_digit(7,3) = [" |  "];
make_led_digit(8,1) = [" _  "]; make_led_digit(8,2) = ["|_| "]; make_led_digit(8,3) = ["|_| "];
make_led_digit(9,1) = [" _  "]; make_led_digit(9,2) = ["|_| "]; make_led_digit(9,3) = [" _| "]

Here's a picture that show how it works...


I will keep learning Haskell and write another blog called "Decimals to Roman - Haskell Style"...stay tuned -;)

UPDATE!

Ok...I must face it...I couldn't leave the application like that knowing that I could make better...here's the new updated version -;)

LED_Fixed.hs
showLED :: Int -> IO()
showLED(num) = do
 putStr(concat(addN(get_led(digits num) 1 num) (length(show num)) (length(show num))))

get_led :: [Int] -> Int -> Int -> [[Char]]
get_led (x:xs) n num = make_led_digit(x,n) ++ get_led(xs) n num
get_led [] n num
 | n < 3 = get_led(digits num) (n+1) num
 | n == 3 = []

digits :: Int -> [Int]
digits = map (read . (:[])) . show

addN :: [String] -> Int -> Int -> [String]
addN (x:xs) n ni
 | n > 0 = [x] ++ addN xs (n-1) ni
 | n == 0 = ["\n"] ++ [x] ++ addN xs (ni-1) ni
addN [] _ _ = [] ++ ["\n"]

make_led_digit :: (Int, Int) -> [[Char]]
make_led_digit(0,1) = [" _  "]; make_led_digit(0,2) = ["| | "]; make_led_digit(0,3) = ["|_| "];
make_led_digit(1,1) = ["  "];   make_led_digit(1,2) = ["| "];   make_led_digit(1,3) = ["| "];
make_led_digit(2,1) = [" _  "]; make_led_digit(2,2) = [" _| "]; make_led_digit(2,3) = ["|_  "];
make_led_digit(3,1) = ["_  "];  make_led_digit(3,2) = ["_| "];  make_led_digit(3,3) = ["_| "];
make_led_digit(4,1) = ["    "]; make_led_digit(4,2) = ["|_| "]; make_led_digit(4,3) = ["  | "];
make_led_digit(5,1) = [" _  "]; make_led_digit(5,2) = ["|_  "]; make_led_digit(5,3) = [" _| "];
make_led_digit(6,1) = [" _  "]; make_led_digit(6,2) = ["|_  "]; make_led_digit(6,3) = ["|_| "];
make_led_digit(7,1) = ["_   "]; make_led_digit(7,2) = [" |  "]; make_led_digit(7,3) = [" |  "];
make_led_digit(8,1) = [" _  "]; make_led_digit(8,2) = ["|_| "]; make_led_digit(8,3) = ["|_| "];
make_led_digit(9,1) = [" _  "]; make_led_digit(9,2) = ["|_| "]; make_led_digit(9,3) = [" _| "]

Greetings,

Blag.
Development Culture.