Skip to content

Instantly share code, notes, and snippets.

@mattneary
Last active August 29, 2015 14:24
Show Gist options
  • Save mattneary/19e6481eed94a4e9ff41 to your computer and use it in GitHub Desktop.
Save mattneary/19e6481eed94a4e9ff41 to your computer and use it in GitHub Desktop.
Python Fetch Functor
import requests
import threading
# a -> ()
def out(x):
print x
def get(url, cb):
def work():
cb(requests.get(url).json())
return work
# (b -> c) -> (a -> b) -> a -> c
compose = lambda f, g: lambda x: f(g(x))
# type Fetch a = (a -> ()) -> ()
# String -> Fetch JSON
fetch = lambda url: lambda cb: threading.Thread(target=get(url, cb)).start()
# (a -> b) -> Fetch a -> Fetch b
fmap = lambda f: lambda r: lambda cb: r(compose(cb, f))
# Fetch JSON
fetchGist = fetch('https://api.github.com/gists/5b2bb0e6ef1fe7deb976')
# JSON -> String
getContent = lambda x: x['files']['poem.txt']['content']
# String -> ()
printLen = compose(out, len)
# Fetch ()
fetchContent = fmap(compose(printLen, getContent))(fetchGist)
# ()
fetchContent(lambda x: x)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment