How to add in programming languages


This is a tongue-in-cheek attempt to expose programming language expressiveness.

1. Scheme

(+ 1 2)          
(+ 1 2 3 4)  
  • Characters needed to add n digits: + 3
  • Prefix notation allows passing in multiple operands
  • Prefix notation is not popular

2. JavaScript/Python/Ruby/Octave/Matlab

1 + 2
  • Characters needed to add n digits: + (n – 1)
  • Very simple and generally accepted
  • Infix form requires two operands leading to (n – 1) operators

3. Java/C#

//CSharp
class Program
{
  static void Main(string[] args)
  {
    Console.WriteLine(1 + 2);
  }
}
  • Characters needed to add n digits: N (where N is a largeeeeeeee number 🙂 )
  • Too verbose – you need a class to add two numbers

4. SQL

SELECT 1 + 2;
  • Characters needed to add n digits: + 8
  • Simple but not popular
  • Declarative nature can impact clarity

Do you know of any other language examples? Feel free to share in the comments!

Here are a couple of other posts you might also like:

Staying Up-to-date : Newsletters and Resources


Software developers and programmers work in one of the most volatile of environments, things evolve fast – API upgrades (which imply code obsolescence :P), new technologies (nodejs is about 4 years old only, yeoman was only released in 2012, Hadoop etc) and the emergence of new language releases and tools.

Programmer have to actively follow new trends to avoid playing catch up with the rapidly changing landscape. Curiosity is a great quality of the programming trade and programmers should ideally itch to learn why things work, how they work and how to improve existing tech.

Here are a couple of resources that I monitor, I enjoy them and I hope they are useful to programmers too.

General

1.  StatusCode

Status code is one of my favourites – it is an eclectic mix of information about software development, programming languages, tools and libraries. There is almost always something to tickle your fancy in the weekly releases, from advice on programming to tooling tips to new developments. Updates are not limited to the expected Java/C/C++ combo, Scala, Haskell, Clojure and others regularly pop up now and then.

2. CodeProject

The concise daily reports on industry and development news is something I look forward to. A quick scan of the mail allows me to quickly get abreast what is trending for that day in the Software development world.

Web Development – JavaScript, HTML5, …

1. JavaScript Weekly

This carefully curated selection of JavaScript-related articles is simply amazing. The weekly updates contain libraries, tools, presentations, tutorials, videos and articles. Did I mention that jobs are advertised too?

If you are a web developer, front-end engineer, JavaScript developer or are just plain interested in JavaScript; then you should try to follow this. At times, the list of ‘goodies’ might be too long however the short descriptions beside each link should make it easy to decide if you are going to check the link or not.

I keep wondering how the curator is able to keep finding new things week after week.

2. A drip of JavaScript

This awesome weekly post on JavaScript usually has a simple description of a single area of JavaScript. Joshua Clanton does a really really beautiful job at explaining things and making them clear with examples.

I have learnt a couple of tricky things about JavaScript from this and gotten a deeper understanding of the language. I wish there were weekly drips of Java and Python too.

Warning: the brief write-ups are usually dense so you have to dedicate some time to understanding it.

3. Ember Weekly

Ok, I write code using EmberJS and this allows me to ‘keep’ my eyes on the project (they seem to update it rapidly), it also allows me to follow the community and see what is happening there. Like others, it includes links to articles, videos/screencasts to watch, code samples as well as the ever-ubiquitous job postings.

4. Node Weekly

Node weekly is new, in fact I think the fourth issue was only published one or two weeks ago. It contains information on the NodeJS paradigm, there are articles to read and code samples to peruse. Some useful libraries and tutorials are sometimes included as well.

5. DailyJS

This has information on some topics in JS and is available daily. Most times I take a cursory glance at the contents and at times I go to the main website to check out information. Oh, they do have a LOT of articles on the website.

6. HTML5 Weekly

This is strictly for web developers and designers; it is a good source of updates about topics like websockets, HTML5, WebGL, CSS3 and browser updates. There are also links to tips and demos of exciting stuff.

Python

Pycoder’s Weekly

Python projects, jobs, news, new developments, articles and possible upcoming events. It is a cool way to keep tabs on developments in the Python community. Yes, it’s only once a week too so you don’t get information overload.

