Skip to content

feat: range_with_step convenience function #887

@Vulwsztyn

Description

@Vulwsztyn

I found myself needing to implement that function and thought that the usecase is popular enough to warrant a PR stdlib.

This is a variation of range with the user-provided step.

The general use would be:

/// ```gleam
/// range_with_step(1, 5, 2)
/// // -> [1, 3, 5]
/// ```
///
/// ```gleam
/// range_with_step(1, 6, 2)
/// // -> [1, 3, 5]
/// ```
///
/// ```gleam
/// range_with_step(6, -1, -3)
/// // -> [6, 3, 0]
/// ```
/// 
/// ```gleam
/// range_with_step(6, -3, -3)
/// // -> [6, 3, 0, -3]
/// ```

Discussion (if the idea is not rejected):

  • what should happen when given step is 0?
  • what should happen when from and to are equal?
  • which of the 2 above takes precedence?
  • what should happen when (from<to and step<0) or (from>to and step>0)?
  • a propos the one above should the function return List(Int) or Result?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions