miércoles, 3 de diciembre de 2014

Keep going with BeagleBone Black - Small Midi Piano

Looks like I'm on fire these days -;) I got somehow the plan of building new things on the BeagleBone Black as fast and often as I can...better way to learn for sure -:)

This time I have made a small Midi Piano...and excuse my total lack of Piano skills -:(


For this I used Python, AdaFruit_BBIO, PyGame and Timidity++.

Greetings,

Blag.
Development Culture.

martes, 2 de diciembre de 2014

DYI Simon Says - Using BeagleBone Black

Today...I was thinking what else could I built using the BeagleBone Black...and what came out was...making a Simon Says game! -:D


For this I simply used Python and AdaFruit_BBIO -:)

Of course...I need to add music and put a new color LED -;)

Greetings,

Blag.
Development Culture.

My first BeagleBone Black Project

As you may know...I started with the BeagleBone Black, almost a month ago...and of course...I have a project done already -;)

Sorry for the crappy quality of the video...but I shot it with my IPhone and uploaded to YouTube...


As you can see...it will play the Happy Birthday song while lighting the LEDs simulating a keyboard...not way cool...but it was a really good start for me -:)

I used Python, AdaFruit_BBIO, PyGame and Pyknon.

Greetings,

Blag.
Development Culture.

martes, 11 de noviembre de 2014

Starting up with the BeagleBone Black

I have always been a software guy...always immersed in programming and cool projects...but a couple of days ago I start playing with my first electronics toy...a BeagleBone Black...

I got myself The Ultimate BeagleBone Black Development Kit from Amazon which is really good to get you started...the only problem is that it comes with an PTBB-170B breadboard which is really small and doesn't really come with the ground and positive rails...so I have no clue how to use it...yet -:(



Good thing is...I work at the d-shop in SAP Labs Palo Alto...so we have plenty of regular and extra big breadboards -;)

Anyway...I started building up my first simple projects and using Adafruit BBIO on Python to code...which is really nice -:)





Of course...I still have a very long to go...but I have a couple ideas on my mind that could become really nice projects...let's see what becomes on my first adventure on electronics -;)

Greetings,

Blag.
Development Culture.

miércoles, 5 de noviembre de 2014

Web scrapping with Go and PhatomJS

Some time ago I wrote a blog called Web scrapping with Julia and PhantomJS...then I wrote another blog called Web scrapping with Haskell and PhantomJS...

This time...it's Go's time -;)

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 Go...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 Go code...

TwitterHashtags.go
package main

import ( "os/exec"
  "strings" 
  "fmt" )

func main() {
 cmd := exec.Command("phantomjs","--ssl-protocol=any","Hashtags.js", "Blag")
 out, err := cmd.Output()
 if err != nil {
  println(err.Error())
  return
 }
 
 Tweets := strings.Split(string(out), "\n")
 charmap := make(map[string]int)
 for _, value := range Tweets {
  if value != "" {
   charmap[value] += 1
  }
 }
 
 for key, value := range charmap {
  if value >= 5 {
   fmt.Print("(", key, ", ")
   fmt.Println(value, ")")
  }
 }
}

The only problem with this script is that there's not an easy way to sort a map[string]int...so I will simply leave it like that -:)

Here's the result...


If someone knows an easy way to sort this...please let me know -:)

Greetings,

Blag.
Development Culture.

miércoles, 29 de octubre de 2014

LED is my new Hello World - Go Time

As I keep learning Go, I'm learning more commands...so...as usual...here's my take on LED Numbers...maybe not the best code ever...but it works -:)

LED.go
package main

import ( "fmt"
   "strconv" 
   "strings" )

func main() {
 fmt.Print("Enter a number: ")
 var num int
 var list []string
 var line1, line2, line3 string
 fmt.Scanf("%d", &num)
 numList := strings.Split(strconv.Itoa(num), "")
 romans := map[string]string {
  "0" : " _  ,| | ,|_| ",
  "1" : "  ,| ,| ",
  "2" : " _  , _| ,|_  ",
  "3" : "_  ,_| ,_| ",
  "4" : "    ,|_| ,  | ",
  "5" : " _  ,|_  , _| ",
  "6" : " _  ,|_  ,|_| ",
  "7" : "_   , |  , |  ",
  "8" : " _  ,|_| ,|_| ",
  "9" : " _  ,|_| , _| ",
 }
 for _, value := range numList {
  list = strings.Split(romans[value],",")
  line1 += list[0]
  line2 += list[1]
  line3 += list[2]
 }
 fmt.Println(line1)
 fmt.Println(line2)
 fmt.Println(line3)
}

