Programming ≈ Fun

Are Those (Lisp) Wackos Actually Right?!

For the last week I am having this crazy thoughts. What if Lisp really is an asymptote that all programming languages are converging to. It seems to me more power you have, closer to Lisp you are. I’ve heard it hundreds of times, but never “bought” it. What if you really don’t see parentheses after a while? How would the syntax look then? Ever since BASIC on C-64 I was exposed to Algol like syntax. That was 24 years ago, and I was 10 at a time. Is it strange that I find “other” syntax strange (pun intended).

So the burning question in my mind is: Is Ruby just a transition to Lisp. Ruby has all this nice stuff that enables me to better express myself. Best part is that feeling that I get while working with Ruby. It is cozy and familiar, but is Ruby just softening me up to see the Light?

This is how I see Lisp programmers. They are crazy, bearded and ambushing poor language syntax from their beloved S-expressions. They are making poor syntax do stuff they(syntaxes) are too ashamed to tell their mothers. Stuff that is terrible beyond imagination. Stuff that you hear only happening in third world jails in banged-up-abroad kinds of show. I am talking about stuff like making method private only in production and public for test and development… hey wait, that might actually come in handy.

Read on →

Lisp Lure Revisited Allais Paradox

One of comments over my last blog post was by Alan Crowe who suggested to checkout usage of macros in his implementation of Allais paradox.

(defun small-win ()
  (format t "~&Congratulations! You win a million dollars."))

(defun big-win ()
  (loop repeat 5 do (small-win)))

(defun no-win ()
  (format t "~&Shrug. You were not likely to win and you didn't."))

(defun big-regret ()
  (format t "~&What a loser! You could have had a million dollars."))

;;; A recursive function that turns a,b,c into a, a+b, a+b+c.
(defun accumulate (numbers total)
  (if (endp numbers)
      '()
      (let ((more (+ (first numbers) total)))
        (cons more
              (accumulate (rest numbers)
                          more)))))

;;; Common Lisp code that is incomprehensible to mere mortals :-)
(defun make-guarded-commands (die-var)
  (lambda (clause bound)
    `((< ,die-var ,bound) ,@(last clause))))

(defmacro percentage-case (&body clauses)
  (dolist (clause clauses)
    (check-type (first clause) integer))
  (assert (= 100
             (reduce #'+ clauses
                     :key #'first)))
  (let ((cumulative-distribution (accumulate (mapcar #'first clauses) 0))
        (die-roll (gensym)))
    `(let ((,die-roll (random 100)))
           (cond ,@(mapcar (make-guarded-commands die-roll)
                     clauses
                     cumulative-distribution)))))


;;; Code for all to read

;;; The point of the macro definition was to permit the
;;; four options at
;;; https://www.thebigquestions.com/2010/10/20/the-noble-savage
;;; to be coded cleanly

(defun 1a ()
  (small-win))

(defun 1b ()
  (percentage-case
    (89 (small-win))
    (10 (big-win))
    (1 (big-regret))))

(defun 2a ()
  (percentage-case
    (11 (small-win))
    (89 (no-win))))

(defun 2b ()
  (percentage-case
    (10 (big-win))
    (90 (no-win))))
Read on →

What’s Special About Lisp?

One thing that bothers me with Lisp is that I can’t seem to find example of this alleged superiority of the S-expression way. I get the flexibility that you can gain by using macros and defining language on the fly, but it looks to me this is not as important as it might seem.

  • You can fake it anywhere else with more or less work
  • Idea is the main problem, not the elegance of future solution
  • When you are not “smart” you work harder to compensate - that is usually enough (just check out Russian vs. German tactic about producing tanks in WW2)

Best answers about using macros I was able to find was: look in Lisp you can do thing like RSpec in 20 minutes. My point is that I have that in Ruby. I know it might not be as elegant as in Lisp but if someone else does the hard work to bring me DSL that I need, what is advantage of doing it myself. The problematic part of RSpec was idea. I would never think of such a great idea in the first place. Coding of it is irrelevant.

Read on →

Lisp vs. Smalltalk vs. Ruby

I took first part of example from Peter Siebel book ‘Practical Common Lisp’.To understand it better I’ve tried to port it to Ruby and Smalltalk and see what I like and dislike about each. Bare in mind that Lisp is straight from the book written from a crazy smart person as far as I am concerned, and that my knowledge of all three languages is minimal.

Lisp

(defun make-cd (title artist rating ripped)
  (list :title title :artist artist :rating rating :ripped ripped))

(defvar *db* nil)

(defun add-record (cd) (push cd *db*))

(defun dump-db()
  (dolist (cd *db*)
    (format t "~{~a:~10t~t~a~%~}~%" cd)))

(defun save-db (filename)
  (with-open-file (out filename
             :direction :output
             :if-exists :supersede)
    (with-standard-io-syntax
      (print *db* out))))

(defun load-db (filename)
  (with-open-file (in filename)
    (with-standard-io-syntax
      (setf *db* (read in)))))
  • What I like :

    • Property list syntax when making-cd
    • Cool trick while dumping-db on the screen (list in list)
    • Emacs doing auto indentation on my behalf
  • What I dislike:

    • Not used to Polish notation
    • That cool trick is cryptic
    • File open - way to many params for default scenario to do
    • add-record and make-cd two functions (It’s speculation, probably right but I like to stick to YAGN until proven otherwise). I get a feeling that this kind of speculation is part of Lisp culture (everything is generic), and I dislike it

Ruby

$db ||= []

def add_record(title, artist, rating, ripped)
  $db << { title:title, artist:artist, rating:rating, ripped:ripped }
end

def dump_db
  $db.each do |cd|
    cd.each {|key, value| puts "%-10s%s" % [key.to_s.upcase ,value] }
    print "\n"
  end
end

def save_db(file_name)
  open(file_name, 'w') { |file| file.puts $db.inspect }
end

def load_db(file_name)
  $db = eval(open(file_name) {|file| file.read})
end
  • What I like

    • syntax for hash in add_record that is emulating keyed params from Lisp/Smalltalk
    • concise (perlisms)
    • conventions (implicit self, no parentheses…)
  • What I dislike

    • def/end is kinda ugly and eating up space, most noticeable for one liners. (Only thing worse from this would be Python ‘indentation’, it feels weird looking at Python code)
    • I miss Smalltalk blocks [] that I could be able to end on same line
Read on →

Enterprise Menu

You can check out emenu in action here.

If you have many options that are dependant on user role or licence you may want to try out what emenu has to offer. Main idea is to pull out menu definition in ruby based configuration.

  menu.item :orders do
    item :new_orders, cars_path
    item :look_at_orders do
      item :last_month_orders , last_month_orders_path
      item :all_orders, all_orders_path
    end
  end

I’ve opted out for internal DSL so it’s pure ruby, and you can add conditions as needed.

menu.conditions do
  item :really_neat_stuff, neat_path if user.admin?
  item :same_old_boring_crud, crud_path
end

I had a pretty complex html to generate, and I suspect you basically need it that way for anything that needs to look remotely normal. That’s why I’ve ran away from templates to code-based generation. It is straightforward to change code to support your specific styling needs.

Composite vs. Surrogate Key a Battle to the Death

Recently while reading “Enterprise Rails” I’ve revisited some of mine assumptions about data modeling. I tend to go from one camp to another, only thing constant is me dissing the wrong camp.

One thing in that book struck a cord with me.

Database as a fortress!
When you think about it, data is most important asset that you have. Taking that and a fact that if you are building anything remotely big you will not have one entry point(application) but more likely many, all of a sudden Rails way of being DRY and only defining data validations in application layer seems naive. I am not saying it cannot work, all I am saying that it is little naive.

Read on →

Test Driven Development By Example

I’ve made 20 minutes long screencast demonstrating my TDD technique.

I clearly remember the day that test driven development clicked with me although it was seven years ago. I had such a hard time understanding the motivation and the technique. It was Ron Jeffries article about (his bellowed example) bowling where he did test driven play-by-play. I remember thinking the whole time that he is crazy to go in such a small steps… but then in a split second it became crystal clear that it really is the fastest way to do it. The tests enabled him to be brave and to pull himself out of a really bad situation.

Although I knew the theory, it took a good example for me to get it.

Interestingly later on I’ve read that Robert C. Martin (aka Uncle Bob) had the same feeling when Kent Beck was showing him the technique. Anyhow I’ve slapped together a screencast that can explain why and how to do TDD from my perspective. It’s not much, but at least it’s original :)

English is obviously not my native tongue… so I end up breaking my tongue a lot… but other than that I think it’s rather educational. It you are still up for it you can check it out here. Just make sure to watch HD version…

Pay attention how I didn’t listen to tests in the beginning, and how completely lost I was because of it, trying to fix the thing that I thought was a problem. The poor test was yelling at me the whole time so I finally manged to figure it out!
require File.join(File.dirname(__FILE__),'node')

