miércoles, 9 de septiembre de 2015

Firing Up (BeagleBone Black, WebCam, Missile Launcher and OpenCV)

This post was originally posted on Firing Up (BeagleBone Black, WebCam, Missile Launcher and OpenCV).



If you are an SAP Employee, please follow us on Jam.


My good friend John Astill came to visit us at d-shop some time ago…and he saw the Thunder Missile Launcher that I bought as a Christmas present for my team dinner. He told me…”Blag…you should build that app that fires up missiles when you put a color or object in front of the camera”…that got me thinking of course -;)

So John…this blog is for you 

So, let’s start…

You will need OpenCV installed…so the regular way should work…

sudo apt-get install opencv

And OpenCV should be installed and ready to use now…

Next…I needed to hack the Missile Launcher in order to be able to make it fire…so this is where the fun begins…I hooked the Missile Launcher to my Linux box on VMWare and made a lsusb -vv command to get the information about the USB device…


Here we can see that the IdVendor is 0x2123 and the idProduct is 0x1010. This doesn’t really give us anything as we don’t know to pass the information we need in order to make it work…so next step was to sniff the device in order to get the USB commands…I did this using SnoopyPro a USB Sniffer Tools for Windows…and just notice that I couldn’t make it work properly on Windows 7, so I need to use my old and nice Windows XP VMWare Image…and install the Dream Cheeky software...


Once we have SnoopyPro running, need to connect the Missile Launcher and click on File --> Unpack Drivers…


Next, we need to look for the correct IdVendor and IdProduct and right-click to select Install and Restart.


This will allow us to log everything that happens while using the device. By default, at least for the Missile Launcher…it will log 14 lines that don’t matter much to us…but when we hit for example the Up button…then we get more interesting info…


This is basically saying that to make the launcher move up, we need to pass the following…

0x02 0x02 0x00 0x00 0x00 0x00 0x00 0x00

Now…the part that interests us is the Fire action…so let’s explore it…


This one is

0x02 0x10 0x00 0x00 0x00 0x00 0x00 0x00

Cool…now we’re getting closer…but…there’s a piece that it’s missing…how do we send this command? Easy…just take a look at the Transfer command…


This command tells us that we need to pass the information like this…

0x21 0x09 0 0

Followed by the action we want to perform…

In order to use the device on our BeagleBone…we need to install something first…

pip install pysub

This library will allow us to connect and more important, send commands to our device.

As we’re going to use a Camera and the Missile Launcher both at the same time…we need a USB Hub that it’s powered…otherwise, it will never work…I got this one from Amazon.

So…power up the USB Hub and connect both WebCam and Missile Launcher, connect the Hub to your BeagleBone Black and then connect the BeagleBone to your laptop…

Use Nano or any other editor of your choice and create a file called Thunder.py

Thunder.py
import cv2
import numpy as np
import usb.core

cam = cv2.VideoCapture(-1)
cam.set(3, 960)
cam.set(4,720)
s, img = cam.read()

dev = usb.core.find(idVendor=0x2123,idProduct=0x1010)
try:
            dev.detach_kernel_driver(0)
except Exception, e:
            pass

winName = "Thunder"
cv2.namedWindow(winName, cv2.CV_WINDOW_AUTOSIZE)
while s:
        s, img = cam.read()
        key = cv2.waitKey(10)
        if key == 27:
                cv.destroyWindow(winName)
                break
        hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
              lower_green = np.array([38, 100, 50])
              upper_green = np.array([75, 255, 255])

        mask = cv2.inRange(hsv, lower_green, upper_green)
        res = cv2.bitwise_and(img,img, mask=mask)
        cv2.imshow(winName, res)

        if cv2.countNonZero(mask) > 61000:
                dev.ctrl_transfer(0x21, 0x09, 0, 0,
                                  [0x02, 0x010, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00])


This code will basically open up the camera connection, and turn the display black so only the color that we want to track is shown. In this case, we want to fire up the missile launcher only when we see something “green”. As you can see we’re passing the transfer command as single parameters but the fire command as an array.

