Search Results: "cklin"

29 December 2005

Decklin Foster: Ruby Love

Matthew Palmer writes about the goodness that is Ruby. I, too, have just recently been won over, and am stumbling along with the online docs. In fact, this can be done in one line:
STDIN.readlines.sort_by   rand  .each    line  puts line  
But this is slow, as sort_by needs to be at least O(n lg n), and probably not perfectly random (I suppose you could do rand <=> 0.5, but that s not perfect either). What to do? We can implement a proper Fisher-Yates shuffle in a function, but that takes a couple lines. Can we still perform the actual read-and-print in one? Yesterday, why wrote something that really made me stop and go wow . We can attack the random sort problem in a similar (if more boring, as there are no lambdas to pass in) way, by simply adding another method to Array:
class Array
    def swap!(a, b)
        self[a], self[b] = self[b], self[a]
        self
    end
    def shuffle!
        each_index do  i 
            j = i + rand(length - i)
            swap!(i, j)
        end
        self
    end
end
STDIN.readlines.shuffle!.each    line  puts line  
Now that is cool.

9 December 2005

Decklin Foster: Permalinks Fixed

I had a very silly typo in my Atom 1.0 flavor (flavour?). Permalinks should work again. If this causes everything to rehash and thus flood the planet, sorry. Once PyBlosxom 1.3 is out, Atom will be native and I won t have to worry about not catching thinkos like this for months...

Jesus Climent: Timeline

08:02am: Decklin Foster mentions dropping FreeDB in favour of MusicBrainz, and abcde in favour of jack. 09:48am: I wonder if adding MusicBrainz support on abcde would be a bad idea. 05:20pm: Decklin Foster points me to a python script which provides the needed interface. 2 years later: I add MusicBrainz to abcde. No. Just joking. I will work on it during the next few days. Or in debconf-es2. Cheers!

8 December 2005

Decklin Foster: Really Bad Joke

I think #342502 would have read better in poem format:
this is just to say

i have recompiled
the bonnie++
which was linked
against libstdc++5

and which
you were probably
saving
until etch freezes

forgive me
g++-3.4 has a new ABI
so portable
and so fast
Somebody shoot me for doing that, please.

Jesus Climent: MusicBrainz

Decklin Foster: Why not writing an abstraction of a MusicBrainz plugin for jack and we can use it also in abcde?

4 December 2005

Clint Adams: She comes waltzing in the nighttime made of wings

She is composed of sharp angles. Her limbs are thin and long, and her elbows curve into rose thorn-like points. Her face is a smooth void of black, but you can make out the prominent cheekbones and sharp chin. Her hair is shiny and silver like spiderweb. Slim braids run from the crown of her head and wind up around her horns resulting in two horn-shaped mounds of braids on the top of her head. The rest of her hair is braided and looped in the back. Her fingers are long and thin with four joints. She wears a midnight black velvet gown with a high silk fin-like collar rising out from behind the neck. The bodice is fitted tightly and has an angular U-neckline that dips down into a V. The sleeves are long and tight, and form a V to reach the middle finger and cover the knuckles. The skirt clings to the hips and fans out at the bottom with a shredded hem, revealing red satin underneath; the effect appears remniscent of flames. It has a long train that trails in the back. Silver spiderweb lace hangs from the sleeves like fins and from the collar like a cloak. It is for you.

24 November 2005

Clint Adams: Musical Personality Disorder

Decklin, MPD looks pretty interesting. I have filed a wishlist bug on vux on the presumption that integrating it with mpd would be peachy keen. Still, mpdscribble is conspiring against me. That it can't get MusicBrainz IDs is a minor irritation. The more significant problem is that it doesn't work at all. For every song, it whines about skipping, like this:
new song detected (Leonard Cohen - Hey, That's No Way to Say Goodbye)
skipping detected, not submitting.
I'm not doing any skipping, and crossfade is set to '0'. I continue to use my happy NIH pseudosolution.

21 November 2005

Daniel Burrows: A type-system hack

WARNING: nothing in this post has been implemented or tested, except maybe at a minimal proof-of-concept level ("it compiles!"). It's just some musings on how you can make the computer do your work for you.

I was reading a defense of Hungarian notation when I started to get a familiar notion tickling the back of my brain. The author was explaining that the original idea behind Hungarian was not to replicate type information in variable names, but rather to include some sort of usage information: one example would be giving a string value holding a value that is untrusted the name utName. While it's more reasonable than lpschwzMyVariable, this suggesion made me suspicious that the author was really hacking around suckiness in the C language.

Tagging values with usWhatever and then verifying that you never mix usBlah with Blah isn't just tedious and error-prone, it smells a lot like doing work by hand that the computer ought to do for you. It seemed to me that it ought to be possible to "tag" unsafe values at the type level and let the language's type-checker sort things out. The simple approach is of course to define something like this:

newtype Unsafe t = Unsafe t


NB: I'm using Haskell because it was the first language that sprang to mind when I thought "type system hack".

This newtype declaration lets you attach an Unsafe tag to any type. Presumably the type signatures of routines that return untrusted data (such as a hypothetical getHTMLFormValue or a routine to retrieve previously entered form values from a database, if they aren't sanitized first) should be modified; for instance, if the signature of getHTMLFormValue was

getHTMLFormValue :: HTMLForm -> String -> String


it should instead read

getHTMLFormValue :: HTMLForm -> String -> Unsafe String


To create a new Unsafe value you simply apply the Unsafe constructor to the value:

getHTMLFormValue = Unsafe (oldGetHTMLFormValue)


To unwrap Unsafes you use pattern-matching:

let (Unsafe unwrapped) = wrapped in ...


So, this sort of works, but it's unsatisfying on several levels:



While it may not be perfect -- it only makes sure that you don't accidentally violate safety, and doesn't stop you from deliberately doing something dumb -- this approach feels a lot safer to me than ad-hoc mangling of variable names. I used Haskell because you can do type hacks fairly easily in it, but I don't see why you couldn't play a similar game (albeit much less conveniently) in C++, or even in a dynamic language like Python. Of course, Python would give you runtime errors about safety violations instead of static ones, but that still beats getting runtime security holes.

I would add as a hypothesis that it should be possible to express just about any variable-name-mangling technique whose goal is to "make wrong code look wrong" in the type system, provided your type system doesn't suck; and that doing so is preferable to doing it manually, since it means that a lot of repetitive error-checking can be handled by the computer. And since the computer is better than you are at verifying formal invariants, forcing it to handle them whenever possible is always a good idea.

20 November 2005

Matthew Palmer: The Fun Never Stops With Ruby

Well, there's nothing like posting a really rubbish program to your blog to find out the right way to do it. Firstly, Philipp Kern has come up with another, even shorter version than his version I posted in the update to my original Ruby post, this time blowing me away with the strangeness of explicit casts in Ruby. Apparently, casting an input stream to an array produces an array with one-line-per-element, as in this example:
 STDIN.to_a.sort_by rand .each l puts l 
As Philipp warns, however: "Ruby is full of fun and power; one must watch out that the scripts remain readable". For a fairly toy example, this solution is still fairly readable, however I can see hair growing on this thing if your program grows large. Next, Pierre-Charles David showed his qualifications for becoming a co-author of the next edition of the Pickaxe (AKA "Programming Ruby: The Pragmatic Programmers' Guide"), with his example and explanation. He's kindly given permission for me to reproduce his e-mail in it's entireity, since it's too good to chop up:
Here is the shortest and most readable version of your "randomize stdin" script I can think of:
  #!/usr/bin/env ruby
  print ARGF.readlines.sort_by   rand  
 
ARGF is a special variable representing either stdin or, in the case where file names are passed on the command line, the content of these files (think of Perl's <>). ARGF behaves like an IO object (although I think it is not one). In particular, it responds to the #readlines method which splits the receiver's content into lines and returns an array. In other words,
  lines = ARGF.readlines
 
is the idiomatic way of doing
  lines = []
  while line = gets() do
    lines << line
  end
 
in your example. The next part (sort_by) uses a new form of the sort method, which I think is not documented in the Pickaxe. It implements the Schwartzian transform technique. In essence, a random number is associated to each element of the array, which are then sorted based on this index. As a general style remark, note that although Ruby supports "while" and "for x in y" loops, they are rarely used, in favor of iterators and/or higher order methods. For example, to gather all the input lines in an array without using readlines, one might write
  lines = Array.new
  ARGF.each_line    l  lines << l  
 
or
  lines = ARGF.inject([])    lines, l  lines << l  
 
Hope it helps, and welcome to Ruby!
Pierre, your explanation definitely helped, and I hope it helps others too. The third person to help me out is Decklin Foster, through a post in his blog. What particularly caught me about his solution was the neat demonstration of another feature of Ruby which I think I will really learn to love: the ability to dynamically add methods to an existing class. "Aiee!" I hear all the purists scream -- and I can see the potential for severe misuse, too. But, since every good idea seems to get deeply perverted, we may as well have some real useful features to pervert while we're at it. Decklin has the clever idea of adding methods to the Array class that shuffle the elements of the array in a random fashion. His code is beautiful in it's simplicity:
 class Array
    def swap!(a, b)
        self[a], self[b] = self[b], self[a]
        self
    end
    def shuffle!
        each_index do  i 
            j = i + rand(length - i)
            swap!(i, j)
        end
        self
    end
 end
He then goes on to rewrite my bumbling example in probably the most comprehensible manner I've yet seen:
 STDIN.readlines.shuffle!.each    line  puts line  
Decklin writes, "Now that is cool.". I couldn't have put it better myself. My thanks to Philipp, Pierre-Charles, and Decklin, for helping me along the Ruby path. So, two new Ruby Rules for me to live by: Hmm, is it possible that Ruby is nothing more than Functional Programming by Stealth? Aiee!

27 October 2005

Randall Donald: Tackling Bugs

I've been cleaning up many of my bug reports this week. A new upload will be happening soon along with legacy packages. I'm also getting an amd64 system so more attention will be paid to that side. The libGL.so issue is still a pain in the ass. I thought I had it figured out but it wouldn't upgrade cleanly so back to the drawing board. And I'll finally be uploading 2.6 modules. The status of the 2.4 ones is still undecided.

Next.

Previous.