Record of experiments, readings, links, videos and other things that I find on the long road.
Registro de experimentos, lecturas, links, vídeos y otras cosas que voy encontrando en el largo camino.
Tuesday, May 27, 2014
Sunday, May 25, 2014
Interesting Talk: "The Curious Clojurist"
Interesting Talk: "Functional Programming You Already Know"
Today I watched this interesting talk by Kevlin Henney:
Saturday, May 24, 2014
Interesting Interview: "Stuart Halloway on Clojure and Functional Programming"
Today I watched this great interview with Stuart Halloway:
It's a great way to get an overview of where Clojure can be helpful for you.
Bizarre factorial in Clojure
I'm reading Brian Marick's Functional Programming for the Object-Oriented Programmer book.
This is my solution for an exercise from the first chapter, Just Enough Clojure in which I had to write a "bizarre version of factorial that uses neither iteration nor recursion":
Tested at the REPL it seems to work fine:
However, there are problems for big enough results because they don't fit into integers:
This can be easily solved using arbitrary precision integers.
You just need to append an N after any number in the calculation, since it's "contagious" to the rest of the calculation:
Now it works:
That's ok but how does bizarre-factorial works?
Let's first examine range function:
So, for example, to get the first 10 positive arbitrary precision integers we'd write:
We might refactor bizarre-factorial using let to create a helper function, positive-ints-until, to make this more explicit:
Let's see now how apply works:
So in the case of bizarre-factorial the apply function is applying the * function to a list of numbers returned by the positive-ints-until helper function.
That's how bizarre-factorial works.
Finally we can use partial to give a better name to the partial application of apply and *:
This version of bizarre-factorial is much more readable.
This is my solution for an exercise from the first chapter, Just Enough Clojure in which I had to write a "bizarre version of factorial that uses neither iteration nor recursion":
Tested at the REPL it seems to work fine:
However, there are problems for big enough results because they don't fit into integers:
This can be easily solved using arbitrary precision integers.
You just need to append an N after any number in the calculation, since it's "contagious" to the rest of the calculation:
Now it works:
That's ok but how does bizarre-factorial works?
Let's first examine range function:
So, for example, to get the first 10 positive arbitrary precision integers we'd write:
We might refactor bizarre-factorial using let to create a helper function, positive-ints-until, to make this more explicit:
Let's see now how apply works:
So in the case of bizarre-factorial the apply function is applying the * function to a list of numbers returned by the positive-ints-until helper function.
That's how bizarre-factorial works.
Finally we can use partial to give a better name to the partial application of apply and *:
This version of bizarre-factorial is much more readable.
Subscribe to:
Posts (Atom)