Hacker News new | past | comments | ask | show | jobs | submit login

‘matchAll’ looks great but why make it an iterator vs a ‘map’ style callback? Just seems so arcane.



> ‘matchAll’ looks great but why make it an iterator vs a ‘map’ style callback?

Because that makes it significantly easier to e.g. optionally collect into an array, or to only partially iterate the sequence (e.g. only get the first 3 matches) which is painful to impossible using JS's callbacks. An iterator is simply more flexible.


For what it’s worth Array.from() [0] lets you pass a map style callback as its second argument, so Array.from(yourStr.matchAll(yourRegex), yourMapFn) works.

[0] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...




Consider applying for YC's Spring batch! Applications are open till Feb 11.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: