Python itertools... in Racket!
(require itertools) | package: itertools |
1 Introduction
This module implements the bits of the Python itertools library that aren’t already present in one way or another in Racket. The provided functions all work with sequences, which along with streams, are the closest things to Python iterables in Racket (But sequences are much more fundamental to Racket, so that’s what you get).
Conventions followed by this module: Functions that return a new, altered sequence generally have a in- prefix, and ones that just filter or pass through existing sequences usually start with sequence-, which seems to be the general Racket practice (except when it’s not). Where Python uses tuples, these functions instead generally return multiple values. If you need a single value instead, wrap them in a list with in-values-sequence.
Select functions from the more-itertools library and other sources might make their way in here too; please file issues (And preferrably a pull request too) for any desired ones.
2 itertools API
2.1 Infinite iterators
procedure
(in-count [start step]) → (sequence/c real?)
start : real? = 0 step : real? = 1
itertools.cycle is in-cycle in Racket.
procedure
(in-repeat val [n]) → (sequence/c any/c)
val : any/c n : (or/c exact-nonnegative-integer? #f) = #f
2.2 Iterators terminating on the shortest input sequence
procedure
(in-accumulate seq [f]) → (sequence/c any/c)
seq : (sequence/c any/c) f : (-> any/c any/c any/c) = + (in-accumulate seq f init) → (sequence/c any/c) seq : (sequence/c any/c) f : (-> any/c any/c any/c) init : any/c
itertools.batched is in-slice in Racket.
itertools.chain is in-sequences in Racket.
procedure
(in-sequences* seq) → sequence?
seq : (sequence/c sequence?)
procedure
(sequence-compress data selectors) → sequence?
data : sequence? selectors : (sequence/c any/c)
procedure
(sequence-drop-while pred? seq) → sequence?
pred? : procedure? seq : sequence?
procedure
(sequence-filter-not pred? seq) → sequence?
pred? : procedure? seq : sequence?
procedure
(in-group-by seq? [#:key key #:equal? =?])
→ (sequence/c any/c list?) seq? : (sequence/c any/c) key : (-> any/c any/c) = values =? : (-> any/c any/c any/c) = equal?
itertools.islice is currently not implemented.
procedure
(in-pairwise seq) → (sequence/c any/c any/c)
seq : (sequence/c any/c)
itertools.starmap is basically equivalent to sequence-map in Racket.
procedure
(sequence-take-while pred? seq) → sequence?
pred? : procedure? seq : sequence?
itertools.tee is currently not implemented.
procedure
(in-longest-parallel seq ... [ #:fill-value fill]) → sequence? seq : (sequence/c any/c) fill : any/c = undefined
2.3 Combinatoric iterators
procedure
(in-cartesian-product seq ...) → sequence?
seq : (sequence/c any/c)
itertools.permutations is basically in-permutations in Racket, though it doesn’t support the optional length argument of the Python iterator and only takes lists. A more Pythonic version is planned.
itertools.combinations is basically in-combinations in Racket, but only takes lists. A more Pythonic version is planned.
itertools.combinations_with_replacement is not currently implemented.
3 Extras
Additional functions not taken directly from itertools.
procedure
(sequence-take seq len) → sequence?
seq : sequence? len : exact-nonnegative-integer?
procedure
(sequence-interleave seq ...) → sequence?
seq : sequence?
procedure
(sequence-interleave-longest seq ...) → sequence?
seq : sequence?
procedure
(in-repeat* val ...+ [#:count count]) → sequence?
val : any/c count : (or/c #f exact-positive-integer?) = #f
procedure
(sequence-repeat-each seq [n]) → sequence?
seq : sequence? n : exact-positive-integer? = 2