30
August
2008

Emacs: kill-matching-buffers-by1

Nothing fancy here, just some emacs functions that assist in killing multiple buffers based on buffer name or file name. I wrote this to help me cope with the large number of log files I so frequently open in emacs (where else?).

(defun act-on-buffers (pred action)
  (let ((count 0))
	(dolist(buffer (buffer-list))
	  (when (funcall pred buffer)
		(setq count (1+ count))
		(funcall action buffer)))
	count))
 
(defun kill-matching-buffers-by (acc)
  "Kill buffers whose name matches the input"
  (let* 
	  ((re (read-string (format "kill buffers matching: ")))
	   (match (function 
			   (lambda (buf) 
				 (string-match re (funcall acc buf)))))
	   (kill #'(lambda (buf) (kill-buffer buf)))
	   (count (act-on-buffers match kill)))
 
    (message "%d buffer(s) killed" count)))
 
(defun kill-matching-buffers-by-name ()
  (interactive)
  (kill-matching-buffers-by 'buffer-name))
 
 
(defun kill-matching-buffers-by-filename ()
  (interactive)
  (kill-matching-buffers-by #'(lambda (b) (or (buffer-file-name b) ""))))
26
March
2008

Easier path construction using overloaded /3

At long last I found a justifiable use for operator overloading. Recently I’ve been writing quite a bit of rake files which involved a lot of path construction, like

 path = "#{all}/#{along}/#{the}/#{watch}/#{tower}"

Noticing all those extra characters required to create the path string I realized that the / operator can used for concatenation in this specific use case

class String
  def /(other)
    File.join(self,other)
  end
end

and then we can write a much cleaner version

 path = all/along/the/watch/tower

I wouldn’t advocate the usage outside of a rake file or other path construction intensive code, but for these specific scenarios it is pretty sweet.

Also posted on dzone snippets

11
February
2007

Silly little Ruby let0

It occurred to me yesterday, while playing with the Ruby script I was writing, that it would be useful to have a let facility in Ruby, which would enable the following code

def foo
  # do something really exciting with the object 'master_foo'
  # now...
  master_foo.select { |x| x>3 }.let do |mf|
    puts "#{mf}, #{mf.length}"
  end
end

What is it good for? Well, instead of introducing a new local variable and using assignment to hold a temp value (like the result of the select call above), we can add a let method that simply yields itself to the block it gets. Something like

class Object
  def let(&block)
    yield self
  end
end

which enables the following silly example

"hello cruel world".split.let do |p|
  puts "The number of words in '#{p.join(' ')}' is #{p.length}"
  puts "and the second one is '#{p[1]}'"
  p
end

instead of

p = "hello cruel world".split
puts "The number of words in '#{p.join(' ')}' is #{p.length}"
puts "and the second one is '#{p[1]}'"

Well guess what? The upcoming Ruby 1.9 release a new ‘’tap’’ method is included that is similar, though not identical.

25
September
2006

Afraid of choosing the right language0

Got to admit it, I’m a language coward. 3 times in a row I have chickened out and didn’t go with the programming language I knew was the right one for the job. Even though my recommendation would probably be accepted. And it gets better – once it was in my company, where I was the CTO (and founder). What a wimp.

It occurred to me after reading Yegge’s interesting but long post (aren’t they all) of his now (or was it then) favorite programming language. I, just like him, was and still am, on a holy-grail quest of finding the ultimate programming language. Elegant, efficient, with lots of libraries and easy FFI, concise, extensible and multi-paradigm. And guess what? I’ve found it, only to discover that even I can’t stand behind it and advocate it to my fellow (or subordinate) developers. After all, who enjoys these “not another crazy idea” look in their eyes. Just like with any leader/follower situation, the leader’s biggest fear is to lose his followers. And how easy that is when you come with this fancy, academic language which nobody really uses and will look plainly odd on their resumes. Does it really matter it implies a better programming experience? Probably not.

Ah, wonder what was that language? Well it wasn’t any language in particular although more than anything it was Scheme. And oddly, scheme is pretty familiar to many people much more so than the arcane OCaml or Erlang.

Will I ever be manly enough to do the right thing? Sometime I get carried away in dreams of an Utopian programming position when I will be expected to make bold decisions and will be given enough room to go with what I feel is right. Sounds too good? Well, doesn’t that sound like a research position? And if so, will choosing the right language is limited to research and out of scope for what is considered to be real-world? Probably and sadly, yes.