Clojure after a month

Leave a comment

(4 weekends, to be precise)

“A programmer should learn at least one new programming language each year.”

Clojure is a functional programming language. I never particularly liked the term. In discussions related to functional programming, it always feels like a bunch of nerds talking a weird language that has nothing to do with real life: monads, laziness… All the toy stuffs that highlight the intellectual superiority of the cult. Most importantly, I want readability. Code is the means to express yourself to the machine and peer programmers. The more expressive and intuitive it is, the better. This is why I liked Python over C++ and Java so much. My friends did Scheme before, and it just seems like a luxurious brain exercise, while I have plenty of other things to spend my brain on.

The main reason I tried Clojure was the way it handles concurrency. At work I’ve seen a couple of enterprise softwares that let people drag and drop components into a diagram that will do data processing. It’s easy to convert those diagrams into serial code, but not the other way round. It’s partly why people cannot simply have a compiler that translates serial codes into programs with concurrency. We need a programming language that is also expressive of the dependencies of the tasks in a program – the intent of the programmer.

From this perspective, syntax is the first thing that strikes me. I don’t like the prefix order, as it’s unfamiliar, but it’s not too bad as it’s very quick to learn. Except for the mathematical formula. Turns out, it allows for a lot of flexibility. You can even write a code to write codes. You can also visualize codes as diagrams.

Looping is the next thing that strikes me. I cannot write loops the way I did with C, Java or Python. Instead I needed to use tail-recursion to replicate this. I avoided using loops, as it doesn’t work well with the way my brain was reasoning about functions. Instead, map and a bunch of other functions that operate on collections are preferred. Usually this means cleaner code, and sometimes codes that can be parallelized. It’s embarrassingly hard to write this kind of code compared to the old ways of simply looping, as you are now trying to communicate your intention & organization, not only your instructions.

Along with looping was immutable persistent data structures. It makes it harder to write codes, and the code is less efficient. Clojure did a great job reducing the inefficiency to a constant factor with that “persistent” property. With these data structures, now one can really talk about functional programming, as data is now value, and a pure function will always give the same output from the same input. Memoization can easily be supported as a result (a plus for me since I have to implement dynamic programming algorithms now and then).

Because you now communicate with the compiler in terms of values and functions rather than instructions, it is easier to visualize your code. You can write the code on one side of the screen, while the other side automatically update the values obtained by the function (kudos to LightTable). At the same time, you tend to use smaller functions, because the program becomes visually ugly as it grows pass a certain size. The combined effect is a new programming style, where you write small functions, unit test it instantly by typing in different values and observe how the end result is updated on the output screen. Small functions can be called to composed into other functions naturally, as you don’t need to worry about side effects.


After a few days familiarizing myself with Clojure, the old Python code now looks ugly to me. My old way of writing code was clearly harder to test, and the dependencies between code blocks weren’t clear.

It was one way Clojure changed my thinking about code. It’s not always desirable, for example when you want to write some quick dirty patchwork. But for a long term project, I believe that laying down those code organisation, and reasoning in terms of functions and values, are always things you should strike for. Not that it’s always possible though, and I like that Clojure allows for flexibility built into its design that lets you leave the functional world graciously.

Another great idea in Clojure is about abstraction. It provides a bunch of tools to generate new classes and new objects with different properties. This took me another few days to get a hang on how Clojure does OOP. It’s like you have different kinds of loops for different intentions (foreach loop, map-reduce loop, iteration loop), you also have different sorts of inheritance and polymorphism for different intentions. You can add functions to old types, and you can also add new types that support old functions. Sometimes you can just get away with a new instance, rather than having to create a new class/interface. A legacy class/object now becomes something alive that can be broken down, combined, or taught new tricks.

If Perl’s way is “there are different ways to do things”, Python’s way is “there is one prefered way to do things”, then Clojure’s way is probably “there is an ideal way and a practical way to do things, and we’ll tell you when you need to make a choice”.

I haven’t discussed the most prominent use of Clojure – how it handles concurrency. I haven’t mastered it now, but it is definitely a good topic for another time.

Now I’ll discuss how the implementation details are affected when you port an algorithm to Clojure. We are finding all the primes under a threshold.

Notice the main body of the code:

(primes-from 11 wheel))

It takes a function primes-from, starts from 11, and use the wheel, which is an infinite sequence as to instruct the steps it should take to find the next prime.
Primes-from perform a primality test as follows:

