Skip to content

ranges #

Operating with Ranges of Numbers

The ranges module provides tools for creating ranges of numbers.

Ranges are represented by the generic Range iterator, which has start and end points and a step size.

import ranges

// Iterate from 0 to 5 with step 2. Negative values also supported.
for i in ranges.range(0, 5, 2) {
    println(i)
}
// 0
// 2
// 4

See more usage examples in ranges_test.v.

fn from_string #

fn from_string[T](s string, config RangeFromStringConfig) ![]Range[T]

from_string parses the input string and returns an array of iterators. This function supports only V native number types and big.Integer. Use from_string_custom if you want to use custom type with special string convertion rules.

Supported string formats are start-end and start[:step]end. start and end values are sepatared by 'sep' which is hypen (-) by default. Single number will be interpreted as range of one element. Several ranges can be specified in a line, separated by 'group_sep' (comma by default). 'sep' and 'group_sep' can be overrided by user.

Some example input strings:

  • 5 - range from 5 to 5 (single element).
  • 0-10 - range from 0 to 10.
  • 15:-1:0 - range in MathLab-style syntax from 15 to 0 with negative step -1.
  • 1..8 - range from 1 to 8 with '..' sep.
  • 0-7,64-71 - multiple ranges: from 0 to 7 and from 64 to 71.

Only MathLab-style syntax allows you to specify a step directly in the string. For all other cases, the step is equal to one.

Example

assert ranges.from_string[int]('1-7')! == [ranges.range(1, 7, 1)]

fn from_string_custom #

fn from_string_custom[T](s string, conv StringConvertFn[T], config RangeFromStringConfig) ![]Range[T]

from_string_custom parses the input string using conv function to convert string values into numbers and returns an array of iterators. This is an extended version of from_string with the same semanthics.

Example

import math.big
import ranges

conv := fn (s string) !big.Integer {
	return big.integer_from_string(s)!
}

for range in ranges.from_string_custom('0-3,8,11-13', conv)! {
	for i in range {
		println(i)
	}
}
// 0
// 1
// 2
// 3
// 8
// 11
// 12
// 13

fn range #

fn range[T](start T, end T, step T) Range[T]

range creates new Range iterator with given start, end and step values.

Generally numbers are expected. If type is a struct the following operators must be overloaded to perform comparisons and arithmetics: +, -, <, ==. See https://docs.vlang.io/limited-operator-overloading.html for details.

The range includes the end value.

Note: Zero step value will cause an infitite loop!

fn (Range[T]) next #

fn (mut r Range[T]) next() ?T

next returns the new element from range or none if range end is reached.

fn (Range[T]) reset #

fn (mut r Range[T]) reset()

reset resets the internal iterator state to its initial value, after which the iterator can be reused.

Note: for i in iter { does not modify the internal iterator state, but direct next() call does.

fn (Range[T]) with_step #

fn (r Range[T]) with_step[T](step T) Range[T]

with_step returns copy of the range with new step value.

type StringConvertFn #

type StringConvertFn[T] = fn (s string) !T

struct RangeFromStringConfig #

@[params]
struct RangeFromStringConfig {
pub:
	sep       string = '-'
	group_sep string = ','
}