Now…the BeagleBone doesn’t have a display…so you could attach a monitor and a keyboard or you could do something more interesting and smart…

We’re going to install Tight VNC Server

sudo apt-get install tightvncserver

The first time you run it, it will ask you for a password for the current user, so go ahead and provide one…

And it will start running…so you will be able to open a Remote Desktop connection to your BeagleBone…now you only need a client to connect to it...

If you’re using Windows you can download the TightVNC Client from here…

http://www.tightvnc.com/download/2.7.10/tightvnc-2.7.10-setup-64bit.msi

http://www.tightvnc.com/download/2.7.10/tightvnc-2.7.10-setup-32bit.msi

If you’re using Linux you can simply download it using apt-get install like this…

sudo apt-get install xtightvncviewer

Run it on the terminal

Just provide the IP for your BeagleBone which should be 192.168.7.2:1 on the small box that will appear, provide the password that you setup for the VNCServer on the next screen and you’re all set…




If you’re using Windows…simply double click the tightvnc-jviewer.jar file and enter the following information…




In order to test this, we need to simply print a nice green box…


Then we can run the app…


And put the green box in front of the camera -;)


Sorry for the flaky picture…but the Missile Launcher was firing off and I had my phone on one hand and the green box paper in the other one -;)

Greetings,

Blag.
Development Culture.

Nest Thermostat and R - Creating a Shiny dashboard

This post was originally posted on Nest Thermostat and R - Creating a Shiny dashboard.



If you are an SAP Employee, please follow us on Jam.

At the d-shop we're always looking forward to play with the latest technologies...a while the Nest Thermostat is not really "new", we're in the process of getting one.

For now, we are happy to be able to play a little bit with Nest Home Simulator available on the Google Chrome Store.

In this blog, we're going to use the Nest Home Simulator, the Statistics Programming Language R and the Shiny package, which allows us to create awesome web interfaces to manage data. Also, we're going to need the RJson package.

First step will be to create an account on the Nest Developer Program and the install the Nest Home Simulator.


Wen we log into the Simulator, we are going to be able to create a Thermostat to start playing around with the current and target temperatures...


The second important thing, is that when we are log into the Nest Developer Program we're going to be able to create a client which will access our thermostat and also, we will going to able to generate a unique number to communicate with it.


We need to just copy and paste the URL from the Authorization URL and after accepting the conditions a number will be presented...we need this number for our R code...but keep in mind that once the connection is closed, we will need to generate a new number...


Now, we're ready to add some code to the mix...when working with R, is always better to use RStudio...so copy and paste this code...and remember to replace the code variable with your own number...also you will need to replace the Client ID and Client Secret with the information from your Nest Developer Program Client code...

Nest_Dashboard.R
library("shiny")
library("rjson")

