Last active
July 2, 2024 06:44
-
-
Save evtn/2870a67981414d2575846472c39a214e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from typing import Callable, Iterable, TypeVar | |
| # In Python, for loops (and, by extension, comprehensions) | |
| # actually have the ability to define variables and write into keys and attributes of objects. | |
| # | |
| # This ability is unintuitive but also creates a weird set of things possible with loops | |
| T = TypeVar("T") | |
| R = TypeVar("R") | |
| # Here, we build a mapping from an iterable, with indices being the keys, and values — well, values. | |
| # It's a simple task, but here we don't actually do anything for that to happen: | |
| def iterable_to_dict(iterable: Iterable[T]) -> dict[int, T]: | |
| result: dict[int, T] = {} | |
| # it's the loop header that is mutating result | |
| for i, result[i] in enumerate(iterable): | |
| pass # we do nothing here | |
| return result | |
| print(iterable_to_dict("abcd")) # {0: 'a', 1: 'b', 2: 'c', 3: 'd'} | |
| # Comprehensions are a core feature of Python | |
| # Here's how we can make the `iterable_to_dict` even weirder with comprehensions: | |
| def iterable_to_dict_alt(iterable: Iterable[T]) -> dict[int, T]: | |
| return ( | |
| [ | |
| # put the state into the array | |
| result | |
| # create a persistent state once | |
| for result in [{}] | |
| # add the index: value mapping | |
| for i, result[i] in enumerate(iterable) | |
| # this ensures that we append the state only once (better memory efficiency) | |
| if not i | |
| ] | |
| or [{}] | |
| )[0] | |
| # We don't even need real variables anymore | |
| # Okay, but let's do something more complex. | |
| # A generic reduce function would do: | |
| def reduce(iterable: Iterable[T], callback: Callable[[R, T], R], init: R) -> R: | |
| return ( | |
| [ | |
| # put the state into the array | |
| state | |
| # create a persistent state once | |
| for state in [{"result": init}] | |
| # get the current element | |
| for i, current in enumerate(iterable) | |
| # calculate the result | |
| for result in [callback(state["result"], current)] | |
| # update the state | |
| for state["result"] in [result] | |
| # this ensures that we append the state only once (better memory efficiency) | |
| if not i | |
| ][0]["result"] | |
| if iterable | |
| else init | |
| ) | |
| # sum | |
| print(reduce(range(100000), lambda p, c: p + c, 0)) # 4999950000 | |
| # And this does look weird, but what about the same thing, minified? | |
| reduce_inline = lambda t,f,p:([s for s in[{0:p}]for i,c in enumerate(t)for r in[f(s[0],c)]for s[0]in[r]if i==0][0][0]if t else p) | |
| # well... | |
| # the good thing is, this is all done in one expression | |
| # the bad thing is — I'm going to hell for this |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.