-
-
Save eldritchideen/e56a9dc5ad9b9ec9967a54bd5bd2346e to your computer and use it in GitHub Desktop.
clojure.core/partition and clojure.core/partition-all in Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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