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

miércoles, 15 de julio de 2015

Translate simple program from Java to Clojure

In Java:
public static double hypot (double x, double y) {
 final double x2 = x * x;
 final double y2 = y * y;
 return Math.sqrt(x2 + y2);
}
In Clojure
(defn hypot [x y]
 (let [x2 (* x x)
  y2 (* y y)]
 (Math/sqrt (+ x2 y2))
  )
)
  The execution
(println (hypot 2 3))

martes, 14 de julio de 2015

My first code in clojure

I've finished reading Javascript The Good Parts. Yesterday a co-worker said to me that I could learn some functional language, He recommend to me Clojure son I've just started with Programming Clojure and this is my first little program:

(defn average [numbers]
 (/ 
 (apply + numbers)
 (count numbers)
 )
)

(println 
 (average [1 2 3 4 5])
)

When I was writing this little piece of code, I've remember when I was at university and I studied Lisp in IA signature.

miércoles, 24 de junio de 2015

List In Javascript

I've finished my new pet project: lists with fluent api and like a linq operations. It lives here
It appeared as a code kata, but it finished like a little library.

some tests

 
it("Given different lists with numbers when call zip method, return a list with zipped elements", function(){
   var numbers = new List([1,3]);
   var result = numbers.zip(new List([2,4,6]));
   expect(result.isEqual(new List([1,2,3,4,6]))).to.be(true);
});

it("Given list with numbers when call skip method then Ignores the specified number of items and returns a sequence starting at the item after the last skipped item (if any)", function(){
   var numbers = new List([1,2,3,4,5,6,7,8]);
   var result = numbers.skip(3);
   expect(result.isEqual(new List([4,5,6,7,8]))).to.be(true);
});

it("Given list with numbers when call apply condition with fluent api", function(){
   var result = new List([1,2,4,3,5,3,7,9])
                    .where(function(x){ return x > 1})
                    .where(function(x){ return x < 5});
    expect(result.isEqual(new List([2,4,3,3]))).to.be(true);
});

describe(".orderBy()", function(){
   it("Given list with numbers when call orderAscending method then returns an list ordered", function(){
      var numbers = new List([{number:5, letra:'e'},{number:3, letra:'q'},{number:1, letra:'a'},{number:9, letra:'w'},{number:4, letra:'r'},{number:7, letra:'t'}]);
      var result = numbers.orderBy(function(x, y){
         return (x.number - y.number);
});
    expect(result.isEqual(new List([{number:1, letra:'a'},{number:3, letra:'q'},{number:4, letra:'r'},{number:5, letra:'e'},{number:7, letra:'t'},{number:9, letra:'w'}])))
.to.be(true);
    });
});