jueves, 19 de marzo de 2015

Real World OCaml - Book Review

I finally finished reading the Real World OCaml book...and I really enjoyed it -:)


This book, just like all the "Real World" series gives you a deep introduction into the language and allow you to start coding pretty fast...of course...if you have used any Functional programming language before...that really helps and OCaml has of course some aspects that makes it not so easy to learn...


The more you read this book, the more you're going to like OCaml...it's a really nice language -:)

The book covers the basics like Lists and Patterns, Records, Variants and Error-Handling.  But of course it goes beyond with Functors, Objects and Command-Line Parsing...not letting important concepts like JSON handling Concurrent Programming aside. So...it's a pretty complete reference to start out.

By the way...the book is 509 pages...so it's pretty long...but full of examples and demonstrations...

Here's a little code that I wrote to flat out lists -;)

Flat_List.ml
open Core.Std

let rec flat list =
 match list with
  | [] -> ""
  | head :: tail -> head ^ (flat tail)

let () = 
 printf "%s" (flat ["1";"2";"3"])

Here's the result...


As you can see...OCaml is fun...so go ahead...read this book and expand your knowledge -;)

Greetings,

Blag.
Development Culture.

lunes, 2 de marzo de 2015

LED is my new Hello World - OCaml Time

Learning OCaml is not easy...I have to admit that...but I'm moving forward -;) So, here's my take on LED Numbers...

LED_Numbers.ml
open Core.Std

let get_leds number =
let leds = [0, [" _  ";"| | ";"|_| "];
            1, ["  ";"| ";"| "];
            2, [" _  ";" _| ";"|_  "];
            3, ["_  ";"_| ";"_| "];
            4, ["    ";"|_| ";"  | "];
            5, [" _  ";"|_  ";" _| "];
            6, [" _  ";"|_  ";"|_| "];
            7, ["_   ";" |  ";" |  "];
            8, [" _  ";"|_| ";"|_| "];
            9, [" _  ";"|_| ";" _| "]] in
 for i = 0 to 2 do
  for j = 0 to String.length(number) - 1 do
   let line = List.Assoc.find_exn leds 
        (int_of_string(Char.to_string(number.[j]))) in
   printf "%s" (List.nth_exn line i)
  done;
  print_string "\n"
 done
 
let () =
 print_string "Enter a number: "
 let num = read_line() in
 get_leds num

The funny thing is that at first I tried to translate my previous Julia and Go codes...but then I remembered that even when OCaml can be made into some sort of an imperative language...it's actually Functional in its core...so I said...Ok...I need to reuse my Haskell code...but somehow I start thinking about a new whole different way of doing it...and I think I came out with a more cleaner and well done version...which is something that only learning a new programming language can give you...a new way of thinking about old problems -;)

Here are the screenshots as always -:)



Greetings,

Blag.
Development Culture.


martes, 24 de febrero de 2015

My first weekend with Amazon Echo

So...after a not so long wait...this Saturday, the Amazon Echo arrived home -:) After a weekend using it...here are my thoughts...

Being an Amazon Prime Member, I got a discount on the Echo, so I get it for $100...which is a pretty good deal...

The Echo comes in a nice black box...which makes it more elegant...


The Echo itself is of course...Black too -;)


It comes with a charger and a remote control...that I haven't used yet as Echo comes with enough microphones to hear you out even a long distances...


Setting it up was easy enough...in no time I had it on my network and the companion app was linked to it...


Of course, I tried the expected, like..."Alexa, what's the weather" or "Alexa, play some music"...I even when farther beyond as asking "Alexa, make me a sandwich"...which Alexa (the waking up word for Echo) kindly replied that she lack the skills for that...

When I told her "Alex, I'm hungry", she told me that I should get something to eat -:)

Of course...there are some things missing...like when I asked her "Alexa, where's the nearest Italian Restaurant"...where she replied to me that she didn't had enough information to answer my question...but...Echo is still young and in constant development...so it's all good in the hood -:)

If you want to see and hear more...here's a small video...



So far...I can only say that I love Echo...I can pair it with my phone via Bluetooth and stream music...I can ask it about Wikipedia facts...and many more things that I'm sure I haven't tried yet...

And of course, I have applied already to be able to code for it as soon as it's available...as always I have a lot of ideas in my mind...

Would I have bought it if it cost me $200 instead of $100? Sure...of course...after having for a weekend...I can't think of not having it -:) And I'm excited about all the possibilities that it can bring in the future...

Happy Echoing!

Greetings,

Blag.
Development Culture.

sábado, 21 de febrero de 2015

My first post on OCaml

After tasting some Functional Programming with Erlang and Haskell I knew it was time to keep moving forward -;) This time...I choose to learn OCaml -:D

OCaml is a multi-paradigm, imperative, functional, object oriented programming language.

Of course...I'm reading a book to learn about it -:) So will see my review as soon as I finish it...


My first impressions on OCaml is that it looks like Haskell...but with some differences...which makes it akward because I tend to code in Haskell but then realized that some things are quite different...anyway...OCaml seems like a really nice language so far and of course...it's not a pure as Haskell...

As the best way to learn is to code...I build a Fibonacci Sequence Generator...based of course of my previous Haskell code -;)

Fibonacci.ml
open Core.Std

let rec fib num a b = 
 match num with 
 | num when a > 0 && num > 1 -> string_of_int (a + b) ^ " " ^ fib (num - 1) (a+b) a
 | num when a = 0 -> string_of_int a ^ " " ^ string_of_int b ^ " " ^ 
                     string_of_int (a + b) ^ " " ^ fib (num - 1) (a+b) b
 | num -> ""
 
let () =
 print_string "Enter a number: "
 let num = read_int() in
 printf "%s" (fib num 0 1)

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



Greetings,

Blag.
Development Culture.

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.