Skip to content

Instantly share code, notes, and snippets.

@0atman 0atman/thread.py

forked from anonymous/chain.py
Last active Nov 4, 2019
Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner Author

0atman commented Jan 5, 2018

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

This comment has been minimized.

Copy link
Owner Author

0atman commented Nov 4, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.