Here are the screenshots...



Greetings,

Blag.
Development Culture.

Mi first post on Go

As Go is gaining some popularity...I decided to give it a try...and sure...the fact that Ken Thompson is one of the three main designers of the language helped as well -:P

In order to learn I'm reading the book An Introduction to Programming in Go so expect a book review soon...



Now...my first impression of Go is that is basically C/C++ without the makeup...meaning...take out some nasty and weird features and you have yourself the Go programming language...

Go is supposed to be compiled, concurrent, imperative and structured.

Anyway...C++ was one of the first languages I ever learned so learning Go feels like going back home for a while...

For sure...the best way to learn a new language is to code something...so again I start by building something simple like the Fibonacci number generator based on Haskell code from another post...

Fibonacci.go
package main

import ( "fmt"
   "strconv" )

func main() {
 fmt.Print("Enter a number: ")
 var num int
 fmt.Scanf("%d", &num)
 fmt.Print(fib(num, 0, 1))
}

func fib(num int, a int, b int) string{
 var result string
 if a > 0 && num > 1 {
  result += strconv.Itoa(a+b) + " " + fib(num-1, a+b, a)
 } else if a == 0 {
  result = strconv.Itoa(a) + " " + strconv.Itoa(b) + 
                             " " + strconv.Itoa(a+b) + 
                             " " + fib(num-1, a+b, b)
 }
 return result
}

When we run it...we're going to see this -:)



Greetings,

Blag.
Development Culture.

How to Design Programs - Book Review

I just finished reading How to Design Programs, a book meant for beginners that want to get into the wonderful world of programming...and for that they use Scheme and DrScheme (which is now called Racket).



Of course, they don't focus on Scheme but on the design principles of programming...which makes this one a really important book.

The book itself is pretty big...with 565 pages...

There's a lot of really nice examples ranging from really basic to somehow complex, so there's a good learning curve attached to it.

Using Scheme as a starting point for learning programming is in my opinion a really smart choice...the syntax might be kind of weird sometimes but the good thing is that there are not so many keywords so learning it it's not a hard task...



Even if you have experience in programming...this book is really helpful and it goes beyond the simple act of programming as it teaches you how to design, build and test your application...making it more robust and less error prone...

If you have the chance...read this book...you will not regret it -;)

Greetings,

Blag.
Development Culture.

lunes, 27 de octubre de 2014

LeapTV - Game System Review

I have always love LeapFrog so when I read that we're planning to release a brand new gaming console...I couldn't stop myself from pre-ordering it -;)

LeapTV is in my opinion...a mix between Education, Wii and XBox Kinnect...just enough to keep our little ones engaged, exercised, happy and practice their school skills...


The console comes with a control and a Kinnect like sensor...but no cartrige games...however...it includes a "Pet Play World" game when you register it online...it's a nice game where you need to pick a pet and take care of him/her -;)

Of course...I couldn't left my daughter with just one included game...so I bought a couple more -:)


Kart Racing Supercharged is a "Mario Kart" like game where the kids needs to solve some math questions in order to get better equipment for their cars...

Sports! includes nine games like Super Goallie, Skateboarding and Bowling...and here kids needs to use their addition, subtraction, patterns and shapes skills...


The un-boxing and setup was of course pretty easy and the controller is wireless...


This picture is from the "Pet Play World" game...


And here's the skateboarding game on Sports!


Kart Racing Supercharged is really nice too -;)


I can say...LeapFrog and LeapTV haven't disappoint me at all...this is a great gaming console for little kids and they will open their App Store soon...so the fun will never end -;)

Greetings,

Blag.
Development Culture.

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" ["--ssl-protocol=any","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.

viernes, 22 de agosto de 2014

Programming Erlang Software for a Concurrent World - Book Review

I just finished reading Programming Erlang Software for a Concurrent World by Joe Armstrong...the creator of Erlang.

Nothing better that reading about a language, from the mouth of it's creator -:)


The book is huge with 519 pages...but every single page is worth reading...full of nice, funny and well explained examples, Mr. Armstrong makes you love Erlang -:)

Learn about Sequential Programming, Concurrent Programming, Distrubuted Programming, Sockets, DETS, OTP and Mnesia...among others...




This book is just great and I have learned so much...of course...while I'm still an Erlang newbie..I have enough knowledge to build small applications and have a lot of fun -;)

What I like about the book is that the approach is very light and casual...the example are not too easy or too hard, so using them to learn is just right...

Erlang is a language is that more than worth learning...and this book really does the job of hooking you up and getting ready for more...

Greetings,

Blag.
Development Culture.