Letting RegExp method return something iterable?

Claude Pache claude.pache at gmail.com
Tue Aug 27 14:50:17 PDT 2013



Le 27 août 2013 à 18:48, Andrea Giammarchi <andrea.giammarchi at gmail.com> a écrit :

> let me rephrase ... I've no idea what this code does if not a syntax error (and for different reasons)
> 
> `for (let m of re.execAll(str) {`
> 
> what is `of` ... will `let` mark that variable as local ? what is returned and what will be `m` ?
> 
> I need to know these things ... this has nothing to do with "Don’t be clever, don’t make me think." a point which also I don't understand (I have to think about such statement ... I don't demand Ocaml language to be C like 'cause I don't get it)

Trying to reexplain: `for (let m of re.execAll(str))` is a direct, one-to-one translation of the meaning of the programmer into (the expected) EcmaScript 6. But with `while (m = re.exec(str))`, you exploit some secondary fact about the value of `re.exec(str)` (falsy iff when over) which is unrelated to the object of the code. (And yes, you have to learn the complete syntax of `for/of` in order to understand the code, but it is unrelated to the point.)

—Claude


> 
> Anyway, I've already commented my point of view.
> 
> Regards
> 
> 
> On Tue, Aug 27, 2013 at 9:42 AM, Andrea Giammarchi <andrea.giammarchi at gmail.com> wrote:
>> sure you know everything as soon as you read `of` ... right ? How objectives are your points ? If you know JS that while looks very simple, IMO
>> 
>> 
>> On Tue, Aug 27, 2013 at 5:24 AM, Claude Pache <claude.pache at gmail.com> wrote:
>>> 
>>> Le 27 août 2013 à 01:23, Brendan Eich <brendan at mozilla.com> a écrit :
>>> 
>>> > Andrea Giammarchi wrote:
>>> >> Is it very useful because you wrote for instead of while ?
>>> >>
>>> >> ```javascript
>>> >> while (m = re.exec(str))
>>> >>  console.log(m[0])
>>> >> ;
>>> >> ```
>>> >
>>> > It is, for two reasons:
>>> >
>>> > 1. in JS only for can have a let or var binding in the head.
>>> >
>>> > 2. the utility extends to all for-of variations: array comprehensions, generator expresisons.
>>> >
>>> > /be
>>> 
>>> There is a third reason. The syntax:
>>> 
>>> ```javascript
>>> for (let m of re.execAll(str) {
>>>         // ...
>>> }
>>> ```
>>> 
>>> has the clear advantage to express the intention of the programmer, and *nothing more*. It does not require good knowledge of the details of the language to understand what happens.
>>> 
>>> Indeed, when I read `while(m = re.exec(str))`, I really have to analyse the following *additional* points:
>>> * `=` is not a typo for `==` (here, some annotation would be useful);
>>> * `RegExp#exec` returns a falsy value if *and only if* there is no more match;
>>> * `re` has its global flag set, and its `.lastIndex` property has not been disturbed.
>>> 
>>> All these tricks are unrelated to the intention of the programmer, and are just distracting points, especially for any reader that use only occasionally `RegExp#exec` with the global flag set.
>>> 
>>> In summary, citing [1]: "Don’t be clever, don’t make me think."
>>> 
>>> —Claude
>>> 
>>> [1] http://www.2ality.com/2013/07/meta-style-guide.html
> 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20130827/2d8f022c/attachment.html>


More information about the es-discuss mailing list