Skip to content

Instantly share code, notes, and snippets.

@SegFaultAX
Last active July 11, 2016 19:40
Show Gist options
  • Save SegFaultAX/1f8900efb422888eabab to your computer and use it in GitHub Desktop.
Save SegFaultAX/1f8900efb422888eabab 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 xrange(0, len(coll), step or n)))
def partition_all(n, coll, step=None):
return (coll[i:i+n] for i in xrange(0, len(coll), step or n))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment