Sunday, November 25, 2012

Simple Tetris Clone in TypeScript

I wanted to create a JavaScript port of my tetris clone using the HTML5 canvas element but I never got the drive to start it, so it was lingering for ages in my TODO list. The final push come after TypeScript, a little language from the father of industrial grade languages like Delphi and C#. This was just enough to push me to use my little game to familiarize myself with this new language. 

Sunday, November 4, 2012

Bitwise operators in C, JavaScript, Lua and Killa

So I thought that porting a Game Boy Advance (GBA) console emulator written in JavaScript to Killa would be fun, boy I was wrong... All because I wanted to play with this:

Thursday, November 1, 2012

Dynamic programming

Dynamic Programming (DP) problems are fun, but they can be also difficult to solve. For a better definition than I could give you here I'll redirect you to the Wikipedia article, and there are some nice tutorials and sample problems in TopCoder and CodeChef. In this post I'm just going to add commentaries to the DP problem I solved previously without giving much explanation. This problem comes from the Project Euler site:

An n×n grid of squares contains n^2 ants, one ant per square.
All ants decide to move simultaneously to an adjacent square
(usually 4 possibilities, except for ants on the edge of the grid or at the corners). 
We define f(n) to be the number of ways this can happen without any ants ending on 
the same square and without any two ants crossing the same edge between two squares.
You are given that f(4) = 88. Find f(10).

Tuesday, October 2, 2012

Ruby and the joy of programming

Years ago I used to solve many mathematical problems in Ruby. Looking backwards, I find difficult to explain my decision, because if I remember correctly Ruby was slow compared with C++ or Perl, and since most of the time I ended using a brute force approach to solve what my imagination could not solve efficiently I often ended awaiting longer times before obtaining my answers.

However, I can clearly remember that for the first time in my conscious programming life, I was happy and I was enjoying my time with some programming language. So I didn't worry too much about some additional time awaiting a stupid brute force solution. I was solving mathematical puzzles for fun and with Ruby I could quickly test some crazy ideas, leave the process running and go to do other things. Ruby was powerful, concise, elegant, and more importantly it didn't force over me its formatting policies or push me to do things in some way I didn't like to. I had finally found a language where everything fitted in place and seemed natural. I thought: this language must have been created by someone who really likes having fun while programming.

And indeed it was. Ruby was created by Yukihiro Matsumoto (aka Matz), who is one of my heroes actually. I found an interview he gave about Ruby and there he said something that really touched a fiber inside me, I have always used his words as an inspiration:

Fun to use. Freedom to choose. This man taught me all.

Sunday, September 23, 2012

Killa: my scripting language based in Lua and JavaScript

So I have been working these past months on my own programming language. It has been something that I've always wanted to do so I'm very happy that I can announce it here. Without further ado: Killa. I finished the 0.1 version months ago but real life just got busy on my end and I have been slacking this post for ages.

Killa (moon in the native tongue of my country) brings no new paradigm, neither disrupting ideas to the world of programming languages, to be honest I just stole some ideas from this book JavaScript: The Good Parts added some new ones from the top of my head and made it all work using the Lua 5.2 virtual machine, so... Why to bother? could you ask. Why indeed, with so many new and wonderful and exciting languages like Clojure, D, Haskell, Haxe, and trusted powerhorses like Python, Ruby and your favorite one here, creating a new half baked one could be just me following the new craze of "everybody can make his own programming language" (in 21 days).

Something of that was of course present, as I said before, I have always wanted to create a programming language, but even with that idea on mind, investing my time in a horrible language just for the sake of creating "my very own programming language" just doesn't click with me. If I'm going to create a programming language it better must be one that I'd really like to use, and I'm picky, programming after all has been my bread and butter for years. So I started saying that Killa just stole some parts of JavaScript and Lua, looking now backwards it's not surprise that I created Killa. I was not happy with any of them.

JavaScript. It had so many ugly parts that make me feel nervous every time I tried to do some fancy HTML5 stuff, at the end I did nothing (but that is not JavaScript fault, it was just me being lazy). Anyway, I could clearly notice that there were some beautiful gems hidden inside that jungle of errors and blunders. For starters, it was C-like, and C is one of my favorite languages. But, at difference of C, JavaScript had that "plastic" nature that made it powerful for solving problems in a dynamic way. First class functions, closures, that was cool.

Then it was Lua. I found Lua due to my work as a game programmer. Lua is kind of mainstream in the game industry for scripting, and it's a good little language, at least that was my first impression: it's pretty solid, it has a permissive license, an active community, it's very small and easy to embed inside your game engine, it doesn't take much memory and it's quite fast... but man: Why on earth have the array indexes to start from 1 instead of 0? and Why global by default variables? and Why the Pascal-like syntaxis? and Why not ternary operator? and Why not this and this and that? There were so many little annoyances that irked me daily. Somebody could think that I created Killa just because of external appearances, I don't think so. For me Lua was broken by the myriad of its little annoyances, but it's core was very useful and it was a waste not to use it.

