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.

viernes, 8 de agosto de 2014

Bioinformatics with R Cookbook - Book Review

I just finished reading Bioinformatics with R Cookbook...so here's my review -:)



I was excited to read this book, because it's been a while since I read any R book...but...I gotta admit...this is not my kind of book...as I discovered that obviously I had minus one experience in Bioinformatics...

The book is not short but not long either...340 pages...and it's full of recipes...

It starts with a basic introduction to R, which should be appreciated by newbies...but for more season developers that just can be skipped out...

The are chapters dedicated to Sequence Analysis, Protein Structure Analysis and even Machine Learning in Bioinformatics...

Of course there's a lot of new packages that are used for the recipes and well as many interesting graphics...




If you have some knowledge of Bioinformatics...then you should for sure get this book...if you're not...well...you can buy it anyway...even if you don't understand anything...it is still a book about R and it's full of interesting codes...so you might end learning a bunch of new things -;)

The recipes are well explained and the result is always shown...which is good so we can know exactly what to expect...

Greetings,

Blag.
Development Culture.



miércoles, 6 de agosto de 2014

Decimal to Romans - Erlang Style

As I keep going with my Erlang enlightenment...I decided to try to make a Decimal to Roman Numbers application...as I have done with other programming languages in the past...and it wasn't that easy -:)

First...there's no such thing as associative arrays in Erlang...also...you can only assign variables once...on top of that there's no loops...but...who cares...functional programming is cool -;)

Of course...my code might be have done better...but...I'm learning...and by coding I learn more...so that's fine with me...

Here's why I wrote -;)

roman_numerals.erl
-module(roman_numerals).
-export([showRomans/1]).
-define(DECIMALS,{1000,900,500,400,100,90,50,40,10,9,5,4,1}).

showRomans(Number) when is_integer(Number) ->
 showRomans(Number,1,"").

showRomans(0,_,Acc) ->
 io:format("~s~n",[Acc]);
showRomans(Number,Counter,Acc) ->
 case Number >= element(Counter,?DECIMALS) of 
  true -> showRomans(Number - element(Counter,?DECIMALS),
         Counter,Acc ++ make_roman(element(Counter,?DECIMALS)));
  false -> showRomans(Number,Counter + 1,Acc)
 end.
  
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".

When we execute it...we can see that it works nicely -;)


As you can see...Erlang is all about functions, recursion, accumulators and a new way of thinking -:)

Greetings,

Blag.
Development Culture.

lunes, 4 de agosto de 2014

A trip down memory lane - Basic for Macintosh Plus

Many years ago...in the good old days...my first computer was a Macintosh Plus...


Of course...back then I didn't have a clue about programming...but...thanks to the wonderful world of the Internet I was able to get my hands on a Macintosh Plus Emulator called Mini vMac.

I got it a couple of years ago but barely used it...so today...I wanted to get the feeling of old programming by using...


Yep...Microsoft Basic 2.0 for the Mac...

This is how the environment of the Mini vMac looks like...using MacOS 7.0.1


As I have been using the LED example for many programming languages...I thought it would be a good idea to use it here as well...


The output doesn't look to good...but who cares -;)



Here's the source code in case you're interested -:D