(some #(zero? (rem n %))
(take-while #(<= (* % %) n) primes))

… where #(zero? (rem n %)) is the test of division, and (take-while #(<= (* % %) n) primes)) is the list of potential factors.

A typical implementation of this algorithm in non-functional programming language would be the Sieve of Eratosthenes:

for i in range(n):
    if isPrime[i]:
        j = i
        while j<n:
              isPrime[j] = False

I believe the latter code is faster to write and to run. However, its meaning is only clear if one has a rough idea of the algorithm it’s trying to implement. The former code can be broken down into independent pieces, and the reader can try to understand each piece before combining them altogether. Of course, you can also implement the latter code in Clojure, just that it doesn’t give the natural feel. When you leave the functional programming realm, you know right away.


What can’t be solved with data, can be solved with more data

Leave a comment

… mark my words.

Nhiều việc

Leave a comment

… làm mình mệt quá. Toàn những việc nhỏ nhỏ nhưng từ từ ngốn hết thời gian của mình. Mỗi việc đều cần một chút kinh nghiệm. Làm không khéo thì cũng k chết, nhưng sẽ hao phí một khoản nhất định thời gian/quan hệ/thăng tiến.

Trong mạng nhện công việc đó, lại phải luôn định hướng mình tiến về phía trước, sao cho quan hệ với gia đình, bạn bè vẫn được chăm chút, sao cho bản thân mình trở thành một con người tốt hơn. Sao mà khó quá.


Personal content filter

1 Comment

Internet is a leap of human intellect: it lets individuals learn from the collective, and the collective learn from individuals.

Google is the first accelerator to that leap. Without it, people cannot effectively manage the huge number of websites. Previously, personal memory and colleague network are the main search engines.

Web 2.0 is the next accelerator. Facebook, twitter, reddit…

These are all tools to obtain better content. Google gives you what you search for. Facebook gives you what your friends search for. Twitter & reddit give you what your like-minded search for. Quora is also a good one that assist you where Google fails.

This commonality is because, as cost of sharing information goes down, cost of obtaining information goes up: “Information is abundance, but attention is scarce.” To be precise, it’s not the cost of obtaining information, but the cost of filtering for the right amount of information that match YOUR attention. Each day, for example, you have 1-2 hours to read news; but the internet provides you with thousands of hours worth of news. Not surprisingly, working adults (stereotypically) lose their interest in many things, as their free time is significantly shorter, and there is no effective way to scale down the time for each interest: either you do it, or you don’t.

Of course, this is a good problem. The solution is very simple: each of us has a personal filter that keeps track of what we have read, our preference, and filter the right information for us, according to our available attention. I would want to see friends updates, then major news, then minor development tips, then active technical discussion… depending on how much time I have that day/week.
If it’s so simple, why has no one else implemented this?

Because it would kill the internet.

The internet is free, upon the premise that you pay for it using your attention. Google is powered by ads. Facebook is powered by ads. Anything that you don’t pay for is powered by ads.

Of course, individually, you don’t have to care about TOS and just go ahead implementing your kick-ass filtering algorithm, whichever way you like it. Oh, and don’t forget to block those ad domains, too!

But futuristically, if you want to fix this one and for all, I think we need a micropayment platform.

User U joins platform P by paying up-front cost 5$. Platform P then provides user U with the content filters they want: ads blocked, API, programmable filters… User U then visits sites S1, S2, S3, S4… Sites S1 then charges P all the cost from users U1, U2,… the cost they’d get from advertisers.
This system is engineered such that no real transaction is actually micro: user U pays in chunks of 5$, and platform P also pays in chunks of 5$.

Turns out, it’s not easy to implement this system: platform P would have to deal with so many available websites. Well, but that’s exactly the problem Google ran into a decade ago. This time it’s a little bit harder: not only content matters, but representation matters, too.

Nón lá

Leave a comment

Nón lá là một phát minh có tính dân tộc cao:

– Thẩm mỹ cao: Áo dài với nón lá bài thơ…

– Che mưa

– Che nắng

– Quạt mát

– Đựng đồ


Nhưng thời nay nón lá không thông dụng nữa, và bị thay thế bởi dù và bao nylông, một phần là vì nó cồng kềnh bất tiện.

Ta có thể thiết kế một chiếc nón lá gấp (như quạt nan), khung bằng nan sắt cứng, phủ bằng vải dù. Khi không dùng ta có thể gấp lại cất vào túi xách/đeo bên tay như dù.

Restaurant pricing

Leave a comment

How do you like being given a pricing range rather than a pricing point? For example:

Price: 20-30$, depending on your satisfaction. At 20$ we are at a loss.

When the receipt is generated, we also take note of some common feature: age, sex, race. After a while, we have a fairly good idea of who will like what. For example, we’ll know that Chinese males tends to like this, while European females hates it…

Ruby Sparks

1 Comment

This is a powerful movie that you can only bump into once in a while. No surprise, as it comes from the directors of “Little Miss Sunshine”. In one movie, you get a full range of tastes: cheesy, teeny, funny, warm, mistery, cold, fear, relief, philosphy, optimistic…
I enjoyed the movie in at least 3 ways.

It reminds me of my dream girl, and how I have appreciated her as a real girl.

It gives a peek into the relationship between writers and their imagination. It’s also worth noting that the main actress was also the writer.

It highlights the difficulties of a programmer getting laid. He’d try to give a specs, and have some program that delivers the specs, even though it turns out to be broken in the end.

Me and Dung both really enjoyed the movie. Dung recommends it to the ones who like to ponder. Well, most of my friends do, so I’d recommend this to everyone of you. If you’ve been in love, it’ll give you a sneak peek. If you have, it’d surely remind the first time your imagination about love was broken.


Older Entries