Skip to content

An introduction of For patterns #51

@thautwarm

Description

@thautwarm

After considering for extremely a long time, now I think there is a useful case of patterns look like set comprehension.

d = [(i, i + 1) for i = 1:5]
@match d begin
   [(i, i + 1) for i in seq] => seq
end # [1, 2, 3, 4, 5]

Which is to say, (i, i + 1) matches the element of d, thus we can do a reversed comprehension, where for i in seq produces a [i for (i, i+1) in d].

As a result,

d = [(i, i + 1) for i = 1:5]
@match d begin
   [i for i in seq] => seq
end # [(1, 1 + 1), (2, 2+1), (3, 3+1), (4, 4+1), (5, 5+1)]

You might think it a bit useless, however,

x :: Vector{Tuple{String, Term}}
eval_term :: Term => Expr

@active Eval(x) begin
    eval_term(x)
end

@match x begin
   [(a, Eval(b)) for (a, b) in seq] => seq
end # typed Vector{Tuple{String, Expr}}

AWSL!

Metadata

Metadata

Assignees

No one assigned

    Labels

    coreinfrastructures and some standard impl

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions