Python itertools... in Racket!
1 Introduction
2 itertools API
2.1 Infinite iterators
in-count
in-repeat
2.2 Iterators terminating on the shortest input sequence
in-accumulate
in-sequences*
sequence-compress
sequence-drop-while
sequence-filter-not
in-group-by
in-pairwise
sequence-take-while
in-longest-parallel
2.3 Combinatoric iterators
in-cartesian-product
3 Extras
sequence-take
sequence-interleave
sequence-interleave-longest
in-repeat*
sequence-repeat-each
8.16.0.1

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
An infinite series of numbers, starting with start and incrementing by step. Equivalent to itertools.count.

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
Repeats val n times, or infinitely if the count is #f. Equivalent to itertools.repeat.

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
Make a sequence that returns accumulated sums or accumulated results from other binary functions. If an initial value is provided, the accumulation will start with that value and the output will have one more element than the input sequence. Equivalent to itertools.accumulate.

itertools.batched is in-slice in Racket.

itertools.chain is in-sequences in Racket.

procedure

(in-sequences* seq)  sequence?

  seq : (sequence/c sequence?)
Flattens a sequence of sequences into their individual elements. All subsequenes should have the same number of values. Equivalent to itertools.chain.from_iterable.

procedure

(sequence-compress data selectors)  sequence?

  data : sequence?
  selectors : (sequence/c any/c)
Returns a new sequence of just the elements of data where the corresponding element of selectors is true. Equivalent to itertools.compress.

procedure

(sequence-drop-while pred? seq)  sequence?

  pred? : procedure?
  seq : sequence?
Returns a new sequence whose first element is the first one of seq that makes pred? return #f. Works with multi-valued sequences; pred? should take as many arguments as the sequence has values. Equivalent to itertools.dropwhile.

procedure

(sequence-filter-not pred? seq)  sequence?

  pred? : procedure?
  seq : sequence?
Like sequence-filter but keeps only elements for which pred? returns #f. Works with multi-valued sequences; pred? should take as many arguments as the sequence has values. Equivalent to itertools.filterfalse.

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?
Groups series of consecutive elements that compare equal after being transformed by key. Each element of the resulting sequence has two values - the transformed value all elements of the group are grouped by, and the group itself, as a list of elements in the same order they appear in the original sequence. Equivalent to itertools.groupby.

itertools.islice is currently not implemented.

procedure

(in-pairwise seq)  (sequence/c any/c any/c)

  seq : (sequence/c any/c)
Returns a two-valued sequence - the first and second elements of seq, the second and third elements, the third and fourth, and so on. If given a sequence of less than 2 elements, returns an empty sequence. Equivalent to itertools.pairwise.

itertools.starmap is basically equivalent to sequence-map in Racket.

procedure

(sequence-take-while pred? seq)  sequence?

  pred? : procedure?
  seq : sequence?
Return a sequence of the leading elements of seq that make pred? return true. Works with multi-valued sequences; pred? should take as many arguments as the sequence has values. Equivalent to itertools.takewhile.

itertools.tee is currently not implemented.

procedure

(in-longest-parallel seq    
  ...    
  [#:fill-value fill])  sequence?
  seq : (sequence/c any/c)
  fill : any/c = undefined
Like in-parallel, but only stops after all sequences have been exhausted, not after the first. Sequences that end earlier are filled in with fill. Equivalent to itertools.zip_longest.

2.3 Combinatoric iterators🔗ℹ

procedure

(in-cartesian-product seq ...)  sequence?

  seq : (sequence/c any/c)
Returns a sequence that generates the cartesian product of the sequences it’s passed. All those sequences should be finite. The resulting sequence has the same number of values as the number of arguments to the function. Equivalent to itertools.product, though a repeat argument is not currently supported.

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?
Returns a sequence of the first len elements of seq (Or less if seq is a finite sequence with fewer elements).

procedure

(sequence-interleave seq ...)  sequence?

  seq : sequence?
Return a new sequence pulling an element at a time from each argument sequence in turn until one is exhausted. All sequences should return the same number of values. Equivalent to more_itertools.interleave.

procedure

(sequence-interleave-longest seq ...)  sequence?

  seq : sequence?
Return a new sequence pulling an element at a time from each argument sequence in turn, skipping exhausted ones until all are used up. All sequences should return the same number of values. Equivalent to more_itertools.interleave_longest.

procedure

(in-repeat* val ...+ [#:count count])  sequence?

  val : any/c
  count : (or/c #f exact-positive-integer?) = #f
A version of in-repeat that can generate multi-valued sequences.

procedure

(sequence-repeat-each seq [n])  sequence?

  seq : sequence?
  n : exact-positive-integer? = 2
Return a new sequence that consists of each element of seq repeated n times. Equivalent to more_itertools.repeat_each.