viernes, 15 de julio de 2016

Simple 'dsls' using strings

I'm currently reading The Unix Programming Environment (1st ed). At some point, the authors show how to make a terser version of ....

 It's not entirely clear when I saw this for the first time, I'd say I saw it in some norvig's python code (which I can't find anymore, if you know what I'm talking about comment please. I've spent too much time searching that code)


He was creating a regex from a string of whitespace separated words. something like this: "|".join(" ".split(words)) .

 I find super nice the fact that one can create abstractions on abstractions, or use data structures as intermediate stepping stones.

We (at least, myself) tend to usually think at one level of indirection. You can think of a Set implemented via an array, a hash, or a Bloom filter, but it's usually "I want the 'set' functionality, so I go and fetch an implementation of that functionality".  In those cases, they use intermediate data structures so that they can manipulate the information easily, and later convert it to some other data structure.

Another case where this thing appears is when creating Classes in Smalltalk, where you define the instance variables inside a string.

Object subclass: #Dog 
    instanceVariableNames:  
    classVariableNames:  
    poolDictionaries:  
    category: PBE-CIV 



In The Unix programming environment, at some point they write an awk script regarding calendars, and instead of:

days["Jan"]=31; nextmon["Jan"]="Feb" 

days["Feb"]=28; nextmon["Feb"]="Mar"

...


They do:

x="Jan 31 Feb 28 Mar 31 Apr 30...."

split(x, data)

for(i=1;i 24;i+=2){

  data[i]] = data[i+1]
  nextmon[data[i]] = data[i+2]

} 

No hay comentarios: