Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save eldritchideen/e56a9dc5ad9b9ec9967a54bd5bd2346e to your computer and use it in GitHub Desktop.
Save eldritchideen/e56a9dc5ad9b9ec9967a54bd5bd2346e to your computer and use it in GitHub Desktop.
clojure.core/partition and clojure.core/partition-all in Python
def take_while(fn, coll):
"""Yield values from coll until fn is False"""
for e in coll:
if fn(e):
yield e
else:
return
def partition(n, coll, step=None):
return take_while(lambda e: len(e) == n,
(coll[i:i+n] for i in range(0, len(coll), step or n)))
def partition_all(n, coll, step=None):
return (coll[i:i+n] for i in range(0, len(coll), step or n))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment