Thomas Leonard | 21 Oct 2011 16:43
Picon
Favicon
Gravatar

for-must-match

Continuing with our experimental syntax changes to improve the 
reliability of E, here's a patch that adds a "for-must-match" feature.

http://gitorious.org/~tal-itinnov/repo-roscidus/it-innovation/commit/3ad4f5c3072447647c617fdb56738bf929cb8a1a

(see also: http://wiki.erights.org/wiki/Surprise_list)

Normally in E, this silently skips the third item:

   for a:int in [1, 2, "hi"] {
     println(a)
   }

Now, it throws an exception:

   pragma.enable("for-must-match")

   for a:int in [1, 2, "hi"] {
     println(a)
   }

To make a loop that skips non-matching items as before, use the "match" 
keyword. e.g.

   pragma.enable("for-must-match")

   for match a:int in [1, 2, "hi"] {
     println(a)
   }

(Continue reading)

Thomas Leonard | 22 Oct 2011 14:17
Picon
Gravatar

Re: for-must-match

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).

--

-- 
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
e-lang <at> mail.eros-os.org
http://www.eros-os.org/mailman/listinfo/e-lang
Kevin Reid | 22 Oct 2011 16:00
Favicon
Gravatar

Re: for-must-match

On Oct 22, 2011, at 8:17, Thomas Leonard 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 reject this proposal.

1. I am uncomfortable with it because I suspect it of violating E's syntactic conventions about scoping and
evaluation order, though I would have to consult MarkM to be more precise.

2. Furthermore, we already have the accumulator syntax which covers these use cases. I recognize that the
accumulator syntax is ugly; its problem is that it is both too general and not general enough. This
proposal is not general enough.

(Continue reading)

Thomas Leonard | 23 Oct 2011 12:41
Picon
Gravatar

Re: for-must-match

On 22 October 2011 15:00, Kevin Reid <kpreid <at> switchb.org> wrote:
> On Oct 22, 2011, at 8:17, Thomas Leonard 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 reject this proposal.
>
> 1. I am uncomfortable with it because I suspect it of violating E's syntactic conventions about scoping
and evaluation order, though I would have to consult MarkM to be more precise.

It probably does. However, there's also consistency with other
languages to consider. The Wikipedia page has examples both ways,
(Continue reading)

Thomas Leonard | 26 Oct 2011 14:13
Picon
Favicon
Gravatar

Re: for-must-match

On 2011-10-22 15:00, Kevin Reid wrote:
[...]
> As to your previous for-must-match pragma: I doubt its
> appropriateness for the language. However, it is entirely appropriate
> to add it as a pragma as an *experiment* -- that's why we have
> pragmas. You may commit for-must-match, provided that the
> documentation and *EVERY* code addition which supports it is marked
> as experimental and part of for-must-match.

I've now committed a cut-down version of this patch. When for-must-match 
is enabled, all for patterns must match or an exception is thrown (i.e. 
there is no new "for match" syntax).

I just changed the two places in my code that relied on it to use an 
explicit "if". They were in the same bit of code... I suspect I wrote 
that just after learning about the syntax to try it out.

I'm not sure what further documentation is needed. Aren't all the syntax 
pragmas experimental?

By the way: as an experiment (not committed) I changed the ENodeBuilder 
code to require patterns to match always (regardless of whether 
for-must-match was enabled) for all syntax versions. With the above two 
fixes to my code, all tests pass and everything seems to be working 
(including updoc, rune, and all of the standard library that gets used 
by my code).

--

-- 
Dr Thomas Leonard
IT Innovation Centre
(Continue reading)

Thomas Leonard | 24 Oct 2011 11:53
Picon
Gravatar

Re: for-must-match

On 22 October 2011 13:17, Thomas Leonard <talex5 <at> gmail.com> 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:
(Continue reading)

Kevin Reid | 25 Oct 2011 13:05
Favicon
Gravatar

Re: for-must-match

On Oct 24, 2011, at 5:53, Thomas Leonard wrote:

> 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.

This -- that some things expressible as patterns are expectations/assertions and some things are match
failures -- is a significant observation.

Perhaps we *should* have a general rule that any time there is *only one* pattern (i.e. not a switch), it
should not have a failure behavior other than throwing (or the explicit ejector offered by def).

Another possibility is if we could have some kind of general syntax for all patterns which expressed
(Continue reading)


Gmane