Skip to content

A pipeline over a List or slice does not iterate it, and a slice has no iter() #2534

Closed

Description

@ehartford

Surfaced by a quicksort written the way a person writes it:

fn quicksort(xs: List[isize]) -> List[isize]:
    match xs:
        [] => []
        [pivot, ..rest] =>
            let smaller = rest |> filter(it < pivot) |> collect[List]()
            let larger  = rest |> filter(it >= pivot) |> collect[List]()
            quicksort(smaller) ++ [pivot] ++ quicksort(larger)

Today (v0.15.3.0-g8182ccfb1):

  • rest |> filter(it < pivot): error: undefined variable at it. A pipeline over a List/slice value is not an iterator, so the stage does not see a sequence and it is never bound.
  • rest.iter() |> filter(it < pivot): error: unknown method 'iter' for type '[]isize'. The rest pattern binds a slice, and a slice has no iter() (a List does, as the ListIter view).

Expected: a pipeline whose left side is a sequence (a List, a slice, an array) iterates it, the same .iter() the for loop inserts; and a slice has an iter() yielding &T, the same view iterator List has. Nothing here is a choice the programmer could spell differently, so the compiler writes it (mission: when one meaning is forced, With infers). The for x in rest loop form compiles today, so this is the pipeline and slice surface only.

Needs the §18 pipeline words to say a sequence on the left is iterated; the non-generic quicksort above becomes the fixture.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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