Range

GitHub   Edit on GitHub
History
versionchanges
v0.3.0Added module

Utilities for working with ranges. A range represents an interval—a set of values with a beginning and an end.

1
import Range from "range"

Types

Type declarations included in the Range module.

Range.Range

1
2
3
4
enum Range {
Inclusive(Number, Number),
Exclusive(Number, Number),
}

Ranges can be inclusive or exclusive. When Inclusive, the end value will be included in operations. When Exclusive, the end value will be excluded from operations.


Values

Functions and constants included in the Range module.

Range.inRange

History
versionchanges
v0.3.0Initial implementation
1
inRange : (Number, Range) -> Bool

Checks if the given number is within the range.

Parameters:

param type description
value Number The number being checked
range Range The range to check within

Returns:

type description
Bool Whether or not the value is within range

Examples:

1
Range.inRange(1, Range.Inclusive(0, 2)) == true
1
Range.inRange(10, Range.Inclusive(0, 2)) == false

Range.forEach

History
versionchanges
v0.3.0Initial implementation
1
forEach : ((Number -> Void), Range) -> Void

Calls the given function with each number in the range. For increasing ranges, the value is increased by 1 in each iteration, and for decreasing ranges, the value is decreased by 1. The value is always changed by 1, even if non-integer values were provided in the range.

Parameters:

param type description
fn Number -> Void The function to be executed on each number in the range
range Range The range to iterate

Examples:

1
Range.forEach(val => print(val), Range.Exclusive(0, 2))

Range.map

History
versionchanges
v0.3.2Initial implementation
1
map : ((Number -> a), Range) -> List<b>

Produces a list by calling the given function on each number included in the range. For increasing ranges, the value is increased by 1 in each iteration, and for decreasing ranges, the value is decreased by 1. The value is always changed by 1, even if non-integer values were provided in the range.

Parameters:

param type description
fn Number -> a The function called on each number in the range that returns the value for the output list
range Range The range to iterate

Returns:

type description
List<a> A list containing all values returned from the fn

Examples:

1
Range.map(val => val * 2, Range.Inclusive(0, 2)) == [0, 2, 4]
This is a notification!