Java

Well, since I rarely program in Java nowadays (just the odd snippet or Android app at times) I do not actively monitor developments in the community. However I still follow these channels:

1. Java Magazine

This is published once a month and is usually a huge nicely designed magazine available online. I particularly enjoy reading about the interviews with industry experts and the puzzles at the end of the magazine are quite nice too – although I can not solve most of them because I lack the necessary Java expertise.

It is also a cool way to catch up with trends in the Java Community.

Ubuntu/Linux

1. Full Circle Magazine

This magazine is published by enthusiasts and usually contains one or two columns I find interesting. It is available on a monthly basis and primarily focuses on the Ubuntu class of operating systems (Kubuntu, Ubuntu, Lubuntu etc). The magazine contains news, tutorials, a QA section, some cartoons and the ‘pimp my desktop’ section (I like).

If you know of some other great resources, please do feel free to share them.

Thesis Stories Ep 3: Research is Hard!


Alhamdulilah I completed my thesis about three weeks ago; if you’re interested, you can check out my thesis and presentation. Looking back at the two years I spent at MASDAR, I have a couple of thoughts: Alhamdulilah I learnt a lot, met a couple of wonderful people and matured significantly. There were a couple of not-so-pleasant experiences too but I believe I emerged stronger ultimately.

So I switched to the complexity analysis of road networks after my stack overflow adventure ended unsuccessfully. It was a fresh start but I had no alternative since I wanted to graduate. In the end, I defended all my hard work in about 75 mins – imagine! Nearly 6 months of work translating into just 75 mins!!

Research is difficult! As difficult as any other endeavor; I think most researchers don’t know how their efforts will turn out (as most start-ups do at the beginning too). There is usually some hunch about a model, some experiments and then eventually they have to figure out what the ‘right’ result is. Also, ‘big data’ appears to be fun and cool but it requires unbelievable and prodigious amounts of grunt work.

I built JIZNA, a custom Python framework for complexity analysis. JIZNA can parse openstreetmaps XML dumps of cities (the parser was an open-source utility I found and modified), create dual graphs of these networks, merge discrete roads, exclude outliers and calculate the desired metrics. These metrics were used to predict how difficult it would be to search the city. The JIZNA platform is available here.

The Cool Stuff

I think I wrote much better code: the framework was modular, nicely designed and flexible; I was able to write some really cool algorithms for the complex computations and I learnt how to use Sphinx, the Python documentation tool. Sphinx, in my opinion, is a lovely tool once you grasp its basics.

The Not-so-Cool

I got a couple of interesting results however I think they were not so spectacular. I guess further work would reveal some new insights.

I had to throw away some of my code (a complete simulation framework had to be discarded when the approach changed) and my writing (again! This is the umpteenth time I’d be chopping off my writing).

So what did I learn? Lots more Python, algorithms, software design, documentation, writing, latex, vim and some maths (mostly matrix algebra). However, more importantly, I came to appreciate the value of grit, determination and perseverance while working towards goals. Don’t ever give up, even if all appears to be lost.

Next plans? I don’t quite know fully yet; one thing for sure: research is hard! 🙂

Did you like this post? Check out my other posts on Grad School.

For Devs only


I try to do less to achieve more – it is good; it makes me do my job faster and more easily; you should do so too. Automate, use shortcuts, innovate; well the initial investment might take a lot of time but it’s something you will be glad you did.

You can learn a lot by just exploring the tools you use daily. Just try to find a simpler way: some previously unknown feature in your favourite IDE, a macro to allow you repeat a series of actions or just something that saves you some keystrokes. So, I decided to post a couple of tips; I do hope you find them cool and pick up one or two new ideas.

Warning: Some of these work on linux only.

1. Bash Shortcuts

I stumbled upon these while going across someone’s .bashrc file and it stuck me as awesome; yes it is plain awesome. For example, accessing my /var/www/my-web-project folder (and I do access it often) is a pain; why do I have to type cd /var/www/my-web-project all the time? Solution? Just add the following shortcut to your .bashrc:

alias myproj=’cd /var/www/my-web-project’

and that’s it; I just type myproj in bash and am routed to that location. And yes, it doesn’t matter what directory I am in.

2. Bookmarks

Bookmarks are cool, I think they are absolutely essential. Now, if you don’t know how to use them or don’t use them in your IDE/Editor; stop reading and find how to use this wondrous time saver. I use bookmarks a lot in vim and Netbeans (and all other editors if possible); this allows me to jump around and move around. Yes, find out how to use bookmarks and use them!

Talking about vim; I think it’s really awesome and allows you to effortlessly edit anything that is text: I use it for code, latex, writing anything everything. At times, it might be easier to use a less powerful editor however most times, it does all and really cool too. And if you’re a ‘vimmer’, then try out these plugins: nerdtree, nerdcommenter, syntastic and latexbox (if you write in latex).

3. Debugger Statement (JavaScript)

I picked up this while watching an EmberJS tutorial and it has become a major part of my arsenal. Maybe I overuse it (well, I have to write so much JavaScript nowadays). Just put a debugger statement in any part of your JavaScript code and fire up that page in your browser; whenever execution gets to that point, it’ll automatically stop. I use this virtually everytime nowadays and for JavaScript devs; it’s a must use. Another cool JS tip involves using the console object; I use console.log too often but there are a couple of other cool functions like console.trace(), console.dir() etc.

4. Picking an object with a particular Characteristic

I picked up this trick while working in Python on my thesis: I often needed to pick up objects with certain properties out of long lists of objects. The interpreter is essential and this is what I do:

for obj in objectList:

if obj.ppty == criteria:

break

obj

Obj is now equal to my desired object.

5. Testing for -1 (Nice Discovery from SO)

I picked up this explanation from stack overflow and couldn’t help sharing it. I do hope you wouldn’t write code like this but it is always great to gain some insight into how things work.

Assuming you want to test if something is equal to -1; in 2’s complement systems, the representation of -1 is 11111111 (for whatever number of bits the system supports). Just take the bitwise inverse of that number and all you get is zeros. Now for the cool part, zero is a falsy value which can be used in your tests. Here is the link.

Hope you picked up something or the other from this; if you did; do share it with someone who might pick a point or another from it.

Did you like this post? Try out some of my other posts:

The language Series: Python


My Python Story

I had always wanted to learn Python ever since I was an undergraduate but my commitments took all my time. Alhamdulilah, I finally got my chance in my first semester at MASDAR when I had to do the Artificial Intelligence project in Python. It was a pretty challenging one; it involved building a framework that used brute-force in solving problems with humongous search spaces.

It has been a nifty tool for my research work too: starting with stackoverflow to openstreetmap data. Python provides easy-to-use tools for data cleaning, processing and graphing; some of its libraries are a sheer pleasure to use: matplotlib, networkx, scipy, numpy etc. More importantly, I can write my full processing stack in Python without having to resort to other languages or tools. Although, there are speed and memory consumption trade-offs; overall, it’s been a pretty good bargain.

The Good Parts

  • The language has an almost non-existent learning curve; it’s soooooo easy to learn.
  • Comes in handy when you want to write a script or solve a little problem.
  • An awesome community, nice package management and great libraries – examples include scientific libs (matplotlib, numpy, scipy) SQLAlchemy, PyTables, Sage, networkx.
  • Cross-platform and supports full-stack development.
  • Great readability, high expressiveness and a very simple grammar – almost reads like English at times.
  • Allows returning multiple values from a method.
  • Some cool supported operations in Python:
    • Swapping: x, y = y, x
    • Floor division: 5.0//2 = 2.
    • Comparisons: if 3 > x > 1: print x; I don’t know of any other language that has this feature.
    • Iterating through two lists simultaneously using the zip keyword.
    • List comprehensions are super-awesome.