I also believe that every programming language is by definition broken because none can do every task optimally. Lua was broken for me because I was a programmer working most of the time in a C-like world, having to change languages was a pain. By this definition, Killa is broken too, but it's broken for different people and for different reasons than Lua.

So I suppose that Killa has a place to fill and stay. I'm happy with the current status, but of course it needs lots of work to be stable. For now let me show you how the language looks, the little program below solves the Project Euler 393 problem under 9 seconds on my computer:

Not as fast as Python for this problem, but Python has the advantage of having immutable tuples that can be used as dictionary keys, I suspect that makes the above algorithm run faster in Python, because in Killa I had to use string keys and additional data handling was required. Also this problem needed support of big numbers that I added to Killa as an extension just now. It's just the port of lbc from Lua to Killa, but because the original library is under the GPL license I can not add it by default to Killa. I plan to maintain Killa under a permissive license for the sake of my fellow game developer peers. GPL-like licenses are very cumbersome for game development.

So, Who are the potential users of Killa? I think that anybody who likes the embeddable features that Lua has to offer but wants a JavaScript-like experience with many of its ugly parts removed. I could see Killa being used in the videogame industry or the embedded industry. To start things running (and for testing) I have already integrated Killa into two popular game engines: Love2D and cocos2d-x and I have made some small projects with them.

That's it. I need to explain with more detail the features of Killa in a future post, until then: happy coding!

UPDATE: I used bitwise operators to get the time down to 1.12 seconds, faster than Python (1.8 seconds)!

Sunday, September 16, 2012

Combinatorial explosion and Project Euler 393

Last week I found a nice video about algorithmic combinatorial explosion. It was very interesting but it's a bit sad to see the big sister throwing away her life in order to teach her little students the perils of trying to compute combinatorial problems that grow very fast. The problem they studied didn't seem to be hard, just enumerate all the ways you can go from one corner to the opposite corner in a rectangular grid without passing the same node twice. Deceptively simple, for a 1x1 grid there are only 2 ways, for a 2x2 grid there are 12 ways:

Saturday, May 26, 2012

Mandelbrot fractal viewer

One of the first things I did when I got my computer was to render the Mandelbrot Set fractal. This was centuries ago, and I did it in C using DOS Borland-C++ and its fabulous (for me at that time) graphics library. I played a lot plotting lines and points on the screen those days. I don't know why exactly but this program has been always in my heart, maybe due to the colorful images it gave me:

Google+ deleted all my blogger images, oh my

To be fair, I deleted them, but Google+ helped. I was thinking in updating this blog with some pseudo technical posts when I noticed all my blogger images were gone. Kaput. Done.!topic/google-plus-discuss/hJHmhE7mcg0/discussion

  • So basically I signed on this kind of Facebook clone called Google+ just for the kicks and gigs with my Gmail profile.
  • I saw a lot of images there that I never uploaded (now that I recall they were from my blogger account! but it was past midnight and I was asleep)
  • I deleted these Google+ images without remorse, Who else could have uploaded these images to my Google+ account? hackers are scary... or most likely it was just some glitch in this new Google+ thing.
  • Behind the scenes my Blogger images were deleted FOREVER. It seems images were never placed on Blogger itself but in Picasa, I never used Picasa so I didn't know.
  • Google+ linked the blog images from Picasa to my Google+ profile, when I deleted these images I was deleting the blog images, the trouble is that they must have anticipated this basic use case, I mean: Where is my gigantic popup blinking in red saying "DELETING THESE IMAGES WILL DELETE FOREVER YOUR BLOG IMAGES?". So there is no recovery I heard.

Kind of troublesome for me because I don't know where are the missing images (many are forever lost, I did them for the post) but catastrophic for many bloggers that had hundreds of images in Blogger. This blog of mine is rarely updated and has very low activity, however I can tell I have invested some time getting everything right, the images were created, edited, re-sized, recreated, etc, etc. Losing that time makes me sad, I can't imagine the amount of time lost for the users that have lost hundreds of images with this.

It's a very ugly blunder, and it makes me wonder where are all the great usability engineers of Google working these days. In the bright side I saw some improvements and work done in the blogger editor and statistics that were missing the last time I used the site, so I hope that everything is not lost.

Now, about this blog, well, I'm not feeling like restoring the lost images, I'll put the ones I can find at hand, but if not and you are really curious about some one that is missing drop me a note, sorry about the inconvenience.