Re: for-must-match

Thomas Leonard <[email protected]> Mon, 24 Oct 2011 10:53:20 +0100
Newsgroups gmane.comp.lang.e.general
Message-ID <CAG4opy-QGf_5bxRigNw+nyJ_QUDrXHozW6SJANCd7+B6kVxUHw@mail.gmail.com>
On 22 October 2011 13:17, Thomas Leonard <[email protected]> wrote:
> Here's a patch to add list comprehensions:
>
> ? def list := [1, 2, 3]
> ? [2 * x for x in list]
> # value: [2, 4, 6]
>
> http://gitorious.org/repo-roscidus/e-core/commit/9bfacc4f748b4ea6a7cf64dc40d7bc9dd1890d38
>
> And for map comprehensions:
>
> ? def map := ["alice" => 50, "bob" => 60]
> ? [v => k for k => v in map]
> # value: [50 => "alice", 60 => "bob"]
>
> http://gitorious.org/repo-roscidus/e-core/commit/980c8c93d41a69f242acdb5b83c9faccf918f4c3
>
> They don't currently support the 'match' keyword, so you can't use
> them to filter (every item must be mapped to something).

I've now added filtering:

http://gitorious.org/repo-roscidus/e-core/commit/b407e45a23ec942683fac84fc6156809321b805d

However, I didn't use the match syntax for this because, thinking
about it further, I don't think using pattern bindings is a good way
to test for a condition.

Consider a list of pairs [String,boolean] where we want to process all
the true values. With pattern matching (current E syntax), we'd write:

for [name :String, ==true] in pairs { ... }

But this only covers two cases (match and no-match). There are really
three cases:

match [name :String, ==true] => process this item
match [_ :String, _:boolean] => skip this item
match _ => report error

For switch statements and object definitions, this isn't a problem
because when a pattern doesn't match it falls though to the next case
and will eventually be reported or handled. But in for loops errors
are silently ignored.

Therefore, I used a more traditional syntax for list comprehensions:

[name for [name :String, active :boolean] in pairs if active]

I think this is better at separating out the expected format of the
item (Tuple[String,boolean]) from the condition we want to check. You
can use a matchBind in the if to get the old behaviour if required.


-- 
Dr Thomas Leonard        http://0install.net/
GPG: 9242 9807 C985 3C07 44A6  8B9A AE07 8280 59A5 3CC1
GPG: DA98 25AE CAD0 8975 7CDA  BD8E 0713 3F96 CA74 D8BA

_______________________________________________
e-lang mailing list
[email protected]
http://www.eros-os.org/mailman/listinfo/e-lang