Tuesday 5:10 p.m.–5:40 p.m.
Oneliner-izer: An Exercise in Constrained Coding
Chelsea Voss
- Audience level:
- Intermediate
- Category:
- Python Core (language, stdlib, etc.)
Description
Abstract
Have you ever been tempted to write a giant list comprehension that, though unwieldy and unreadable, accomplishes exactly what you need it to do? Oneliner-izer is a project that pushes that giant list comprehension to its natural extreme: it's a "compiler" that can convert most Python programs into one big line of code with the same behavior.
When I say "most", here's what I mean: with
and finally
are the two major open problems, but as for everything else – def
, while
, for
, if
/elif
/else
, class
, try
/except
/else
, import
, break
, continue
, raise
, assert
, del
, exec
– these language features can all be implemented within one line of code! In fact, this is enough for us to oneliner-ize the code of Oneliner-izer itself, as seen here.
In this talk, I'll explain how Oneliner-izer oneliner-izes. We'll describe how to oneliner-ize simple blocks of code, then move up through control flow, loops, recursion, and beyond. Along the way, we'll also learn about a bunch of quirks and oft-unexplored corners of Python, as I share how we can use those corners to work around each challenge. You'll learn about how we used continuation-passing and the Y combinator, and you'll come away from this talk with a new appreciation for the wily list comprehension and the humble lambda expression.