if(!exists("access_code")){
  code<-"XXX"
  info_data<-data.frame(Humidity=integer(),
  Current_Temperature=integer(),Target_Temperature=integer(),Times=character())
  access_code<-system(sprintf('curl -X POST 
  "https://api.home.nest.com/oauth2/access_token?client_id=XXX&code=%s&
  client_secret=XXX&grant_type=authorization_code"',code),intern=T)
  document<-fromJSON(access_code)
  access<-document$access_token
  devices<-paste("https://developer-api.nest.com/devices?auth=",access,sep="")
  all_devices<-system(sprintf('curl -L -X GET -H "Accept: application/json" "%s"',devices),intern=T)
  device<-gsub("thermostats|:|{|\"|humidity.*","",all_devices,perl=T)
  themostat_code<-paste("https://developer-api.nest.com/devices/thermostats/",device,"?auth=",access,sep="")
}

get_nest_info = function(info_data){
    thermostat<-system(sprintf('curl -L -X GET -H "Accept: application/json" "%s"',themostat_code),intern=T)
    info<-fromJSON(thermostat)
    timeframe<-Sys.time()
    timeframe<-gsub("\\d+-\\d+-\\d+\\s","",timeframe)
    new_data<-data.frame(Humidity=info$humidity,Current_Temperature=info$ambient_temperature_f,
    Target_Temperature=info$target_temperature_f,Times=timeframe,stringsAsFactors = F)
    if(nrow(info_data)==10){
      info_data<-info_data[-1,]
    }
    info_data<-rbind(info_data,new_data)
  return(info_data)
}

runApp(list(
  ui = pageWithSidebar(    
    
    headerPanel("Nest Dashboard"),
    
    sidebarPanel(
    ),
    
    mainPanel(
      plotOutput("nestPlot")
    )
  ),
  server =function(input, output, session) {
    autoInvalidate <- reactiveTimer(10000, session)
      output$nestPlot <- renderPlot({
        autoInvalidate()
        info_data<<-get_nest_info(info_data) 
        plot(info_data$Current_Temperature,type="n",axes=F,ann=F)
        lines(info_data$Current_Temperature,col="blue")
        lines(info_data$Target_Temperature,col="red")
        points(info_data$Current_Temperature, pch=21, bg="lightcyan", cex=1.25)
        points(info_data$Target_Temperature, pch=21, bg="lightcyan", cex=1.25)
        box()
        xy<-length(info_data$Times)
        axis(2, col.axis="blue", las=1)
        axis(1, at=1:xy, lab=info_data$Times, col.axis="purple")
        legend("topright",c("Current Temp","Target Temp"),lty=c(1,1),col=c("blue","red"),bty="n")
    })
  }
))

This code will get 10 readings until it refreshes itself to get 10 new ones. So we can update the Current and Target Temperature values in order to see the Dashboard changing...



Greetings,

Blag.
Development Culture.

lunes, 10 de agosto de 2015

LED is my new Hello World - Mercury Time

This one was a little bit tricky and longer than I expected...it also...forced me to spend a lot of time reading the documentation as there aren't many tutorials or books about Mercury...

Anyway...in the end it's working fine, so I'm quite happy -:)

Mercury has a lot of pretty convenient functions...you just need to find them out -;)

led_numbers.m
:- module led_numbers.
:- interface.
:- import_module io.

:- pred main(io::di, io::uo) is det.

:- implementation.
:- import_module list, string, int, array, char.

:- pred get_leds(list.list(character)::in, list.list(character)::in, 
                 int::in, list.list(string)::out ) is det.
 
get_leds(LDIGITS, LDIGITS_AUX, N, RESPONSE) :-
 (
  LEDS = array([array([" _  ","| | ","|_| "]),
                array(["  ","| ","| "]),
                array([" _  "," _| ","|_  "]),
                array(["_  ","_| ","_| "]),
                array(["    ","|_| ","  | "]),
                array([" _  ","|_  "," _| "]),
                array([" _  ","|_  ","|_| "]),
                array(["_   "," |  "," |  "]),
                array([" _  ","|_| ","|_| "]),
                array([" _  ","|_| "," _| "])
               ]),
  list.length(LDIGITS,LEN),
  ( if LEN > 0 then
   HEAD = det_head(LDIGITS),
   TAIL = det_tail(LDIGITS),
   char.to_int(HEAD, HEAD_I:int),
   HEAD_N:int = HEAD_I - 48,
   LINE = elem(HEAD_N, LEDS),
   SUB_LINE = elem(N, LINE),
   get_leds(TAIL, LDIGITS_AUX, N, RESULT),
   RESPONSE = [SUB_LINE] ++ RESULT
    else if N < 2 then
   get_leds(LDIGITS_AUX, LDIGITS_AUX, N+1, RESULT),
   RESPONSE = ["\n"]  ++ RESULT
    else if N = 2 then
   RESPONSE = ["\n"]
    else
   RESPONSE = [] )
 ). 
 