LED
Dim Five$(2)
Dim Six$(2)
Dim Seven$(2)
Dim Eight$(2)
Dim Nine$(2)
Zero$(0) = " _  "
Zero$(1) = "| | "
Zero$(2) = "|_| "
One$(0) = "  "
One$(1) = "| "
One$(2) = "| "
Two$(0) = " _  "
Two$(1) = " _| "
Two$(2) = "|_  "
Three$(0) = "_  "
Three$(1) = "_| "
Three$(2) = "_| "
Four$(0) = "    "
Four$(1) = "|_| "
Four$(2) = "  | "
Five$(0) = " _  "
Five$(1) = "|_  "
Five$(2) = " _| "
Six$(0) = " _  "
Six$(1) = "|_  "
Six$(2) = "|_| "
Seven$(0) = "_  "
Seven$(1) = "  |"
Seven$(2) = "  |"
Eight$(0) = " _  "
Eight$(1) = "|_| "
Eight$(2) = "|_| "
Nine$(0) = " _  "
Nine$(1) = "|_| "
Nine$(2) = " _| "
INPUT "Enter a Number: ", Number$
NumLen = LEN(Number$)
FOR i = 1 to NumLen
Digit$ = MID$(Number$,i,1)
IF Digit$ = "0" THEN line1$ = line1$ + Zero$(0):line2$ = line2$ + Zero$(1):line3$ = line3$+Zero$(2)
IF Digit$ = "1" THEN line1$ = line1$ + One$(0):line2$ = line2$ + One$(1):line3$ = line3$+One$(2)
IF Digit$ = "2" THEN line1$ = line1$ + Two$(0):line2$ = line2$ + Two$(1):line3$ = line3$+Two$(2)
IF Digit$ = "3" THEN line1$ = line1$ + Three$(0):line2$ = line2$ + Three$(1):line3$ = line3$+Three$(2)
IF Digit$ = "4" THEN line1$ = line1$ + Four$(0):line2$ = line2$ + Four$(1):line3$ = line3$+Four$(2)
IF Digit$ = "5" THEN line1$ = line1$ + Five$(0):line2$ = line2$ + Five$(1):line3$ = line3$+Five$(2)
IF Digit$ = "6" THEN line1$ = line1$ + Six$(0):line2$ = line2$ + Six$(1):line3$ = line3$+Six$(2)
IF Digit$ = "7" THEN line1$ = line1$ + Seven$(0):line2$ = line2$ + Seven$(1):line3$ = line3$+Seven$(2)
IF Digit$ = "8" THEN line1$ = line1$ + Eight$(0):line2$ = line2$ + Eight$(1):line3$ = line3$+Eight$(2)
IF Digit$ = "9" THEN line1$ = line1$ + Nine$(0):line2$ = line2$ + Nine$(1):line3$ = line3$+Nine$(2)
NEXT i
PRINT line1$
PRINT line2$
PRINT line3$

The code might not be the most optimal...but what can you expect? It's basic...old basic -:P

Greetings,

Blag.
Development Culture.

viernes, 1 de agosto de 2014

Learn You Some Erlang for great good! - Book review

Today...just a couple of minutes ago...I finished reading Learn You Some Erlang for great good! and I must say...I'm both joyful and tired -:)


The book is huge...not kidding...it's "OMFG this book is huge"...with 628 pages...and lots...lots of examples...

This book is so good that it's actually considered part of the Erlang standard documentation...or something like that...or at least...that's how I think it should be considered...

