viernes, 25 de diciembre de 2015

Playing with promises in javascript

Reading about callbacks, monads, ... I've tried q library.
Interesting reading about monads and promises, source

This the code.

var fs = require('fs');
var Q = require('q');

function readFile(file){
  var deferred = Q.defer();
  fs.readFile(file, 'utf8', function (err, data) {
    if (err){
   deferred.reject(err);
 } 
    else{
   deferred.resolve(data);
 }
  });
  return deferred.promise;
}

var textFilteContent = readFile('notes.txt');

textFilteContent.then(function(data){
 console.log(data);
}).fail(function(err){
 console.log('error reading the file: ', err);
});

console.log('end');

jueves, 3 de diciembre de 2015

Lean Code Kata

Yesterday I was in an event at Software Craftsmanship Madrid. It was about lean code. The result of the exercise lives in my repo at Github.com. The code could be refactored. But I am learning that early refactoring could be a wrong decision for the future.

jueves, 26 de noviembre de 2015

bind in js

I'm studing functional programming, but in the different way as I've studied at univerity. So I'm coding some tipycal fp functions in javascript. Now is the bind function.

The code lives here

With this song while I'm coding

miércoles, 4 de noviembre de 2015

Playing with socket.io

More and more javascript is the language that the most of the time I'm writting. Some time ago I've discovered web sockets, so I've been playing with it. The result is a simple chat with socket.io library.

The source of the example code 

The code of this example lives in github

Happy hacking

jueves, 8 de octubre de 2015

Setup the current number format

I´ve discovered the new way for setting up numbers in the current country format. I´m from Spain, so the numbers are like 12.345,67.

We can use.

var result = parseInt(simpleNumber).toLocaleString('es-ES');

viernes, 28 de agosto de 2015

Discovering Map function in clojure

Here it's the simple function in clojure using map

(map + [1 2 3] [4 5 6] [7 8 9]) 

then it returns

=> [12 15 18] It's nice an simple ;-)

miércoles, 12 de agosto de 2015

My first function in clojure

This a simple recursive countdown, but in clojure is more interesting. This is the code:
(defn countdown [x]
 (if (zero? x)
  (do 
   :blastoff!
   (println "Zero")
  )
  (do
   (println x)
   (recur (dec x))
  )
 ) 
)