describe Node do
  it 'should be able to open' do
    subject.open
    subject.opened?.should == true
  end

  it 'should be able to close' do
    subject.close
    subject.opened?.should == false
  end

  it 'should be open by default' do
    subject.opened?.should == true
  end
  ....

Recording a Screencast on Linux (Ubuntu 11.04 in Particular)

If you would like to record a screencast on Linux (especially code intensive ones) situation seems to be rather hairy:

  • Desktop recorder
    • working alright except for full screen
    • often freezes while recording
    • if you can be certain that you will not loose what you are recording it’s acceptable for smaller regions
  • XVidCap
    • audio capturing is not working in my case
    • can’t record full screen on my not so lame i5 SSD-ed laptop
    • seems like a really good candidate if only I could get audio to work
  • Istanbul
    • not even showing up (because of bug related to Ubuntu 11.04 Unity)
    • with Ubuntu Unity turned off screencast result is flickery
  • VLC - yes you can record with it (stream)
    • result is flickery

So what can you do… cry for Camtasia or buy Mac (I am reading this ‘steal it from my wife’)…before going any further please check out the result.

Yes, it turns out you can use FFmpeg.
ffmpeg -f alsa -ac 2 -i pulse -f x11grab -r 15 -s 1366x768
-i :0.0 -acodec pcm_s16le -vcodec libx264 -vpre
lossless_ultrafast -threads 0 screen_cast.mkv

Why didn’t I know this? I don’t know but it took me a couple of hours to find it… so I am sharing.

The only bad thing about this is when I see the other stuff (as if FFmpeg were not enough) that Fabrice Bellard did, all of a sudden I wanna hide under a rock… his latest project of booting Linux in .js is simply mind blowing…

Rake 0.9 Is Breaking Rails 3

Rake version 0.9 is currently breaking Rails. Error is occurring every time a rake task is invoked:

rake aborted!
undefined method `task

I am sure that patch is on its way. Until that you can use Rake 0.8.7 or monkey patch Rakefile like is’t originally suggested here

module ::NameOfYoursRailsApp
  class Application
    include Rake::DSL
  end
end

module ::RakeFileUtils
  extend Rake::FileUtilsExt
end

Installing Command-T Vim Plugin on Ubuntu 11.04 With Ruby 1.9.2

If you can’t get Command-T plugin to work, or you can get it to work but it breaks Vim frequently with the friendly message noted down, than this post might help you out of your misery…

Vim: Caught deadly signal SEGV
Vim: Finished.

Segmentation fault

I’ve determined two main causes for couple of hours of grief

  • Ubunutu 11.04 Vim build version is 7.3.35 and it has kinda flaky Ruby support with nasty habit of crashing Vim in combination with Command-T on some distributions (Command-T is partially written in Ruby)
  • I have multiple Ruby versions (via RVM). Vim was build with 1.8.7, and I was ‘making’ plugin with 1.9.2 Ruby binding. It turns out this was not such a good idea

First off all you need newer Vim build than the one that is present on Ubunutu 11.04 (you need to have Mercurial installed in order to fetch Vim, I guess Moolenaar has to use Mercurial, since he is employed at Google). Easiest way is to compile it from source, while enabling Ruby support at the same time. I had some strange problems with build version 189 so I’ve chosen build 154 (As I’ve said Ubuntu default is build 35, and they fixed the bug somewhere in between build 35 and 154.)

hg clone https://vim.googlecode.com/hg/ ~/vim
cd ~/vim
hg update -C v7-3-154
./configure --with-features=huge  --disable-largefile \
            --enable-perlinterp   --enable-pythoninterp \
            --enable-rubyinterp   --enable-gui=gtk2 \

make
sudo make install

Check the Vim:

vim --version | grep ruby
Make sure that after checking Vim version you see +ruby, and also make note of version of ruby used (1.8.7, 1.9.2), I got it to work with both but it is important to use same Ruby version when installing the plugin…

Get Command-T script from Command-T Vim page. If you’ve already tried to install Command-T you can implode it (you could say remove it, but it doesn’t sound nearly as cool)

rm -rf ~/.vim/ruby
rm -rf ~/.vim/doc/command-t.txt
rm -rf ~/.vim/plugin/command-t.vim

Then open plugin installer with Vim

vim ~/command-t-1.2.1.vba

While in Vim type :so % into command line and get out of Vim with :q to finish the install

cd ~/.vim/ruby/command-t/
ruby extconf.rb
make

Enjoy the Command-T magic, make sure to steal some cool mappings from Destroy All Software. I have particularly ‘Custom Rails mappings’ in mind…

Copyright © 2019 - Kresimir Bojcic (LinkedIn Profile)