The book starts with the basics of Erlang (the one's that make you brain hurt)...and then keeps moving forward introducing things like Concurrency, Multiprocessing, Supervisors, OTP, Event Handlers and more...much more...

Reading this book will give all the tools and knowledge necessary to start building applications...

If you have never heard of Erlang before and want to give it a try...this is totally the book for you...but...a word of warning...this book is full of content...that it could easily overwhelm you and make your brain hurt even more...

Now that I have finished the book...I think I will constantly come back to re-read some chapters as I need them...gladly...with some many examples it's easy to grasp most of the concepts at least in a slight way...



Of course...I'm still a newbie...so my next book (with review of course) it's going to be Programming Erlang (Software for a Concurrent World) written by Joe Amstrong (one the creators of Erlang)...

Oh...BTW...you can read Learn You Some Erlang for great good! online

Greetings,

Blag.
Development Culture.

viernes, 25 de julio de 2014

My first post on Erlang

So...a couple of days ago I decided (for the second time) to learn Erlang...but this time...I'm taking it serious -;)

But...what the hell is Erlang? Put it simple...it's a multi-paradigm, concurrent and functional programming language...simple, huh?

Erlang is a really nice and beautiful language...but...after this first couple of days...my brain is starting to hurt...I will let you know later why...

Anyway...I'm reading Learn You Some Erlang for Great Good...that can be read online here or bought here. I bought it -;)

So...you can expect a nice review as soon as I finish reading it...

As I usually do with new programming languages...I wanted to port something I have done before into Erlang...because for me...that's the best way to learn...

I took the code for an LED Output that I have built for both Python and Julia and ported to Erlang...read the blog here.

Here's the Erlang code...

led.erl
-module(led).
-export([showLED/0,showLED/1]).

printLines(Counter,Long,Lines,Digits,Line1,Line2,Line3)
 when Counter > Long ->
  [io:format("~s",[X]) || X <- Line1], io:format("~n"),
  [io:format("~s",[X]) || X <- Line2], io:format("~n"),
  [io:format("~s",[X]) || X <- Line3], io:format("~n");
  
printLines(Counter,Long,Lines,Digits,Line1,Line2,Line3)
 when Counter =< Long ->
 printLines(Counter+1,Long,Lines,Digits,
      Line1 ++ [element(1,element(1,X)) || {X,Y}<-Lines, Y == element(Counter,list_to_tuple(Digits))],
      Line2 ++ [element(1,element(2,X)) || {X,Y}<-Lines, Y == element(Counter,list_to_tuple(Digits))],
      Line3 ++ [element(1,element(3,X)) || {X,Y}<-Lines, Y == element(Counter,list_to_tuple(Digits))]).

showLED() ->
 io:format("You must enter a number!~n").
            
showLED(Number) ->
 Lines = [{{{" _  ",1},{"| | ",2},{"|_| ",3}},0},{{{"  ",1},{"| ",2},{"| ",3}},1},
   {{{" _  ",1},{" _| ",2},{"|_  ",3}},2},{{{"_  ",1},{"_| ",2},{"_| ",3}},3},
   {{{"    ",1},{"|_| ",2},{"  | ",3}},4},{{{" _  ",1},{"|_  ",2},{" _| ",3}},5},
   {{{" _  ",1},{"|_  ",2},{"|_| ",3}},6},{{{"_   ",1},{" |  ",2},{" |  ",3}},7},
   {{{" _  ",1},{"|_| ",2},{"|_| ",3}},8},{{{" _  ",1},{"|_| ",2},{" _| ",3}},9}],
 Line1 = "", Line2 = "", Line3 = "", Counter = 1,
 Numbers = integer_to_list(Number),
 Long = length(Numbers),
 Digits = lists:map(fun(X) -> X - 48 end, Numbers),
 printLines(Counter,Long,Lines,Digits,Line1,Line2,Line3).

and here's the output...


So...I'm going to tell you why my brain hurts...and I hope this doesn't make you stay away from Erlang...au contraire...you really should give Erlang a change -;)

First...Variables are invariable...which means...once A = 1 then A is going to be 1 forever and ever...no matter how many times you do A = 2...A is going to keep being 1...and of course you're going to get an error if you do A = 2...because A is 1...got it!

Second...No loops...no For's...no While's...no Do...While's...nothing...just recursion...functions that call themselves...multiple times until something makes them stop...

Third...String...are not strings...yep...no strings in Erlang...everything is integer...so in the end "A" is 65...

Forth...Object Orientation...doesn't even try it...no good...this is functional programming...forget about classes, methods, attributes, friends, templates...and so on...

Ok...why would you want to learn Erlang after all this non-sense? Easy...because Erlang changes your mindset completely...forgot everything you have ever learned...you need to think and do things in a completely different way...but believe me...after a couple of days with Erlang...you're going to be a better developer...that's guaranteed...

Greetings,

Blag.
Development Culture.

miércoles, 16 de julio de 2014

Web scrapping with Julia and PhatomJS

As I have been reading some PhantomJS books and I'm always looking to develop something nice using Julia...I thought that integrate them would be an awesome idea -;)

I thought about Twitter and the hashtags...wouldn't it be nice to write a PhantomJS script to webscrape Twitter and get all the hashtags that I have used?

For this particular script...I'm taking the hashtags from the first 5 Twitter pages linked to my profile...

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 this...we're going to have this output...



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

Let's see the Julia code...


Twitter_Hashtags.jl
tweets = readall(`phantomjs Hashtags.js Blag`)
tweets = split(tweets,"\n")
hashtags = Dict()
for hash in tweets
 try
  hashtags[hash] += 1
 catch e
  hashtags[hash] = 1
 end
end

filter!((k,v)->v>1,hashtags)

for (k,v) in hashtags
 println("$k has been mentioned $v times")
end

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


I still don't know how to sort Dicts in Julia...so bear with me -:)

Anyway...by looking at the output...we can have my top 3 hashtags -;)

#LeapMotion ==> 14 times
#Flare3D ==> 11 times
#DevHangout ==> 8 times

Hope you like this and see you next time -:)

Greetings,

Blag.
Development Culture.