Skip to content

Proposal for windows function(s) #139

Open
@HarrisonMc555

Description

@HarrisonMc555

A task I occasionally reach for is the ability to iterate through a list in a sliding window to get successive pairs of neighboring elements.

Would we consider adding a windows function?

windows : List a -> List ( a, a )
windows l =
    case l of
        x :: y :: rest ->
            ( x, y ) :: windows2 (y :: rest)

        _ ->
            []

Since it may be desirable to have windows of larger sizes, we could name this function windows2 and add functions windows3, windows4, etc.

This is similar to Ruby's each_slice function or Rust's windows, but returning tuples instead of lists.

P.S. Feel free to include feedback on code style, I'm still fairly new to Elm.

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