main(!IO) :-
 io.write_string("Enter a number: ",!IO),
 io.read_line_as_string(Result, !IO),
 ( if
   Result = ok(String),
   NUM = string.strip(String),
   to_char_list(NUM,LDIGITS),
   get_leds(LDIGITS, LDIGITS, 0, RESPONSE)
  then
   io.write_string(string.join_list("", RESPONSE), !IO)
  else
   io.write_string("Not a number...",!IO)
 ).


Anyway...here are the screens -;)



Hope you like it -;)

Greetings,

Blag.
Development Culture.

miércoles, 5 de agosto de 2015

My first post on Mercury

So...my new language for a couple of weeks...is Mercury -:)

You may ask...what is Mercury?

Mercury looks like Prolog, but feels like strict Haskell or pure OCaml.
 In other words is a logical, functional and object-oriented programming language...

As always...I needed to develop an app to learn how to use it...and while it gave me more than one headache...here it is -;)

fibo.m
:- module fibo.
:- interface.
:- import_module io.

:- pred main(io::di, io::uo) is det.

:- implementation.
:- import_module string, int.

:- pred fibo(int::in, int::in, int::in, string::out) is det.

fibo(NUM, A, B, FIBS) :-
 ( 
  if A = 0 
  then fibo(NUM-1,A+B,B,FIB), FIBS = int_to_string(A) ++ " " ++ int_to_string(B) ++ 
                                     " " ++ int_to_string(A+B) ++ " " ++ FIB
  else if A > 0, NUM > 1
  then fibo(NUM-1,A+B,A,FIB), FIBS = int_to_string(A+B) ++ " " ++ FIB
  else FIBS = ""
 ).
 
main(!IO) :-
 io.write_string("Enter a number: ",!IO),
 io.read_line_as_string(Result, !IO),
 ( if
   Result = ok(String),
   string.to_int(string.strip(String), N)
  then
   fibo(N,0,1,FIBS),
   io.write_string(FIBS,!IO)
  else
   io.write_string("Not a number...",!IO)
 ).

The cool thing about Mercury is that it forces you to not allow your app to fail...as the "else" is mandatory...if you enter a number then it's fine...but if you enter a letter, you are forced to do something...

Here are the pics...



 Now...I just need to break my head a little bit more trying to make my LED_Numbers app -:D

Greetings,

Blag.
Development Culture.

viernes, 31 de julio de 2015

LED is my new Hello World - Zonnon Time

You knew this was coming...don't you? -;)

So here...I took another approach because Zonnon doesn't have a built-in split command...and also because I'm not sure how to create an array of arrays and because I couldn't make my procedure return an array and then assign it to another array -:(

Still...I'm quite happy with the result -:D

LED_Numbers.znn
module LED_Numbers;

var num, text, concat:string;
var leds:array 10 of string;
var led:array 3 of string;
var i, j, z, len, lenled:integer; 

begin
 leds[0]:=" _  ,| | ,|_| ,";
 leds[1]:="  ,| ,| ,";
 leds[2]:=" _  , _| ,|_  ,";
 leds[3]:="_  ,_| ,_| ,";
 leds[4]:="    ,|_| ,  | ,";
 leds[5]:=" _  ,|_  , _| ,";
 leds[6]:=" _  ,|_  ,|_| ,";
 leds[7]:="_   , |  , |  ,";
 leds[8]:=" _  ,|_| ,|_| ,";
 leds[9]:=" _  ,|_| , _| ,";
 write("Enter a number: ");readln(num);
 len:=num.Length;
 for j:=0 to len - 1 do
  i:=0;
  text:=leds[integer(num[j])-48];
  lenled:=text.Length;
  for z:= 0 to lenled - 1 do
   if text[z] # "," then
    concat:= concat + string(text[z]);
   else
    led[i]:=led[i] + concat;
    concat:="";
    i:= i + 1;
   end;
  end;
 end; 
 writeln(led[0]);
 writeln(led[1]);
 writeln(led[2]);
end LED_Numbers.

Anyway...here are the pictures -;)



