viernes, 25 de diciembre de 2015

functional programming in javascript

Since summer I've been studing functional programming, first of all I started with clojure, later continued to javascript. Actually I studied some functional programming concepts and I applied into javascript, like map, reduce, foreach, monads, ...

Of course been doing tdd, althoguh the tests are not very expressives.

The code lives here.
Some examples of tests

describe("combine filter, map & reduce", function(){
 it("return the result of combination of filter, mal and reduce", function(){
  var data = [1,2,3,4,5,6];
  var resultExpected = 15;

  var result = reduce(
       map(
        filter(
         data, functionForFilter
        ), 
        functionForMap
       ), 
       0, functionForReduce
      );

  expect(resultExpected).to.eql(result);
 });
 function functionForMap(y){
  return y + 1;
 }
 function functionForFilter(x){
  return x % 2 == 0;
 }
 function functionForReduce(acumulative, z){ 
  return acumulative + z;
 }
});

describe("PipeLine object", function(){
 it("make pipe line through different functions", function(){
  var arrayOfData = [1,2,3];
  var expectedResult = 9;
  var handlerOne = function(data){
   return map(data, function(number){
    return number +1;
   });
  }
  var handlerTwo = function(data){
   var result = 0;
   return reduce(data, result, function(result, number){
    return result + number;
   });
  }
  var pipeLine = new PipeLine();

  var pipeLineResult = pipeLine.pipe(handlerOne)
         .pipe(handlerTwo)
         .execute(arrayOfData);

  expect(expectedResult).to.eql(pipeLineResult);       
 });
});

describe("curry function", function(){
  var expectedResult = 10;
  var add = function(a, b, c, d){
   return a + b + c + d;
  }
  var curriedFunction = curry(add);
  beforeEach(function(){});
  it("curry function with all parameters as partial", function(){
   var result = curriedFunction(1)(2)(3)(4); 

   expect(expectedResult).to.eql(result);
  });

  it("curry function with two parameters as partial", function(){
   var result = curriedFunction(1, 2)(3)(4); 
   
   expect(expectedResult).to.eql(result);
  });

  it("curry function with one parameters as partial", function(){
   var result = curriedFunction(1, 2, 3)(4);
   
   expect(expectedResult).to.eql(result);
  });

  it("curry function with parameters as partial in two pairs", function(){
   var result = curriedFunction(1, 2)(3, 4);
   
   expect(expectedResult).to.eql(result);
  });

  it("curry function with parameters as usual", function(){
   var result = curriedFunction(1, 2, 3, 4);
   
   expect(expectedResult).to.eql(result);
  });
 });

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 ;-)