The Bad Parts

  • Speed; Python is slow :(.
  • Memory consumption; you can’t explicitly free memory and it’s not suited for situations requiring extremely small memory footprints.
  • Inadvertent overwriting of default functions or settings. For example, I once did this str = “blah blah blah”; Python raised no objections, it meekly overwrote the str() function.
  • Multi-threading support is not too good.
  • Scoping issues: variables fall out of scope so it’s essential to choose unique names.
  • .3/.1 = 2.99999999999999996. The floating point issue rears its ugly head again.
  • Constants… Python does not support constant declarations out of the box; seems JavaScript got friends.

The Other Parts

  • The indentation can be a pain if your editor gets it all messed up. Use a good editor (vim/Emacs) and make sure your tabs are converted to 4 spaces.
  • IDE support ain’t that great since it’s a dynamic language and there are lots of ways to do the same thing.
  • Weird syntax for the ternary operator : True if condition else False
  • Installing libraries can be a really painful operation if you get it all wrong.
  • Since it’s not a statically typed language, typos aren’t detected until your program goes KABOOM!
  • How do you declare private variables in classes? I still don’t know.
  • Explicit addition of self all the time; I think it is needed to know the current execution scope but still….
  • There is no switch statement in the language; workarounds exist though.

Beginning Python?

Do it! Just jump into the language, learn the pythonic way and slowly you’ll come to see its beauty. It might feel weird if you come from a ‘braceful‘ language (i.e. Java et. al. ) and some concepts might initially perplex you. However, once you get used to the language, I bet you’ll wonder how you managed to get stuff done in other languages.

The sheer expressiveness is amazing at times: you write so little and achieve so much. I think it’s fantastic for data processing, little scripts and heavy lifting ( provided you have enough memory and processing power; some libraries allow you to leverage highly optimized C/FORTRAN code so it’s not totally bad).

Also learn to use the interpreter (you can also try ipython if you’re just starting out) – I use it a lot to speed up my development process and for debugging.

Rating

8.5 / 10

One of the easiest languages to learn and use for scripting needs and comes in handy for  rapid prototyping. Extra bonuses include loads of great libraries, support for OOP and functional programming. Sure, it has a few issues but what language is perfect?

Python will probably make you lazy but why do more if you can get the same results with less? 🙂

Did you enjoy this post? Check out my reviews of C, Java, PHP and JavaScript too.

UPDATE: Thanks to a reader for clarifying my wrong assumption about Python’s ternary operation support.

The language series: C


I finally took the compulsory software engineering course notorious for its very difficult course project – writing a bitcoin client in C. Alhamdulilah, we successfully completed the project: about 18k lines of code, automated builds/documentation/tests and lots of other stuff. I figure we rank around 7 or 8 on the Joel 12-point scale even though some don’t apply to our project. 😀 Big UPs to the team!

I decided to do a review of all the languages I have used or been forced to use while taking the course; the story behind learning these languages, their strengths and weaknesses; quirks, advice for beginners and some wisecracks too :).

C is first on the list. Here goes!

How I learnt C

I somewhat got forced to relearn this language this year but my first attempt at C was self-study in 2007 or 2008 as an undergrad. Despite my dreams of building the most AWESOME program ever, my C adventure ended abruptly after I read about 3 to 5 chapters of a C book. I was discouraged by apocryphal reports which insinuated that C was no longer relevant; so I left C for C++ and then Java. That story is here.

Well, this year I had no choice but to learn it. Well, there was another choice: getting a poor grade in the software engineering course.

Likes

  • C packs a powerful punch, who doesn’t like power and speed?
  • It has a concise grammar and you can learn the language fast.
  • Purity: its simplicity forces you to think.
  • I think function pointers are kind of cool too.
  • Forces you to learn how low-level computer stuff like stacks, heaps and memory allocation work.

Dislikes

  • It doesn’t support as much abstraction as I want.
  • Bah… why do I have to call free() all the time? Can’t the language help me with this? I already know and agree am spoilt but why make programming harder?
  • No hashtables? No string support? Beats me… every language seems to have these.
  • There is some redundancy in the methods available in the C library; e.g. strtol and atol; seems PHP got a predecessor in C.
  • Pointer tangles; what does this point to or mean? ***a.
  • Uninitialized values can hold all sorts of values; woe betide you if you make the mistake of using them straightaway; C won’t raise any errors.

Writing code in C

It’s one of two things: you’ll either learn code purity and write pretty nifty code or massacre lots of innocent computer bits à la segmentation faults, memory overwriting and stack overflows…

I think everyone starts out in the latter group and moves to the former :).

Recommended For Beginners?

C is pure and has a small grammar (makes it easy to learn) but a bit challenging for a beginner to start with. I think Python or scheme will be easier.
You’ll probably find OOP difficult to grasp if C is your first language however you’ll find other languages really easy.

