This is super useful, but the font betrays that usefulness. The "fun" font is reasonable for titles but for the content, I would stick with a readable sans-serif font, or a monospace for code snippets.
I don't mean to be negative, but that is an impediment to me using it
One pagers are really underrated in general. I have a ruby one for whenever I take a flight that doesn't have wifi. I quickly load it before takeoff and read it joyfully, always feeling refreshed and usually learning something(s) new.
Also there a small typo in: `"".join(["a", "B", "c"])` # 'abc'`.
This sounded like it'd be fun to implement so I took a quick stab at it, it supports unpacking tuples to any number of _ in any position, including with kwargs.
class Placeholder:
def __init__(self) -> None:
pass
_ = Placeholder()
class Arrow:
def __init__(self, func):
self.func = func
def __call__(self, *args, **kwargs):
if not any(isinstance(arg, Placeholder) for arg in args) and \
not (kwargs and any(isinstance(arg, Placeholder) for arg in kwargs.values())):
return self.func(*args, **kwargs)
else:
placeholder_indices = [i for i, arg in enumerate(args) if isinstance(arg, Placeholder)]
placeholder_kwargs = {k: v for k, v in kwargs.items() if isinstance(v, Placeholder)}
original_kwargs = {k: v for k, v in kwargs.items() if not isinstance(v, Placeholder)}
def new_func(new_args):
assert len(new_args) == len(placeholder_indices) + len(placeholder_kwargs), "ERROR: Wrong number of arguments"
return self.func(
*[new_args[placeholder_indices.index(i)] if i in placeholder_indices else arg for i, arg in enumerate(args)],
**{k: new_args[len(placeholder_indices) + i] for i, k in enumerate(placeholder_kwargs)},
**original_kwargs)
return Arrow(lambda *new_args: new_func(new_args))
def __ror__(self, other):
if isinstance(other, tuple):
return self.func(*other)
return self.func(other)
@Arrow
def add(x, y, z):
return x + y + z
@Arrow
def multiply(x, y):
return x * y
@Arrow
def subtract(x, y):
return x - y
@Arrow
def add_one(x):
return x + 1
print((1, 5) | add(_, 3, _) | add_one | subtract(_, 2) | subtract(50, y=_))
Some of you may get annoyed with this, but python variables don't "hold" values as stated in this one-pager; they are just references to objects in memory. The same goes for dictionaries; they don't "hold" key-value pairs. They are references to hash-table objects in memory.
A variable is not a container. That would imply you can only put an object in one container. In Python, a variable is a label for an object. There can be multiple labels on a single object. If the object is mutable, you can tell it's the same object (or use id()).
Yeah... Definitely not correct and pretty misleading in it's incorrectness. Also that font is a bit nightmarish. .
Don't get me wrong, it's really decent overall but that variable as a container is the first thing you see and that font is awful.
As a programming hobbyist, one syntax I really like in Python, compared to C# and JS I've used, is it's for-in loop.
90%+ loops I used in my coding is to iterate elements in a list-like object. It feels very natural to use "for x in arr: do_sth(x)". And the same pattern can be reused with your typical "step increase by 1" loop by using `for i in range(LEN)`, instead of having to memorize another different pattern of `for (i=0; i<LEN; i++)`.
I particularly hate JS's forEach structure. for one, it doesn't follow how I think, and it's different from the other form of loops. And for two, I hate to type these pesky symbols (`a.forEach(e=>{doSth(e);})`) than just word(s).
Furthermore, JS also introduced `for in` and `for of`, which are great, but they're somewhat counterintuitive to my taste. You use `for ele of array` and `for index in array`, sounds fair. To iterate keys of objects, you use `for ... in`, but there is no `for ... of`. To make things worse, not all Array-like objects even support it. I find myself constantly "re-discover" (by that I mean I've google'd it multiple times before) the trick of things like `[...arr]`, and I hate it.
Some disclaimer: I know above is purely my preference, and not trying in anyway to say one is superior than the other. And I imagine for experienced programmers, they probably don't even think of all of these when using, it's just muscle memory. But for me, someone who code in Python/JS about half and half, I still struggle with, and have to google, basic syntax and structure in JS frequently, while I can do similar things without thinking in Python since when I was very new to the language. (And I know Python didn't invent these syntaxes.)
I don't mean to be negative, but that is an impediment to me using it