Jul. 25th, 2009

juan_gandhi: (Default)
I've been noticing lately that there's a lot of, hmm, software engineers, to put it softly (meaning, not exactly programmers) that are kind of proud to be proficient in "algorithms". They know how to sort in n*ln(n), how to implement a queue using two stacks, what is skiplist, what is Patricia trie (not sure here), and how to find a median on a distributed system (I never knew the answer; my guess is it takes ln(n) iterations).

What I notice regularly, though, is that the same smart algorithm people routinely use arrays to keep a list of processed items, and check whether an item was processed already by scanning the array; they use Map to store a set of things to do (map.put(myThing, myThing)); and they create a complicated tangled network of listeners/events queues/managers/factories/delegates where a simple solution (a five-liner) would suffice.

So, in short, no understanding of base data structures, and no artistic feeling of simple beauty.

For a change, let me show you something from Scala.

Option[T] is a class that kind of "replaces null": it either contains a value, or is equal to None. (I won't go deep into toposophical representation of this, but there is one).

Now imagine we have a method that returns an Option[A], then we have to process that A, if there is one, and produce a B; what we need is to have Option[A] -> Option[B].

Would you use if, switch, or something like that, what we use to use in Java? Ha, here's Scala solution:


val opt_a: Option[A] = ....
val result = opt_a map(a => dosomething(a))


See? The dosomething processes the value; the method map wraps it, producing an Option[B] (no, you don't need to declare the type of result, since it is obvious.

Profile

juan_gandhi: (Default)
Juan-Carlos Gandhi

September 2025

S M T W T F S
 1 2345 6
78 9 10 111213
14151617181920
21222324252627
282930    

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated Sep. 13th, 2025 01:47 pm
Powered by Dreamwidth Studios