Skip to content

Instantly share code, notes, and snippets.

@przemek-pokrywka
Last active September 20, 2015 08:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save przemek-pokrywka/f9ea873209c7810d4376 to your computer and use it in GitHub Desktop.
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)`.
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