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":
(defn bizarre-factorial [n]
(apply * (range 1 (+ n 1))))
Tested at the REPL it seems to work fine:
user=> (bizarre-factorial 1)
1
user=> (bizarre-factorial 2)
2
user=> (bizarre-factorial 3)
6
user=> (bizarre-factorial 4)
24
user=> (bizarre-factorial 5)
120
user=> (bizarre-factorial 6)
720
user=> (bizarre-factorial 7)
5040
user=> (bizarre-factorial 10)
3628800
However, there are problems for big enough results because they don't fit into integers:
user=> (bizarre-factorial 40)
ArithmeticException integer overflow
clojure.lang.Numbers.throwIntOverflow (Numbers.java:1388)
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:
(defn bizarre-factorial [n]
(apply * (range 1N (+ n 1))))
Now it works:
user=> (bizarre-factorial 40)
815915283247897734345611269596115894272000000000N
user=> (bizarre-factorial 100)
93326215443944152681699238856266700490715968264381621468592963895
21759999322991560894146397615651828625369792082722375825118521091
6864000000000000000000000000N

That's ok but how does bizarre-factorial works?

Let's first examine range function:
user=> (doc range)
-------------------------
clojure.core/range
([] [end] [start end] [start end step])
Returns a lazy seq of nums from start (inclusive) to end
(exclusive), by step, where start defaults to 0, step to 1,
and end to infinity.
nil
So, for example, to get the first 10 positive arbitrary precision integers we'd write:
user=> (range 1N 11)
(1N 2N 3N 4N 5N 6N 7N 8N 9N 10N)
We might refactor bizarre-factorial using let to create a helper function, positive-ints-until, to make this more explicit:
(defn bizarre-factorial [n]
(let [positive-ints-until (fn [n] (range 1N (+ n 1)))]
(apply * (positive-ints-until n))))
Let's see now how apply works:
user=> (doc apply)
-------------------------
clojure.core/apply
([f args] [f x args] [f x y args] [f x y z args] [f a b c d & args])
Applies fn f to the argument list formed by prepending
intervening arguments to args.
nil
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 *:
(defn bizarre-factorial [n]
(let [positive-ints-until (fn [n] (range 1N (+ n 1)))
multiply (partial apply *)]
(multiply (positive-ints-until n))))
This version of bizarre-factorial is much more readable.

Wednesday, May 21, 2014

Saturday, May 17, 2014

Interesting Talk: "Clojure for Java Programmers"

I've just watched these interesting talks by Rich Hickey:

Interesting Talk: "Dealing with Dynamically-Typed Legacy Code"

I've just watched this interesting talk by Michael Feathers:

Interesting Talk: "Making Little Classes out of Big Ones"

I've just watched this interesting talk by Avdi Grimm:

Interesting Talk: "Build a Bigger Brain: How Healthy Living Makes You Smarter"

I've just watched this interesting talk by Joe Kutner:

Interesting Talk: "Rules of Simple Design"

I've just watched this interesting talk by Corey Haines in which he comments some ideas from his last book:

Saturday, May 10, 2014

Interesting Talk: "Decoupling from Rails"

Great talk by Jim Weirich where he refactors a Rails application code to decouple it from the framework so that it follow the clean architecture: There's a lot of wisdom in this talk.

Values without practices

"All of this high-minded talk is well and good, but if there isn't some way to reduce it to practice, to reinforce it, to make the values a natural habit, then all we will have is yet another brave leap into the swamp of methodological good intentions."
Kent Beck, Extreme Programming Explained

Tuesday, May 6, 2014

Interesting Talk: "The Architecture of Uncertainty"

I've just watched this great talk by Kevlin Henney: This talk gives great insights in the relationships among uncertainty, design, learning and change.

Lately I've been reading and thinking a lot about this after reading Sandi Metz great book on OO design and several interesting discussions with @remosu.

Some intuitions are growing in my head but they are still quite fuzzy.

Saturday, May 3, 2014

Interesting Talk: "Less - The Path to Better Design"

I've just watched this great talk by Sandi Metz:

Kata: Revisiting Gilded Rose in Java

Yesterday Jaume Jornet and I were practicing together doing the Gilded Rose kata in C#.

At the end of the session, we left the code in a point where it was ready to substitute several conditionals with polymorphism. Then we discussed how we would do it without modifying the Item class (to avoid the goblin's rage).

We talked about wrapping Item in another object DegradableItem and then create several subtypes for it. Once having this design in place the Conjured items might be implemented using a decorator over DegradableItem.

When I got home I did the whole kata again in Java and coded these ideas we had talked about with some slight variations:



You can compare it with another version I did nearly a year ago.

Our lifes would be much easier without the goblins :)