Functional Programming in PHP

For the last half a year I have been coding in Scala and today I had to switch back to PHP to add some functionality to an old service. The interesting part was that it was coming more naturally to me to use filter and map to do my job instead of using foreach. Well .. here is what came out:

$keywords = array_filter( explode(" ",$keyword), function($x) { return !empty($x); } );
$keywordsQuery = implode( " AND ", array_map ( function($x) { return "body LIKE '%$x%'"; }, $keywords) );

This was the first time I was using array_filter and array_map and anonymous functions so I was very excited while writing it but the result isn’t the most readable. Also it wasn’t so easy to write it cause I had to go to the PHP website to check in which order do the arguments go (first the callback or the array?), which is something you don’t need to check when coding in a more object-oriented way. Also I noticed that PHP’s anonymous functions have a similar ugliness with Javascript. You have to write the whole word “function” which takes quite some space and makes inline anonymous functions very verbose & hard to read.

Anyway. Luckily I wont have to write too much code in PHP for this new feature and I hope there wont be many more things to write in PHP. I’m not saying that the there is something wrong with the language. I’m just saying it’s hard to go back from Scala to PHP.

So .. that’s all I wanted to share with you today. Below is the code of how I would do it in Scala (didn’t run it but more or less it should work!).

keywords = keyword.split(" ").filter(_.nonEmpty).map(x => "body LIKE '%" + x + "%'").mkString(" AND ")

Gist for my little scripts & configuration files

From time to time I write some scripts to automate tasks. For example, I might write a script to upload some files using POST to a service, or a script to deploy code to a server etc. But not always I can add them to a repository so they are backed up by a version control system. And when the time comes to format my computer I need to go all around & make copies of those scripts.

Sometime ago I discovered Gist offered by Github. Gist allows you to create a repository for storing the contents of one file & keep versions whenever you re-upload it (edit it). And you can do it all through the browser & access those files anywhere and at anytime. This is perfect for backing up your scripts & configuration files. I used Gist for the following:

  • apache & nginx configuration file
  • ~/.bashrc
  • various scripts to do any sorts of tasks
  • nice terminal commands that you can never remember

And I’m planning to add there each little script I write from now and on! But I need to remember every time I modify the script to re-upload it to Gist.

Ok .. that’s all I wanted to write. Not very interesting but really useful! I’m not joking. I just moved to my new laptop & I didn’t need to move any configuration file from my previous computer cause I had them all on Gist 🙂

Multi touch works on my new Lubuntu!

Last week I bought a new laptop (Acer V3-571G i7-3630QM/16GB/750/DVD-RW GT640M) and accidentally a friend sent me an email about Lubuntu. So I decided to give it a try 🙂

There are a few things I liked about Lubuntu:

  1. It’s lightweight. It doesn’t have all that gnome stuff that I’m not really using
  2. It has synaptic so I can properly see all the packages (comparing to that Software Center of Ubuntu that doesn’t show me the technical packages I want to install).
  3. It works out of the box. Didn’t have to install any additional drivers for anything (camera, microphone, wifi, graphics card). That’s the same with Ubuntu of course.
  4. Multi touch works! Most probably it works on Ubuntu as well & on other distributions but that’s the first time I have a multi touch mousepad. Therefore I can now scroll up & down with two fingers!! Even right click by tapping with my two fingers! This is super! I feel like I bought a Mac or something 😀
  5. It uses PC Man File Manager which is super fast!

What I didn’t like about Lubuntu:

  1. It looks a bit “old”. Well .. I can’t say much cause I’m using DWM (that’s the first package I installed!) but when I logged in the first time on that Openbox window manager it looked a bit “old”. But it’s ok. If you want to run it on a weaker machine it’s definitely a good option (although I would recommend DWM if you are a coder like me).
  2. Doesn’t come with Open Office (or Libre Office). It has of course Abiword & Gnumeric installed but I’m not sure how feature-full these programs are. Time will show. For now a couple of CSV files I opened worked fine. I wont install Libre Office for now until I really need it.

That’s it about Lubuntu. I’m not super excited about it but I’ll keep it on my PC. The main reason is because I’m using DWM as window manager and Lubuntu offers me PCMan file manager which is super quick & properly configured (icons show correctly etc).

On my previous Ubuntu installation I gave a try to Thunar (another lightweight file manager) but icons weren’t showing properly. Now with Lubuntu & PCMan everything works like a charm!

Ok .. back to work ..

xargs vs parallel

Have you ever used xargs on linux? Did you ever get frustrated that it doesn’t work very well with the find command?

What do I mean?

Let’s assume we have a list of .csv (1.csv .. 5.csv) files in a directory and we run the following command:

find ./ -name "*.csv" | xargs echo

Then we would expect that for each filename it would run the echo command. But it doesn’t! Instead it returns us:

./1.csv ./2.csv ./3.csv ./4.csv ./5.csv

Apparently it calls echo only once passing it all the filenames.

There is a way to fix this problem by telling the find command to add a 0 (NULL character at the end of each filename & then tell xargs to accept those zeros as delimiter) but it’s not very elegant and it’s easy to forget:

find ./ -name "*.csv" -print0 | xargs -0 echo

Well today I discovered on stackoverflow a comment about the parallel command:

find ./ -name "*.csv" | parallel echo

The parallel command does exactly the same thing as xargs (but instead it properly handles results of the find command) and in addition to that it executes each argument in parallel! Depending on how many processors you have on your machine, the faster it will execute!

This is particularly handy when you are doing lots of uploads (that’s what I was doing today).

Note: You might need to install the parallel command on your machine (on ubuntu you can do it with sudo apt-get install parallel)

Finally my first blog entry!

It has been sometime already that I wanted to start blogging about my adventures in the coderland! There were many occasions that I wanted to share what I discovered (nice linux commands, nice code, nice libraries or frameworks etc) but I was always postponing it. Why?

Well .. I wanted to create my own blog! I mean .. code it! As a proud developer I couldn’t just use WordPress (like I did now). But as the time passed by I realised that I’ll never code that awesome blog I was dreaming about so I should better start writing something before it’s too late! Too late for what? Well .. you know .. you forget all those nice things you wanted to share with others. And it’s a pitty 🙂

Ok! Welcome to my blog! I welcome myself too! 😀

Hope you’ll enjoy my future entries!

And I hope I wont get back to that stupid idea of creating my own blog & stop writing here.

See you!