Skip to content

iter #

Iterator Tools

This module provides two different ways of managing combinatorics. Let see an example for combinations.

Fully formed array of all Combinations

// combinations will return an array of all length `r` combinations of `data`
// While waiting on https://github.com/vlang/v/issues/7753 to be fixed, the function
// assumes f64 array input. Will be easy to change to generic later
pub fn combinations(data []f64, r int) [][]f64

Lazy generation

This case is optimal to generate combinations in a lazy way, optimizing memory use:

// CombinationsIter.new will return an iterator that allows
// lazy computation for all length `r` combinations of `data`
pub fn CombinationsIter.new(data []f64, r int) CombinationsIter

// next will return next combination if possible
pub fn (mut o CombinationsIter) next() ?[]f64

fn combinations #

fn combinations(data []f64, r int) [][]f64

combinations will return an array of all length r combinations of data While waiting on https://github.com/vlang/v/issues/7753 to be fixed, the function assumes f64 array input. Will be easy to change to generic later

fn combinations_with_replacement #

fn combinations_with_replacement(data []f64, r int) [][]f64

combinations_with_replacement will return r length subsequences of elements from the input data allowing individual elements to be repeated more than once. This is as close a translation of python's [itertools.combinations_with_replacement] (https://docs.python.org/3.9/library/itertools.html#itertools.combinations_with_replacement) as I could manage. Using f64 array instead of generic while waiting on https://github.com/vlang/v/issues/7753

fn permutations #

fn permutations(data []f64, r int) [][]f64

permutations returns successive r length permutations of elements in data

fn product #

fn product(data [][]f64) [][]f64

Cartesian product of the arrays in data

fn CombinationsIter.new #

fn CombinationsIter.new(data []f64, r int) CombinationsIter

CombinationsIter.new will return an iterator that allows lazy computation for all length r combinations of data

fn CombinationsWithReplacementIter.new #

fn CombinationsWithReplacementIter.new(data []f64, r int) CombinationsWithReplacementIter

CombinationsWithReplacementIter.new will return an iterator that allows lazy computation for all length r combinations with replacement of data

fn Counter.new #

fn Counter.new(start f64, step f64) Counter

fn Cycler.new #

fn Cycler.new(data []f64) Cycler

fn FloatIter.new #

fn FloatIter.new(params FloatIterParams) !FloatIter

FloatIter.new returns an iterator of evenly spaced floats in the half-open interval [start, stop).

parameters: FloatIterParams { -start f64 : the start of the range. -stop f64 [required]: the end of the range (exclusive). -step f64 = 1: the step between the numbers. }

fn IntIter.new #

fn IntIter.new(params IntIterParams) !IntIter

IntIter.new returns an iterator of evenly spaced integers numbers in the half-open interval [start, stop).

parameters: IntIterParams { -start i64 : the start of the range. -stop i64 [required]: the end of the range (exclusive). -step i64 = 1: the step between the numbers. }

fn LinearIter.new #

fn LinearIter.new(params LinearIterParams) !LinearIter

LinearIter.new returns an iterator of len evenly spaced floats in the interval [start, stop]. The endpoint of the interval can optionally be excluded.

parameters: LinearIterParams { -start f64 : The start of the range. -stop f64 [required]: The end of the range. -len i64 = 50: Number of samples to generate. Must be non-negative. -endpoint bool = true: If true, stop is the last sample. Otherwise, it is not included. }

fn LogIter.new #

fn LogIter.new(params LogIterParams) !LogIter

LogIter.new returns an iterator of len numbers evenly spaced on a logarithmic scale. The sequence starts at base ^ start and ends in base ^ stop (if endpoint = true).

parameters: LogIterParams { -start f64 [required]: base ^ start is the starting value of the sequence. -stop f64 [required]: base ^ stop is the final value of the sequence, unless endpoint is false. In that case, num + 1 values are spaced over the interval in log-space, of which all but the last (a sequence of length len) are returned. -len i64 = 50: Number of samples to generate. -base f64 = 10.0: The base of the log space. -endpoint bool = true: If true, bas ^ stop is the last sample. Otherwise, it is not included. }

fn PermutationsIter.new #

fn PermutationsIter.new(data []f64, r int) PermutationsIter

PermutationsIter.new will return an iterator that allows lazy computation for all length r permutations of data

fn ProductIterator.new #

fn ProductIterator.new(data [][]f64) ProductIterator

fn Repeater.new #

fn Repeater.new(item f64) Repeater

fn (FloatIter) next #

fn (mut r FloatIter) next() ?f64

next returns the next element of the iterator if possible.

fn (IntIter) next #

fn (mut r IntIter) next() ?i64

next returns the next element of the iterator if possible.

fn (LinearIter) next #

fn (mut o LinearIter) next() ?f64

next returns the next element of the iterator if possible.

fn (LogIter) next #

fn (mut o LogIter) next() ?f64

next returns the next element of the iterator if possible.

struct CombinationsIter #

struct CombinationsIter {
mut:
	pos  u64
	idxs []int
pub:
	repeat int
	size   u64
	data   []f64
}

fn (CombinationsIter) next #

fn (mut o CombinationsIter) next() ?[]f64

next will return next combination if possible

struct CombinationsWithReplacementIter #

struct CombinationsWithReplacementIter {
mut:
	pos  u64
	idxs []int
pub:
	repeat int
	size   u64
	data   []f64
}

fn (CombinationsWithReplacementIter) next #

fn (mut o CombinationsWithReplacementIter) next() ?[]f64

next will return next combination if possible

struct Counter #

struct Counter {
pub:
	step f64
pub mut:
	state f64
}

fn (Counter) next #

fn (mut c Counter) next() ?f64

struct Cycler #

struct Cycler {
mut:
	idx int
pub:
	data []f64
}

fn (Cycler) next #

fn (mut c Cycler) next() ?f64

struct FloatIterParams #

@[params]
struct FloatIterParams {
pub:
	start f64
	stop  f64 @[required]
	step  f64 = 1.0
}

struct IntIterParams #

@[params]
struct IntIterParams {
pub:
	start i64
	stop  i64 @[required]
	step  i64 = 1
}

struct LinearIterParams #

@[params]
struct LinearIterParams {
pub:
	start    f64  @[required]
	stop     f64  @[required]
	len      i64  = 50
	endpoint bool = true
}

struct LogIterParams #

@[params]
struct LogIterParams {
	start    f64  @[required]
	stop     f64  @[required]
	len      i64  = 50
	base     f64  = 10.0
	endpoint bool = true
}

struct PermutationsIter #

struct PermutationsIter {
mut:
	pos    u64
	idxs   []int
	cycles []int
pub:
	repeat int
	size   u64
	data   []f64
}

fn (PermutationsIter) next #

fn (mut o PermutationsIter) next() ?[]f64

next will return next permutation if possible

struct ProductIterator #

struct ProductIterator {
	repeat_lengths []u64
	size           u64
mut:
	indices_to_grab []int
	idx             u64
pub:
	data [][]f64
}

fn (ProductIterator) next #

fn (mut o ProductIterator) next() ?[]f64

struct Repeater #

struct Repeater {
pub:
	item f64
}

fn (Repeater) next #

fn (m Repeater) next() ?f64