Greetings,

Blag.
Development Culture.

My first post on Zonnon

As always...I was looking for new and cool programming languages to learn...and then Mark Teichmann came up with something really nice -;) The Zonnon programming language...

So...what's Zonnon? Zonnon is a general-purpose programming language in the Pascal, Modula-2 and Oberon family...and it's run on .NET/Mono...

It's been a really long time since I used Pascal or Delphi...but...just like riding a bicycle...the knowledge came back as soon I started writing some code -;)

Now...the only problem I have with Zonnon...is that's there's almost no documentation...and what's available is in Russian...so...a lot Google helped me up...


Here's the Fibonacci Numbers app...


Fibo.znn
module Fibo;
var num: integer;
var a, b: integer;

procedure fib (var num, a, b : integer): string;
var result: string;
var ab: integer;
begin
 result := "";
 if (a > 0) & (num > 1) then
  num := num - 1;
  ab := a + b;
  result := result + string(ab) + " " + fib(num, ab, a);
 elsif a == 0 then
  num := num - 1;
  ab := a + b;
  result := string(a) + " " + string(b) + " " + string(ab) + " " + fib(num, ab, b);
 end;
 return result;
end fib;

begin
 a := 0;
 b := 1;
 write("Enter a number: ");readln(num);
 writeln(fib(num,a,b));
 writeln("");
end Fibo.

And a couple of pictures...




I need to keep playing and exploring Zonnon...there's a lot of cool things still hiding from me -;)

Greetings,

Blag.
Development Culture.

jueves, 23 de julio de 2015

LED is my new Hello World - Forth Time

Just as you expected...it's time again for my LED Numbers application...this time...using Forth -:)

The code is a little bit longer that I would have expected...but I guess it's either because I'm still a Forth newbie...or because Forth being a stack based programming language has it's own way of dealing with this kind of application...

Here's the source code...enjoy! -;)

LED_Numbers.fth
VARIABLE picky

: COLLECT_PICKY DUP 1- picky ! ;

: NPICK COLLECT_PICKY 2 * 0 ?DO picky @ PICK LOOP ;

: SPLIT BEGIN DUP WHILE 10 /MOD REPEAT DROP DEPTH ;

: First_Line
  CASE
  0 OF SPACE ." _  " ENDOF 1 OF SPACE SPACE ENDOF
  2 OF SPACE ." _  " ENDOF 3 OF ." _  " ENDOF
  4 OF SPACE SPACE SPACE SPACE ENDOF 5 OF SPACE ." _  " ENDOF
  6 OF SPACE ." _  "ENDOF 7 OF ." _   "  ENDOF 
  8 OF SPACE ." _  " ENDOF 9 OF SPACE ." _  " ENDOF
  ENDCASE ;
  
: Second_Line
  CASE
  0 OF ." | | " ENDOF 1 OF ." | " ENDOF
  2 OF SPACE ." _| " ENDOF 3 OF ." _| " ENDOF
  4 OF ." |_| " ENDOF 5 OF ." |_  " ENDOF
  6 OF ." |_  "ENDOF 7 OF SPACE ." |  "  ENDOF 
  8 OF ." |_| " ENDOF 9 OF ." |_| " ENDOF
  ENDCASE ;
  
: Third_Line
  CASE
  0 OF ." |_| " ENDOF 1 OF ." | " ENDOF
  2 OF ." |_  " ENDOF 3 OF ." _| " ENDOF
  4 OF SPACE SPACE ." | " ENDOF 5 OF SPACE ." _| " ENDOF
  6 OF ." |_| "ENDOF 7 OF SPACE ." |  "  ENDOF 
  8 OF ." |_| " ENDOF 9 OF SPACE ." _| " ENDOF
  ENDCASE ;

: LED 
  SPLIT
  NPICK
  DEPTH 3 / 0 ?DO First_Line LOOP CR
  DEPTH 2 / 0 ?DO Second_Line LOOP CR
  DEPTH 0 ?DO Third_Line LOOP ;