C Quirks

7[a] == a[7] if a is an array; it was even on my exam! 😛

while(*s++ = *t++) copy a string t to a string s.

Rating

6/10

Pretty powerful, compact and small although lacks a lot of expected features and development is sometimes painful. There are a couple of libraries that you can use though.

I hear C++ is more challenging… Do the ++  signs signify difficulty? 🙂

Read my reviews of PythonJavaPHP and JavaScript too.

Thesis Stories Episode 2 : Adventures in Ginormous Data


What could be worse than trying to understand ginormous data? Not finding what you’re looking for in it! The original plan was to use stackoverflow data as a proxy for my data mining experiments and after battling the databeast into submission (with the aid of ‘weapons of mass data analysis‘ like Python, SQLServer and sqlite3); it pulled another fast one on me.

I started exploring the murky depths of the subdued dataset and plotted the data distributions (they were mostly heavy-tailed as expected although one was unexpectedly normally distributed). Plotting the distributions was a task in itself – I had to learn about the awesome matplotlib, numpy and scipy (installing them was ‘wahala‘) – and  then the plots were so skewed that I had to spend hours on end finetuning and tweaking before they finally agreed to appear properly on my plots.

Plotting data distributions was child’s play compared with the next set of challenges : getting the candidate posts. Having defined the five features I was interested in; I set out with gusto to find the ‘offending’ entries. I got a surprising outcome – the first three characteristics (deleted answers, converted answers and flagged answers) didn’t exist in the dump; I got the same outcome when I ran my queries on the online data explorer. Finally, I asked on stackoverflow meta and it was confirmed.

You bet I was disappointed right? Sure! I’d spent hours on end writing convoluted SQL queries (subselects, joins, aggregations and what-have-you) and wrapping my head around the data. Heck! Some queries took me about an hour to write, run, verify and tune. Do you know what programming in a declarative non-Turing complete language with lots of constraints (geek-speak for SQL) feels like? It feels like fighting Mike Tyson with one hand tied behind your back. 😛 (Alhamdulilah,  I took the MOOC DB course).

When man fall down, no be the end of hin life… (Nigerian proverb; language: pidgin English)

So I listed out my alternatives : getting a new dataset, using the same dataset or starting on a new topic (most disliked obviously 😉 ). My search for a new dataset was not fruitful – I find other datasets ill-suited to my research and going through the potentially painful process of transforming them does not appeal to me. I went back to my dataset and extracted three other features but the nagging feeling in my mind is that I might have to fall back to the third option.

So do I concede defeat? Nah, nat at all – am a Nigerian remember? We never say die; we’re way too optimistic for our good even :).

Lessons Learnt

  • Never write a lot until you’re really really sure that you’re gonna get something.
  • How to extract information from papers and critique them, know what they are all about.
  • How to read and write continuously for a long period – how do I do it? Pomodoro of course!

Next Steps

I might go back to the SO data; or start all over again but I just pray it turns out all fine – I now have about 4 months left.

Ohh; I forgot to talk about the platform – that’s just been about as good as the experiments.

I am using EmberJS, a MVC framework and it’s being really challenging as I am new to it. I’ve had to fix issues with performance and page load times, integration on Amazon EC2; and all sorts. It’s been so difficult that I’ve started entertaining un-Nigerian thoughts of giving up on EmberJS – plain old vanilla JavaScript is much simpler.

Ok. Magana Yakare (“The discussion is over”, language: Hausa).

Have a great weekend – I just wanted to go at it the Naija style today and not write the same old normal way I do. I hope you enjoyed it; if you did – drop a nice comment or share some of your grad life experience.

N.B: If you’re a grad student having issues with your thesis; don’t worry be happy 😀

 

The student life… of projects, assignments, exams and more work!!!


At last! I finally get to write on my blog. I have been struggling to cope with the never-ending stream of course work, projects and exams. Alhamdulilah I think I have some time for a pretty quick post.

I finally turned in the artificial intelligence project, probably the most challenging projects of the semester. First, it had to be implemented in Python – a language I didn’t know too well. The project involved writing a parser to read PDDL files, creating some representation of the parsed problems, and then plugging the models into the existing infrastructure so that search could be carried out for plans. Well, I started and thought it wasn’t going to be too tough but ultimately spent hours upon hours coding, debugging, testing, validating and as usual, tearing at my hair in frustration at times :D.

At last, I finally got a working model, got more insight into AI and learnt a thing or two more about Python : I particularly love the interactive shell, it was easy to run my buggy programs in interactive mode and then hack them until the bugs ‘submitted’ and vanished! Victory is sweet!! I don’t know any other language that gives me this flexibility. Ahh, there were the usual problems of memory leaks, computation time, generators, memoization and stuff like that; some neat tricks were involved too. Looking back, I can say it was fun and I really enjoyed it. 😀

Artificial intelligence is cool; have been learning about search, planning and now Bayesian Networks – you hear talk of constraint satisfaction problems, D-separation, Bayes’ theorem and stuff like that. I also take courses in Multi-Agent Systems and Data mining too and I have bagged some hours using Octave which is a ‘calculator’ according to my colleague.

Aside, I get to work on an Android-Django project whenever I have few minutes to spare – not too often but I play a supporting role to my teammate who is more involved in coding. Insha Allah, I’ll be doing more work after my finals… for now.. aarghh… I have a project in data mining due soon… more information after I turn it in insha Allah.

Lastly, you should check out Stanford online teaching classes… they are pretty great stuff!!!

Ahhh… the joys of vim


I had to learn python about two weeks back and I felt I ought to do things differently this time; I resolved to learn using an editor and not go through the IDE route. So I fired up gedit and was enjoying it until I wrote a program that ran an endless loop and had to kill the gedit process. After that, my gedit was never the same and all my attempts at fixing it failed. Frustrated at this, I had to choose between kwrite and vim… my choice made me write today.

Vim stands for vi improved. It’s the text editor that comes bundled with most unix distros and is a whole lot of fun. I am pretty much impressed by the features vim’s got: commands for text substitution, deletion and replacement, the lack of need for a mouse, the ability to run shell commands, the ‘cool geeky’ feel it has and best of all, the ease it brings to the development process. Here are some reasons why you should use vim if you are adventurous.

Fast

Vim makes editing and writing code very easy. You don’t have to worry about moving the cursor all around anymore. Just press the appropriate commands and you’ll have your code dancing to your biddings. Copy and paste the last edit process? Just press 3 keys. Text substititution? Easy. Regular Expressions? No sweat.

Extensibility

The thousand and one zim extensions available enable users to customize it and extend it in novel ways. You can use vim to write C, C++, Java, PHP, RoR, Python and a whole lot of other languages. Moreover, you can use vim for text editing purposes too. It just simply makes life easy; you don’t have to have 3 or 4 separate IDEs anymore, use vim and have fun.

Cross-Platform availability

Vim is available on all major platforms and you don’t have to worry about your favourite code development tool not running on some environments.

Before you jump into vim, know that it has a steep learning curve but once you get the drift; you’re gonna love it and better still, it’s available on all platforms. I’m not saying using an IDE is bad – I still use Eclipse and Netbeans – but using vim allows you to become more efficient.

Algorithms I


Finally, I started studying algorithms – after delaying delving into algorithms for a long time; I ultimately had no choice but to learn it. So pronto I picked up a book – Problem solving with Algorithms and Data structures using Python – and started with gusto. Having little Python skills, I was somewhat worried that my lack of experience was going to make it more difficult for me.

However the book is quite easy to read and I found myself learning faster than I expected – maybe this was due to my improved experience writing interpreted languages, maybe not. All the same thanks be to God who made it so easy. Compared to Java, Python is great for implementing algorithms; it makes life a whole lot easier, Java was such a pain. Python, which sometimes reads like English, makes it easy to write Stacks, Queues and other abstract data structures. Complex algorithms are also easy to write and test in fewer lines.

Alhamdulilah I’ve gotten to study classical topics such as modular exponentiation, greatest common denominator, fractals and the towers of Hanoi. Compared to Java solutions, Python solutions are elegant, simple and light. While I still don’t understand some of the algorithms fully, it’s a start and insha Allah I hope to be confident one day…

One thing: I’ve fallen in love with Python and I hope to learn more of the language as I go along.