Inspect Laravels query builder output

When learning something new (in this case Laravel and it's ORM Eloquent) it is always nice to be able to "look behind the curtains" to see what actually happens. This both help you understand the internals of the things you are using and can sometime make bug squashing a lot easier.

I wanted to be able to see the SQL that Eloquents query builder was producing, because SQL is something I'm fairly proficient with.

I found several questions and answers on different approaches on StackOverflow but one approach stood out for me: Using Laravels internal events.

Every time Laravel does anything through a database connection an event i fired with the prepared statement and its bindings. But creating SQL queries from this still required some manual labor because I needed to replace all the question marks with the binding in order.

So to close this gab further I had the prepared SQL and bindings crunched by sprintf before writing it to a log file or stdout, like so:

    function($query, $params, $time, $conn)
        $sql = str_replace('?', '%s', $query);
        $preppedSql = call_user_func_array('sprintf', array_merge([$sql], $params));

        echo $preppedSql . "\n"; // or write it to a log file

Not quite like partition-by ... but still

I had a problem where I needed to partition a collection when ever a specific element occurred.

The data I was working with wasn't numbers but for the sake of making an example lets imagine something like the following where 1 indicates a new partition:

(def coll [1 2 3 4 5 1 2 3 1 2 3 4])

I started looking a partition-by which resulted in:

> (partition-by #(= 1 %) coll)
((1) (2 3 4 5) (1) (2 3) (1) (2 3 4))

After a short while I came to the conclusion that non of the existing core functions partition, partition-all, partition-by, split-at, split-with did what I needed. The closest function I could think of was clojure.string/split but I wasn't working with a string and also the "splitter" needed to be kept.

I needed an end result like the following:

((1 2 3 4 5) (1 2 3) (1 2 3 4))

I went down several mental paths trying to find a solution that didn't feel clumpsy. Suddenly realize that partition-by actually wasn't that far off.

I got the following working and named my function partition-when:

(defn partition-when
  [f coll]
  (map (fn [[a b]] (concat a b))
       (partition-all 2 (partition-by f coll))))

I didn't like the above anonymous function which led me to the followin end result:

(defn partition-when
  "Like partition-by except it splits every other time f returns a new value."
  [f coll]
  (map #(apply concat %) (partition-all 2 (partition-by f coll))))

I haven't tested the performance of it compared a loop-recur solution, but I'm pretty happy about the readability of it. I find my self time and again solving a problem in one way, just to realize that there is a better (often shorter aka. more readable and resource effecient) way.

I don't think Clojure will ever stop amaze me with the endless compositions of core functions.

What's so cool about Clojure

Around 5 years ago a colleague of mine tried to spark some interest for Clojure. He described it using terms like functional, higher order functions, pure functions, composable, immutable data, lazy evaluated, code is data and vice versa, macros, concurrency etc. None of which I truly understood at the time but instead felt like buzzwords.

So instead of making me jump on the bandwagon, he left me with the question:

What's so cool about Clojure?

His enthusiasm and a little 15 minute REPL session showing off some data structures and concurrency made an impression I couldn't shake. It felt awesome and I couldn't explain why... I still didn't have an answer to my question, only a feeling!

After 3 years of naively waiting for Eureka, I realized that I needed to get out of my narrow comfort zone and commit to the learning process. So I decided to take the plunge and I felt good about it. After all I knew I still had a lot to learn - in retrospect that was an understatement.

The more you know, the more you know you don't know. - Aristotle

There was a gap in my knowledge about programming, sometimes entire concepts was unfamiliar other times I had "invented" similar concepts myself without knowing that it was "text book stuff" and it already had a name. To close this gap I needed to start out on familiar ground with something I could relate to and work my way up from there.

I now see that my journey to answer my own question has actually been more about programming in a broad sense than about Clojure as a programming language. The "buzzwords" is starting to make sense and it has got me thinking about how I would have introduced these to my not-knowing-self a few years back. Lately specifically "Lazy sequences".

I have some notes which might become a blog post in the near future... so stay tuned if you want to know what all the fuzz is about.

A new blog engine

For a long time I have wanted to find an alternative to Wordpress. It felt like an obstacle which needed overcoming every time I had an idea for a blog post. The result being that I didn't get any posts published (I have a few drafts which I never finished).

My own Clojure blog adventure was more about my trying to learn the Clojure language, and would probably have some of the same issues I experienced with Wordpress. Also when I stumbled on Jekyll my own blog project got subconsciously shelved, because it already implemented all my ideas so much better.

Apart from Markdown being a first class citizen, the blog can now be hosted on GitHub Pages.

Anyways all my old blog posts have now been migrated to Jekyll and the revamped layout now looks sufficiently good and works on mobile devices so I feel confident making a first release. I have "cheated" and based the design on the Jekyll theme called Hyde.

You are now looking at the result... lets see if this actually help on my post frequency :)

My Gnome 3 usability issues

I've just installed Gnome 3.6 (some packages might still be 3.5.9x) on my laptop by upgrading to Ubuntu 12.10 beta2 together with the Gnome 3 PPA for a few missing things like the updated Nautilus.

I'm a dedicated Gnome user and generally love the alternative focus Gnome 3.x has put on the desktop. But as with all software, no matter how mature (no - Gnome 2.x wasn't perfect either) there is room for improvement. The following is some of my usability issues, I've come across during my daily use of Gnome since 3.0. Some of this might be due to Ubuntu breaking the intended Gnome 3 behavior.

State toggle

It's possible to toggle both the overview (Super) and snap windows to the sides of the screen (Super + <left/right>).

I expected that it would be possible to toggle the new message tray as well with a subsequent Super + M. Instead Escape is needed to close the message tray. Having the Escape shortcut is fine, but I would like Super + M to work as well - just like with the overview. Also it seems that the Maximize toggle has been removed. Super + <up> still works for maximizing but for restoring the window you now need to press Super + <down>. For ease of use and consistency I would have preferred that a subsequent Super + <up> would restore the window (just like with the side-snapping).

When maximizing windows by dragging them to the top, I would like it to be more clear that the area next to the application menu is a drag handle. I'm not sure exactly how to accomplish this but here are my immediate thoughts on the subject:

  • A transition effect when dropping the window where the title bar slides up behind the top bar.
  • Something visual that connects the title bar and the drag handle on top bar to make it clear that the top bar now serves as the title bar for the maximized window.
  • Have a mouse-drag-pointer fade out / zoom out over the drag handle on the top bar upon drop.


From time to time I find myself wishing for arrow navigation between the application windows on the overview and to be able to select the window I want to have focus on my workspace with Enter.

Also if that worked it would be nice to also be able to move an application window to a new workspace using the Ctrl + Alt + Shift + <up/down> - just like outside the overview.

When a window is minimized I would like it to differ slightly visually from non-minimized windows - perhaps by being slightly transparent?... I dont know. When we look for a specific application I use everything we know about the application window to find it quickly: Application UI, window size, open content and if I could... the fact that it is minimized.

I don't like the automatic open of the of the overview when the last application is closed. Usually I have my windows maximized and can't always remember whether I have one or several applications on a workspace. I often find my self closing an application with Alt+F4 and pressing Super right after to open a new one. If the application was the last one of the workspace I close the automatic opened overview instead of opening it. This is slightly annoying.

File dialog

I expected that keyboard navigation for the file dialog would be like the navigation in Nautilus aka. Files. Ie. Alt + <arrows> for navigation. All that said the Gnome experience is awesome and I already can't wait for all the cool stuff in 3.8 and ahead :)