And of course...you want to see an image...so here it goes -;)


Hope you like it... -:D

Greetings,

Blag.
Development Culture.

martes, 21 de julio de 2015

My first post on Forth

So...after a very long time...I finally decided to learn Forth...and if I thought I have dealt with difficult programming languages before...I was like Jon Snow...I knew nothing -:(

Forth is a stack based programming language...meaning that you program using a stack with the LIFO functionality...Last In - First Out...

Something like this...you enter 1 2 3 and Forth will output it as 3 2 1...weird, huh?

Of course...I started to read a book...and unlike most other times...I have finish it a long time ago...but with Forth...one book is not just enough...I have been reading and coding like crazy...

The book is Starting Forth by Leo Brodie...


The book itself is really good...it's 166 pages so it's not very long and gives you a nice introduction to the world of Forth...of course...as I said...one book is not enough...and even when this book is really good and highly recommended...Forth is very difficult to grasp but learning it is just amazing -:)

It took me a very long time but finally I was able to come with an example of my already "famous" Fibonacci Numbers...

Fibo.fth
: FIBO 
   1 SWAP 
   0 DUP . 
   ?DO 
      DUP . OVER OVER + ROT DROP 
   LOOP 
   DROP DEPTH 
   DEPTH 2 = IF 2DROP 0 THEN 
;

I told you...Forth is not by any chance and easy language...and actually I wrote the code in just one line...but put it here in different lines to make more clear...

Let's see an image...


I'm sure I going to spend quite more time with Forth...as I feel that's there so much to learn...if you like what you see...learn it too -;) Otherwise...look for something less weird -:)

BTW...I'm using pForth...I used gForth when I first started but I think pForth is more Forth complaint...

Greetings,

Blag.
Development Culture.

lunes, 29 de junio de 2015

Exploring SparkR

A colleague from work, asked me to investigate about Spark and R. So the most obvious thing to was to investigate about SparkR -;)

I installed Scala, Hadoop, Spark and SparkR...not sure Hadoop is needed for this...but I wanted to have the full picture -:)

Anyway...I came across a piece of code that reads lines from a file and count how many lines have a "a" and how many lines have a "b"...

For this code I used the lyrics of Girls Not Grey by AFI...

SparkR.R
library(SparkR)

start.time <- Sys.time()
sc <- sparkR.init(master="local")
logFile <- "/home/blag/R_Codes/Girls_Not_Grey"
logData <- SparkR:::textFile(sc, logFile)
numAs <- count(SparkR:::filterRDD(logData, function(s) { grepl("a", s) }))
numBs <- count(SparkR:::filterRDD(logData, function(s) { grepl("b", s) }))
paste("Lines with a: ", numAs, ", Lines with b: ", numBs, sep="")
end.time <- Sys.time()
time.taken <- end.time - start.time
time.taken



0.3167355 seconds...pretty fast...I wonder how regular R will behave?

PlainR.R
library("stringr")

start.time <- Sys.time()
logFile <- "/home/blag/R_Codes/Girls_Not_Grey"
logfile<-read.table(logFile,header = F, fill = T)
logfile<-apply(logfile[,], 1, function(x) paste(x, collapse=" "))
df<-data.frame(lines=logfile)
a<-sum(apply(df,1,function(x) grepl("a",x)))
b<-sum(apply(df,1,function(x) grepl("b",x)))
paste("Lines with a: ", a, ", Lines with b: ", b, sep="")
end.time <- Sys.time()
time.taken <- end.time - start.time
time.taken


Nice...0.01522398 seconds...wait...what? Isn't Spark supposed to be pretty fast? Well...I remembered that I read somewhere that Spark shines with big files...

Well...I prepared a file with 5 columns and 1 million records...let's see how that goes...

SparkR.R
library(SparkR)

start.time <- Sys.time()
sc <- sparkR.init(master="local")
logFile <- "/home/blag/R_Codes/Doc_Header.csv"
logData <- SparkR:::textFile(sc, logFile)
numAs <- count(SparkR:::filterRDD(logData, function(s) { grepl("a", s) }))
numBs <- count(SparkR:::filterRDD(logData, function(s) { grepl("b", s) }))
paste("Lines with a: ", numAs, ", Lines with b: ", numBs, sep="")
end.time <- Sys.time()
time.taken <- end.time - start.time
time.taken



26.45734 seconds for a million records? Nice job -:) Let's see if plain R wins again...

PlainR.R
library("stringr")

start.time <- Sys.time()
logFile <- "/home/blag/R_Codes/Doc_Header.csv"
logfile<-read.csv(logFile,header = F)
logfile<-apply(logfile[,], 1, function(x) paste(x, collapse=" "))
df<-data.frame(lines=logfile)
a<-sum(apply(df,1,function(x) grepl("a",x)))
b<-sum(apply(df,1,function(x) grepl("b",x)))
paste("Lines with a: ", a, ", Lines with b: ", b, sep="")
end.time <- Sys.time()
time.taken <- end.time - start.time
time.taken


48.31641 seconds? Look like Spark was almost twice as fast this time...and this is a pretty simple example...I'm sure that when complexity arises...the gap is even bigger...

And sure...I know that a lot of people can take my plain R code and make it even faster than Spark...but...this is my blog...not theirs -;)

I will come back as soon as I learn more about SparkR -:D

UPDATE

So...I got a couple of comments claiming that read.csv() is too slow...and I should measuring the process not the loading of an csv file...while I don't agree...because everything is included in the process...I did something as simple as moving the start.time after the csv file is done...let's see how much of a change this brings...

SparkR

Around 1 second faster...which means that reading the csv was really efficient...


Plain R

Around 6 seconds faster...read.csv is not that good...but...SparkR is almost 50% faster...

HOLLY CRAP UPDATE!

 Markus from Spain gave me this code on the comments...I just added a couple of things to make complaint...but...damn...I wish I could code like that in R! -:D Thanks Markus!!!

Markus's code
logFile <- "/home/blag/R_Codes/Doc_Header.csv"
lines <- readLines(logFile)
start.time <- Sys.time()
a<-sum(grepl("a", lines, fixed=TRUE))
b<-sum(grepl("b", lines, fixed=TRUE))
paste("Lines with a: ", a, ", Lines with b: ", b, sep="")
end.time <- Sys.time()
time.taken <- end.time - start.time
time.taken


Simply...superb! -:)

Greetings,

Blag.
Development Culture.

jueves, 18 de junio de 2015

LED is my new Hello World - Lua Time

Getting on with my tradition of building an LED application for each and every new programming language that I learn...it's time for Lua -;)

LedNumbers.lua
local function split(s,delim)
 local result = {}
 for match in (s..delim):gmatch("(.-)"..delim) do
  table.insert(result,match)
 end
 return result
end

leds = {[0] = " _  ,| | ,|_| ",
        [1] = "  ,| ,| ",
        [2] = " _  , _| ,|_  ",
        [3] = "_  ,_| ,_| ",
        [4] = "    ,|_| ,  | ",
        [5] = " _  ,|_  , _| ",
        [6] = " _  ,|_  ,|_| ",
        [7] = "_   , |  , |  ",
        [8] = " _  ,|_| ,|_| ",
        [9] = " _  ,|_| , _| "}

io.write("Enter a number: ")
num = io.read()
for i = 1,3 do
 for j = 1, #num do
  line=split(leds[tonumber(string.sub(num,j,j))],",")
  io.write(line[i])
 end
 print("")
end

What you can see right away...and that's something that really surprised me...is that Lua doesn't provide a "split" or "explode" command out of the box, so you need to make it yourself...actually...the same holds true for Haskell...but for me...functional languages are on another league...and anyway...Haskell's implementation of the split function is way shorter...

Here's the result...


Greetings,

Blag.
Development Culture.