ranger

package module
v0.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 28, 2022 License: MIT Imports: 2 Imported by: 0

README

GoDoc

ranger 🧝‍♂️

The missing range functions for Go. Generating a range of numbers or characters (intervals) is an inane but useful task – perfect for a computer.

I missed the range type in Ruby so I wrote this.

The package includes functions for generating ranges of the following types:

  • Integers: int, int8, int16, int64, uint, uint8, uint16, uint64
  • Floats: float32, float64
  • Characters: rune, string

Examples

// Integer intervals
// ----------------
numbers := ranger.Range[int](1, 10)
fmt.Printf("%v", numbers) // [1 2 3 4 5 6 7 8 9 10]

odds := ranger.Range[int](1, 10, ranger.Step(2))
fmt.Printf("%v", odds) // [1 3 5 7 9]

littleNumbers := ranger.Range[int8](1, 127)
fmt.Printf("%v", littleNumbers) // [1 2 3 4...for a little while]

bigNumbers := ranger.Range[int64](1, 1000000)
fmt.Printf("%v", bigNumbers) // [1 2 3 4...for a long while]

// Float intervals
// ---------------
floats := ranger.Range[float32](1, 2.5, ranger.Step(0.5))
fmt.Printf("%v", floats) // [1.0 1.5 2.0 2.5]

// Character intervals
// -------------------
arrows := ranger.Rune('←', '↓')
fmt.Printf("%v", arrows) // [8592 8593 8594 8595] Runes are just numbers

alphabet, err := ranger.String("a", "z")
if err != nil {
  fmt.Println("Start or end string wasn't exactly one character")
}
for _, letter := range alphabet {
  fmt.Println(letter) // a b c d e f g...!
}
fmt.Println("Now I know my ABCs!")

Development

To run all tests:

go test ./...

To run benchmarks:

go test -bench ./...

Documentation

Overview

Range functions for Go. Easily generate intervals of integers, floats, runes, and strings.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Range added in v0.0.2

func Range[N Number](start, end N, opts ...Option[N]) []N

Returns an interval slice of numbers, with the specified start and end value. Values between the start and end are incremented ("stepped") by 1, unless a different step option is provided. If the end value is smaller than the start value, returns an empty slice.

func Rune

func Rune(start, end rune, opts ...Option[int]) []rune

Returns an interval slice of runes, with the specified start and end value. Runes are incremented by adding an integer step value to the rune's integer value, and casting that value back into a rune. The step value is 1, unless a different step option is provided. If the end value is smaller than the start value, returns an empty slice.

func Step

func Step[N Number](size N) func(*options[N])

The Step option changes the increment ("step") between values in generated intervals. If this option is omitted, the step defaults to 1.

func String

func String(start, end string, opts ...Option[int]) ([]string, error)

Returns an interval slice of strings. Under the hood, this generates an interval of runes and casts them to strings. For that reason, starting and ending strings cannot be empty and must contain a single character, or an error is returned. The step value is 1, unless a different step option is provided. If the end value is smaller than the start value, returns an empty slice.

Types

type Number added in v0.0.2

type Number interface {
	constraints.Integer | constraints.Float
}

type Option

type Option[N Number] func(*options[N])

An Option is just a function that changes the provided options struct. All range functions in this package accept Options as variadic arguments, so you can pass (optional!) options like step size.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL