[FoRK] The grand scheme of things...

Jeff Bone <jbone at place.org> on Fri Nov 2 17:34:40 PDT 2007

On Nov 2, 2007, at 2:34 PM, Lion Kimbro wrote:

>   My code doodling lately has been in what I am affectionately  
> calling,
>   "AplPy," pronounced, "Apple Pie."
>   I am now in deep heresy with my friends, none of whom take me
>   seriously any more...
> ...
>   Should I want to print out all files in the directory, it's:
>   P.p("\n---\n".join(O.t(p) for p in O.ff()))

Oh yeah?  Well, check this crazy shit out.

-- snip --

#  Convenience

curry = lambda func, *args, **kw:\
     lambda *p, **n:\
         func(*args + p, **dict(kw.items() + n.items()))

class Thing(dict):

     def __init__(self, d=None):
         if d: self.update(d)

     def __getattr__(self, key):
         if self.has_key(key):
             return self[key]
         raise AttributeError, repr(key)

     def __setattr__(self, key, value):
         self[key] = value

     def __repr__(self):
         return dict.__repr__(self)

#  Parts - generic, mostly functional

def prn(s,fd=sys.stdout): print >> fd, s; return
def out(o,fd=sys.stdout): return prn(repr(o), fd)

toks   = lambda s: string.split(s)
slurp  = lambda f, p, c: [ c(toks(s)) for s in f.readlines() if p(s) ]
cks    = lambda r, s, t: r in t and not s in t
tl     = lambda l: l[1:]
hd     = lambda l: l[0]
hdl    = lambda l: [ hd(l) ]
pairs  = lambda xs: map(tuple, zip(front(xs), tl(xs)))

#  (Ab)using lambda -- notation as tool of thought, or Haskell envy?

T  = Thing
F  = lambda l: T({'f':True,  'oid':l[2], 'd':l[4], 'ts':float(l[10])})
O  = lambda l: T({'f':False, 'oid':l[2], 'd':l[4], 'ts':float(l[7])})
m  = "marker"

dif    = lambda (a, b): b.ts - a.ts   # delta t
fed    = lambda x: x.f                # x is fed
flip   = lambda a, b: a.d != b.d      # change dir
flips  = lambda l: [ b for (a, b) in pairs(l) if flip(a,b) ]

a_ = lambda l: hdl(l)
s_ = lambda l: hdl(filter(fed, l))
_s = lambda l: flips(filter(fed, l))
_a = lambda l: flips(l)

aa = lambda l: a_(l) + _a(l)
ss = lambda l: s_(l) + _s(l)
as = lambda l: a_(l) + _s(l)
sa = lambda l: s_(l) + _a(l)

def distill(os, fs):
     def mark_fed(o, p): o.f = p; return o
     fids   = [ f.oid for f in fs ]
     os = [ mark_fed(o, o.oid in fids) for o in os ]
     os.sort(lambda a, b: cmp(a.ts, b.ts))
     return os

def run(symbol, f=distill):
     fs  = slurp(open("fs"),  curry(cks , symbol, m), F)
     os = slurp(open("os"), curry(cks , symbol, m), O)
     return f(os, fs)

-- snip --

Yeah, that's a snippet of something I actually wrote recently.  And  
yeah, it actually made sense at the time. ;-)

There's one really clever bit of notational abuse in there, but...   
despite the density of the whole thing, I'm quite certain that nobody  
else could ever understand what was being accomplished with it, so in  
that sense, like the old APL saw, this was probably write-once /  
write-only code.  But --- the notation-as-tool-of-thought thing is  
actually true.  It wasn't until I hit the notational density you see  
above that I could actually conceive the solution to the problem.   
Those aa, ss, as, and sa functions are the permutations of functional  
combinations of the s and a functions on lists under list addition.

The imperative / iterative solution to the problem was MUCH more  
difficult to reason about.  Buggy, error-prone, and MUCH longer.



More information about the FoRK mailing list