3 ms·
I can't believe I never knew about this. This is super cool, and although yes it's potentially subject to readability problems, it allows you enough freedom to
by kragen 5y ago
I can't believe I never knew about this. This is super cool, and although yes it's potentially subject to readability problems, it allows you enough freedom to design your desired notation that you might be able to come up with something that's more readable than standard Python for a given embedded DSL rather than less, in particular for associative operators. And it's way easier to search the documentation for than non-alphabetic operators.
Consider Tk-style page or GUI layout, as in http://canonical.org/~kragen/sw/dev3/alglayout.py http://canonical.org/~kragen/sw/dev3/alglayout.py (in OCaml I used `:` and `..` http://canonical.org/~kragen/sw/dev3/alglayout.ml http://canonical.org/~kragen/sw/dev3/alglayout.ml):
pageheader |above| (leftsidebar |beside| vstack(*abstracts)) |above| footer
My Python version used `|` and `-` and `~`, which I think is super confusing, and I think would probably be a lot better with named infix operators like the above:
vr, hr, dhr = [~String(s) for s in '|-=']
left = (('Chapters' | String('')) - hr -
(chapno | vr | [t | ~String(' . ') for t in titles]))
pages = 'Page' - hr - pnos
table = ~('*' - hr - '*' - ' ') | ' ' | dhr - (vr | left | pages | vr) - dhr
This produced this layout:
* ========================================================================
- |Chapters Page|
* |----------------------------------------------------------------------|
| 0.|The unchained desert of the singing perfume. . . . . . . 1|
* | 1.|La brisa ilusa de la neblina abandonada. . . . . . . . 43|
- | 2.|The smiling murder within her young daughters. . . . . . 80|
* | 3.|Aquella hija tortuosa de su precipicio árido. . . . . . . 96|
| 4.|False villages of the foolish monks. . . . . . . . . . 114|
* | 5.|El roble sacrílego de mi sacerdote desmoronado y enlutado. . 124|
- | 6.|The brazen river of his filthy, fallen ivory. . . . . . . 166|
* | 7.|The laughing monster of his humble scream. . . . . . . . 203|
| 8.|The rough tattoo of his unchained abomination. . . . . . 219|
* | 9.|Los perfumes silenciosos de un árbol monstruoso muriéndose. . 237|
- |10.|The sweet tower of the smooth fire. . . . . . . . . . 247|
* ========================================================================
Such named infix operators would also take care of the coercions; instead of writing `('Chapters' | String(''))` I could write `'Chapters' |beside| ''` and let `beside` do the coercion.†
— ⁂ —
Another application for weird infix operators is computation on a lattice (in the partial-order sense):
w |meet| x |meet| (y |join| z)
Boolean bitvectors are a lattice, so it might be forgivable to overload the bitwise operators for general lattice elements, but I would argue that the following notation is less readable than the above:
w & x & (y | z)
— ⁂ —
How about binary relations, as in http://canonical.org/~kragen/binary-relations http://canonical.org/~kragen/binary-relations?
neighbor = (x |compose| near |compose| x.T) |union| (y |compose| near |compose| y.T)
You could usefully apply this to composing SQL queries, for example. Instead of writing
"""SELECT clients.name, orders.id
FROM clients, orders
WHERE clients.id = orders.clientid
ORDER BY clients.name
"""
you could reasonably write
clients.name |product|
(clients.id |compose| orders.clientid.T
|compose| orders.id)
or, with better namespace management, maybe just
name |product|
(clients.id |compose| clientid.T |compose| orders.id)
— ⁂ —
Maybe 3-D modeling, as in https://dercuano.github.io/notes/algebraic-graphics.html https://dercuano.github.io/notes/algebraic-graphics.html? Extrusion along a path is associative even if it contains rotation, right?
square = xline |extrudedby| yline
cube = square |extrudedby| zline
twistedcube = square |extrudedby| (zline |during| zrotate(2*math.PI))
— ⁂ —
Maybe melodies, as in http://canonical.org/~kragen/synthgramelodia http://canonical.org/~kragen/synthgramelodia and http://canonical.org/~kragen/sw/aspmisc/gramelodia.py http://canonical.org/~kragen/sw/aspmisc/gramelodia.py? The operators defined there aren't associative but they could be.
theme = bell.twice |then| bell.fifthlower |then| rest
tune = theme.twice.octavehigher |during| theme.slower
rhythm = boom |then| whack |then| boom.twice.faster |then| whack
play(rhythm.repeatedly |during| tune)
Of course the real test is not whether you can write code to say what it means straightforwardly; that's necessary but not sufficient. The real test is whether you get reasonable error messages when you write something like
play(rhythm.repeatedly |during| then| tune)
____
† In that particular case, if the code had a purpose other than being an experiment in ultra-minimalism, `ljust('Chapters')` would have been better: the particular goofy thing I was doing was that string boxes right-justify their strings if they get allocated more space than they need, while horizontal concatenation boxes allocate any extra space they receive to their rightmost box, so you can get left-justified strings by just stacking an empty string box to their right.