Categories
boo c# lisp Programming python

The power of lisp part 2

As mentioned earlier, lisp is incredible at abstracting away small everyday stuff you write over and over. A good example is this way one has to define a local variable in order to make variables sucked into a delegates from the surroundings static in C#. As mentioned briefly in this blog post, one needs to change the loop to read like the following code for it to print 0 to 9 instead of 10 times 9:

foreach (int i in numbers) {

        int j = i;

        l.Add(delegate() { System.Console.WriteLine(j); };
}

In lisp one would see that this is a pattern and simply abstract it away like this. As one can see, the macro static-loop is more or less exactly the same as the pattern which luckily made it quite easy to write.

(defmacro static-loop ((i container) &body body)
 (with-gensyms (j)
  `(loop for ,j in ,container do
    (let ((,i ,j))
     ,@body))))

The only small trick is the with-gensyms macro which prevents leaking the temporary variable j into the scope of where it will be substituted into. So in that regard the macro is even nicer than rolling your own temp variable hack 🙂

For reference the python example works the same way, but allows one to use the lambda function to redefine variables, although one must note that the static-loop solution is more general in that it will make the loop variable local for the whole scope and not just for one lambda function. Funny thing is that I had originally written it like this, which actually worked, although that means that the closure binding is not only by reference but by name.

I tried writing the macro in boo but I failed. Apparently their macro support is still quite new and there’s close to no examples to draw inspiration from. If anyone could help me out with the solution please mail it to me or even better add it as a comment.

Categories
boo Books Programming

The power of lisp part 1

Lately I’ve been reading the book Practical Common Lisp. My earlier experience with lisp was at the university where I briefly touched lisp in the form of the Scheme dialect. After having read Paul Grahams excellent Hackers and Painters twice, I thought it would probably be a good idea to see if lisp lived up to the hype. After seeing Practical Common Lisp being recommended by bamboo of boo fame and seeing this google tech talk I was convinced that the book was the best way to see all the wonderful stuff lisp has to offer.

So far I’ve been quite impressed with some of the things one can do in the language in a very elegant way. And I suddenly understand why closures work the way it does in C# and python. The problem with moving stuff from a really dynamic language like lisp to a static language like C# is that not everything fits just as fine and you’ll find yourself wondering why the hell code like this returns 10 times 9 instead of 0 to 9. Another thing is that I often find myself remembering small code idioms that just completely goes away when one can build abstractions so easily. The first example is if you want to break out of a double loop. A situation more often that one would think.

I C# one could write a double loop something like this. Notice the introduction of the found variable which is needed to make sure that the loop doesn’t do needless work. Even with an anonymous function it’s quite ugly (and the same is true for a goto solution :)). In lisp it would look something like this (my first common lisp program :-)). The abstraction is in the block concept and in the generalized return construct return-from.

Edit: Can be simplyfied to this:

(let ((l (list (list 1 2 3 4) (list 5 6 7))))
  (loop named outer for i in l do
	(loop for j in i do
	      (when (= j 3)
		(format t "found value, yeah!")
		(return-from outer)))))
Categories
On the web

Nokia n810

3 words baby, gimme one now

Categories
fowa iola

Future of Web Applications postmortem

FOWA is now over and it has been a great conference. On the way back in the plane I got to thinking that almost all the new web applications work a lot like open source projects. In the way that they are first and foremost open, encourage external contributions (using API´s), encourages the users to drive the development of new functionality, and are actively encouraging feedback from their users in an open way where users can help other users solve problems.

Categories
fowa

FOWA day 2

Day 2 is great, won a red?! 8 gig ipod. Great and funny talks about startups and ducks among other thing so far… 🙂

Categories
fowa iola

FOWA day 1

Day one was really great. A lot of awesome talks. The most interesting guy was perhaps Umair Haque talking about edgeeconomy.  I havn’t  heard the economic  perspectives of open  distributed  production, really interesting. Steve Souder showed a very nice tool called Yslow, that can really help you find speed bottlenecks in your web apps. Anyway next up is Paul Graham so I better stop here 🙂

Categories
iola Personal

Future of Web Apps

I’ll be attending the Future of Web Applications next week. The conference seems to have quite a few interesting talks, I’m particularly looking forward to hear Paul Graham talk since I enjoy his writings immensely.

Categories
On the web

UI design

Saw this very interesting UI-video today called Away with Applications: The Death of the Desktop by Aza Raskin (part of the google tech talks). It shows an interesting application enso, very similar to quicksilver. The interesting part is of course the abstract (what it is a solution to) and the conclusion (and why). Especially things like application silos and how enso marries the CLI with the UI gives some food for thought.

Categories
Free Software On the web

Finally skip-free audio playback in Linux

Stumpled upon this story the other day. Finally a tool where you can actually decide which things get prioritized instead of the kernel relying on heuristiscs. I did a test with alsaplayer playing back using through jackd (very sensitive to not getting enough time slices). To stress the system I did a emerge sync together with a fetchmail process sucking down the days mail. I don’t really care if it takes 5 seconds longer to fetch my mail or if it takes 2 minutes long to emerge sync as long as I can be sure that my music doesn’t skip. And guess what? With DeskOpt you can 🙂

Categories
boost cpp My Media System Programming

Boost part 2

In the quest for improving the quality of the MMS code in 1.1.0 I’ve done several things, one of which was to convert all the source to the boost foreach statement. Since I liked the result of the foreach cleanup so much, I decided to see what other libraries could be of use. I quickly found the function and bind library which allowed me to remove the internal function pointers framework we have currently been using. While converted I was surprised to see how cleaner the code became. The bind function is truly easy to use and read and made it possible to remove several helper classes which only purpose was to capture state to create a uniform interface.

So instead of:

class FP
{
  std::list numbers;

  FP(std::list n)
    : numbers(n)
  {}

  call(int t)
  {
    int total;
    foreach (int i, numbers)
      total += i*t;
    return total;
  }
};

(I saved the part of actually creating the function pointer object). One gets:

call(int t, std::list numbers)
{
  int total;
  foreach (int i, numbers)
    total += i*t;
  return total;
}

boost::bind(&call, _1, list_of_numbers);

Voila. Now if only the wordpress code formatting didn’t suck so much 😉