Functional Programming & ME


After finishing Rebecca Murphy’s jqfundamentals and finding it to be quite an enjoyable and smooth read, I decided to learn JavaScript; I scanned my languages-to-learn list and quickly brought JavaScript to the front ahead of the others: Python/C/C++/C#/Ruby/Bash. One of the motivating factors was the fact that JavaScript supports functional programming; a paradigm I have been itching to learn. I fired up Google and scoured the internet, looking again for an open source book on JavaScript.

Eventually I got Eloquent JavaScript, A Modern Introduction to Programming by Marijn Haverbeke. Although I was somewhat skeptical about the book and didn’t dive into it; I however convinced myself to open it up and was impressed by the writer’s witty style, presence of exercises to test understanding of content, embedded console which allows writing and executing JavaScript removing the need to open up a separate program, a BIIIG winner with lazy lazy me ;); it also contains good quality elegant code and that code actually led to this blog post.

I eventually got to the chapter on functional programming and started it with gusto. Although I was new to the paradigm, it was fun and plain sailing until I got to the section describing the reduce algorithm. Reduce is a popular algorithm in functional programming that allows you to reduce a list of values to a single value.

I tried attempting the exercise on reduce and finally got my program to work. Feeling on top of the world, I checked the author’s code and got two shocks: the author’s code looked ‘nicer’ than mine and worse still, I couldn’t figure out why the code worked.

I refused to concede defeat to some piece of code so I spent the better part of an hour trying to understand why it worked; how it worked and why I couldn’t write such code. I later found out that the only mistake I made was in overlooking the fact that functions can RETURN functions in functional programming. Dumb me? No, I’ve been using OOP and procedural techniques only.

Once I got that, I quickly closed the window, thanked Allaah and went back to my daily tasks and forgot about paradigms, programming and all similar mind-bending stuff. However, my brain felt somewhat nicely stretched – the same way you feel after some tough exercise :D. Victory is sweet.

Next is the map algorithm insha Allah, let’s see who’ll emerge victor.

:D

6 Comments

·

Leave a Reply

  1. Quite amazing, how you sounded sweet mouthed about learning javascripts. Is it that fun to program in ? Anyways, you sound really convincing and I might check it out as my next program of choice to learn. Hope it’s gonna fun for me too…:) Take care all and God bless.

    Like

    • lol @ sweet-mouthed; but really I just started delving into javascript and jQuery this year and I’m amazed by it’s power. Tho it has some bad – which language is perfect anyway? – but it’s great. Next on my list is writing games in js. Try the eloquent js and jqfundamentals whenever you want to start. :D

      Like

  2. I do trust all of the ideas you’ve presented to your post. They’re very convincing and can definitely work.
    Nonetheless, the posts are too short for starters.
    May you please prolong them a bit from next time? Thanks for the post.

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s