Last active
September 20, 2015 08:52
-
-
Save przemek-pokrywka/f9ea873209c7810d4376 to your computer and use it in GitHub Desktop.
I've stumbled upon wonderful https://github.com/kachayev/fn.py library. One thing I missed was the ability to use `_.method(args)` syntax. Following quick hack aims to fix that. CAVEAT: it's a hack, it'll work unpredictably for cases different than `_.method(args)`.
This file contains 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
class Underscore: | |
def __getattr__(self, methodname): | |
return lambda *args, **kwargs: \ | |
lambda o: eval("o." + methodname)(*args, **kwargs) | |
_ = Underscore() | |
filter(_.startswith("A"), ["Africa", "America", "Europe"]) | |
map(_.upper(), ["hello", "world"]) | |
map(_.count("I"), ["TEAM", "TIM"]) | |
# ['Africa', 'America'] | |
# ['HELLO', 'WORLD'] | |
# [0, 1] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment