Skip to content

Instantly share code, notes, and snippets.

@0atman
Forked from anonymous/chain.py
Last active March 29, 2023 19:17
Show Gist options
  • Save 0atman/cc83e79e00f8943adcc1c475a5077e90 to your computer and use it in GitHub Desktop.
Save 0atman/cc83e79e00f8943adcc1c475a5077e90 to your computer and use it in GitHub Desktop.
clojure `->`-style functional threading
from functools import reduce
def thread(*args):
"""
Functional thread ->, inspired by clojure and https://stackoverflow.com/a/17122666/333294
Toy example that only works for functions or airity 1
"""
caller = lambda x, y: y(x)
return reduce(caller, args[1:], args[0])
@0atman
Copy link
Author

0atman commented Jan 5, 2018

>>> thread(
>>>     [1, 2, 3],
>>>     max,  # 3
>>>     str,  # '3'
>>>     len   # 1
>>> ])
1

@0atman
Copy link
Author

0atman commented Nov 4, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment