4 ms·
>I prefer the loops instead even if it means going down at individual elements in a list/matrix. Actually you have it backwards, probably because you confused
by coldtea 7y ago
>I prefer the loops instead even if it means going down at individual elements in a list/matrix.
Actually you have it backwards, probably because you confused J's somewhat cryptic notation as the only way to implement the same concept.
It's the for loops, with their sprawl of non-declarative code, that would be equivalent to the regex opaqueness, and a well named operator or function to achieve the same thing that would be more like J.
E.g. would you rather use a:
sort(myList, order=desc)
or write your own sorting with for loops?
Similarly, think of operations like:
findElement(myList, predicate)
filterElements(myList, predicate)
keepElements(myList, predicate)
forEach(myList, myFunc)
forEachParallel(myList, myFunc, chunks=5)
In other words, reduce, map, and specialized versions of them.
If 10-20 such language provided functions covered all cases, I'd use them over for loops all the time. In fact that's how people use e.g. lodash.
What J adds is a succinct syntax to write and compose such primitives at the language level.
But such a syntax is not necessary to achieve the same concept (although not the same brevity/expressiveness) and be better/more readable than for loops...
P.S And regexes themselves should be compared to the equivalent parsing code -- which could require a FSM, or an ad-hoc buggy implementation of the same checks/captures spanning 10s or 100s of lines depending on the regex. Except if you just use a regex for very simple things where e.g. a "contains" function or some splitting etc will be simpler.
- tluyben2 7y agoI have a substantial library with those functions for C#. Why not use Linq? Because I see people struggle with it (usually external clients who license our code and outsource development); when they do, they resort to loops. So I wrote these simpler functions which are implemented mostly with Linq but they keep people who do not understand linq (again, yes, they are there and rather a lot of them) from messing up the codebase. Advantage is that these functions translate to any other language, while Linq doesn’t, so we have the same (partially implemented) semantics for the same things over a few languages we use.