News for hackers. Stuff that matters.
Re:Coconut: Simple, elegant, Pythonic functional programming by jimsimmons on Thursday November 04, 2021 @07:46AM (#29105888)
I'm confused when people call it simple or elegant. I wonder if the same crowd would use similar words to describe regex.
Re:Coconut: Simple, elegant, Pythonic functional programming by gliptic on Thursday November 04, 2021 @08:25AM (#29106252)
Compared to writing the parsing in procedural code? Yes.

e.g.:

  a(b|c)d
vs something like:

  return str.StartsWith("a") && str.EndsWith("d") && str.Length == 3 && (str[1] == "b" || str[1] == "c");
Why wouldn't you consider regular expressions simple and elegant for the problem they are solving?
Re:Coconut: Simple, elegant, Pythonic functional programming by jimsimmons on Thursday November 04, 2021 @09:00AM (#29106594)
That's a simple example. Things quickly get out of hand once you have a more complex problem like recognising a valid URL [1]:

`r"(?i)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s`

Regex has a reputation for being "write once read never". It's primarily because it doesn't have any structure or "modularity". Functional programs also tend to pack many variables or operations in the same line without delimitation. For example Haskell's argument syntax.

[1] https://www.geeksforgeeks.org/python-check-url-string/

Re:Coconut: Simple, elegant, Pythonic functional programming by sweezyjeezy on Thursday November 04, 2021 @09:41AM (#29107047)
> Regex has a reputation for being "write once read never". It's primarily because it doesn't have any structure or "modularity"

I don't think this is true - it's because people don't write regex every day - I used to write a lot of web-scrapers which involved getting pretty handy with regexes, it's fine, trust me. If a regex is REALLY hard to read - it's normally because it's badly written, or because it's trying to do something genuinely complicated.

Also how are they not modular? You always build up a regex from smaller regexes, maybe the modularity isn't always easy to follow, but it is there. e.g.

{regex1}{regex2} = regex1 followed by regex2 (concatenation) ({regex1}|{regex2}) = regex2 OR regex2 (OR operator) etc.

Your URL example can be broken down into smaller modules - e.g. there's a 3-way OR block at the start that cover http(s)://, www(123)., abc.aa/ (don't understand the last one..)

Re:Coconut: Simple, elegant, Pythonic functional programming by jimsimmons on Thursday November 04, 2021 @10:45AM (#29107833)
Modularity exists but it's not made explicit. I need to mentally track the level of nesting, column number, suffixes (*+?) etc. If the structure was made explicit, it would be a lot more readable. And expressing the composition gradually, bottom up, comes very close to the procedural specification.
Re:Coconut: Simple, elegant, Pythonic functional programming by sweezyjeezy on Thursday November 04, 2021 @11:35AM (#29108487)
There's no chance you're going to convince me that throwing out regexes in favour of writing EVERYTHING procedurally is a good idea. I fully agree that they should often be built programatically to improve readability and reduce duplication. But capture groups, lookahead operators, kleene stars - these are a drag to do manually, not to mention you lose regex compiler optimisations - regex is the way.
Re:Coconut: Simple, elegant, Pythonic functional programming by renewiltord on Thursday November 04, 2021 @06:05PM (#29113068)
Actually, what is the modularity concept in regexes? Is there a regex extension language where I can say "first match this then match that" but where I can name each part appropriately?
Re:Coconut: Simple, elegant, Pythonic functional programming by ModernMech on Thursday November 04, 2021 @06:55PM (#29113451)
At some point, you’re probably better off using an actual parser. Parser combinator libraries make writing such things very easy.
Re:Coconut: Simple, elegant, Pythonic functional programming by threatofrain on Thursday November 04, 2021 @09:48AM (#29107129)
To be really persuasive, I think we'd all like to see what a well-written URL checking procedure would look like when optimized for human reading.
Re:Coconut: Simple, elegant, Pythonic functional programming by pizza on Thursday November 04, 2021 @12:45PM (#29109444)
This is basically code golfed. You could always break up a regex into named variables of its constituent logical parts; same with FP.

This is kind of like calling js ugly because you could come up with a minified js script that is really complex and fits in one line, or if you inlined all function calls in a long C file into the main() function body. There's a sweet spot of the complexity density per LOC that humans are seemingly more-or-less tuned for, where it's often possible to get a computer to handle more complexity-dense code.

Personally, the shorter the code and the stronger the types, the faster I can tell if I made a mistake and the fewer places there are for me to make one, and that's why I like terse FP languages.

Re:Coconut: Simple, elegant, Pythonic functional programming by kristianbrigman on Thursday November 04, 2021 @01:06PM (#29109762)
I personally like regex as well, but it can be hard to read… there are other similar things like python parse (https://pypi.org/project/parse/,”parse is format in reverse”) which are a little easier for easy cases.
Re:Coconut: Simple, elegant, Pythonic functional programming by SOLAR_FIELDS on Thursday November 04, 2021 @09:02AM (#29106628)
Sure, but I think your example goes way too far in the other direction. URLs are quite complex in comparison to a lot of pattern matching problems.

Also, what would be the procedural equivalent of the regex you linked? My guess is that if such a solution existed in procedural code, that this symbol soup you linked would be considered simple and elegant in comparison.

Re:Coconut: Simple, elegant, Pythonic functional programming by jimsimmons on Thursday November 04, 2021 @09:46AM (#29107112)
Procedural version is more verbose and quite long. That's the downside but it's also the reason why it'd be more readable and maintainable. Procedural version would actually be very similar to a parser combinator with elementary parsers being things like `while str[i].isalphanumeric(): i++`.
Re:Coconut: Simple, elegant, Pythonic functional programming by gliptic on Thursday November 04, 2021 @10:19AM (#29107515)
Or you could write it as a PEG (or indeed parser combinators) that would be even easier to read. I don't buy that verbosity on its own makes something readable and maintainable.
Re:Coconut: Simple, elegant, Pythonic functional programming by jimsimmons on Thursday November 04, 2021 @10:39AM (#29107762)
Imagine comparing diffs of a verbose parser vs diff between two complex regexes. The parser one could be 10x larger or even more, but it'd have a tractable diff. In the regex case I'd have to mentally keep track of column numbers and a bunch of other things.
Re:Coconut: Simple, elegant, Pythonic functional programming by gliptic on Thursday November 04, 2021 @10:50AM (#29107911)
PEG doesn't have that problem.
Re:Coconut: Simple, elegant, Pythonic functional programming by Zababa on Thursday November 04, 2021 @09:14AM (#29106749)
> Things quickly get out of hand once you have a more complex problem like recognising a valid URL

I'm not sure it's so complex. What's the equivalent code for recognising an URL? Regex are implemented in many languages, so this regex is, in a way, a portable library/function in most programming languages. And since it's shared, it can easily be checked way more than any single library would be.

Re:Coconut: Simple, elegant, Pythonic functional programming by jimsimmons on Thursday November 04, 2021 @10:41AM (#29107798)
Portability is an orthogonal concern. I agree with you that it's the reason regexes are the way they are but we could have also had better cross platform APIs.
Re:Coconut: Simple, elegant, Pythonic functional programming by Zababa on Thursday November 04, 2021 @11:47AM (#29108636)
Regexes are still better than cross platform APIs, they're an abstract concept. Just like you can implement a tree or a graph in any language and transfer your "tree skills" or your "graph skills", you can do so with regexes.
Re:Coconut: Simple, elegant, Pythonic functional programming by mcdonje on Thursday November 04, 2021 @09:31AM (#29106934)
ok, now do the same check in procedural code like the comment you responded to did.
Re:Coconut: Simple, elegant, Pythonic functional programming by hobs on Thursday November 04, 2021 @09:36AM (#29106980)
Ad absurdum - what do you think is easier to memorize - a story (eg The Iliad) or an equivalently explanatory regular expression?
Re:Coconut: Simple, elegant, Pythonic functional programming by mcdonje on Thursday November 04, 2021 @12:33PM (#29109246)
Just trying to get an apples to apples comparison. He's using a relatively involved problem to say regex is difficult to parse. But the alternatives are libraries that hide the complexity, or large code blocks if you're rolling your own.
Re:Coconut: Simple, elegant, Pythonic functional programming by dekhn on Thursday November 04, 2021 @10:02AM (#29107309)
regular expressions have structure. That's why you can concatenate two regular expressions a b to ab and you have a resulting expression that will match a string if a matches its first part and b matches the rest

Mainly what you're showing is that regular expresisons are awkward for matching certain types of syntax. You could have split each of the unique components in your expression into small parts and it would be much more readable.

Re:Coconut: Simple, elegant, Pythonic functional programming by jimsimmons on Thursday November 04, 2021 @10:40AM (#29107783)
Completely agree. My point is that having things broken down and built up gradually (aka procedural) is a better approach than one shot declarations.
Re:Coconut: Simple, elegant, Pythonic functional programming by staticautomatic on Friday November 05, 2021 @01:13AM (#29115695)
If you aren’t writing verbose regexes and/or commenting the shit out of them, you’re really doing future self and others a disservice.
Re:Coconut: Simple, elegant, Pythonic functional programming by ReleaseCandidat on Thursday November 04, 2021 @12:01PM (#29108809)
I'd call

    return str == "abd" || str == "acd"
_way_ more elegant and simple, to be honest. Because the actual code would look like

    return match("a(b|c)d", str)
Re:Coconut: Simple, elegant, Pythonic functional programming by parhamn on Thursday November 04, 2021 @08:41AM (#29106401)
Maybe they mean syntax? I guess you could compare it to say syntax like "char 'a' + either char 'b' or char 'c' + char 'd'".

I like regex as is but that would def make it more approachable for the folks I know who don't.

Re:Coconut: Simple, elegant, Pythonic functional programming by chillfox on Thursday November 04, 2021 @10:48AM (#29107893)
I watched a video explaining monads where the presenter explained that simple meant small…

So yeah, I think they would call regex simple as well.

It honestly never occurred to me before watching that video that someone could mean small when they say simple. I have always understood simple to mean “easy to understand” or “low complexity”.

Re:Coconut: Simple, elegant, Pythonic functional programming by dekhn on Thursday November 04, 2021 @10:00AM (#29107271)
err.... regex, which are a part of a larger field of formal language processing, which is elegant (but not simple). https://en.wikipedia.org/wiki/Regular_language The whole area of parsing and grammar is fascinating.
Re:Coconut: Simple, elegant, Pythonic functional programming by jimsimmons on Thursday November 04, 2021 @10:47AM (#29107868)
Expressivity is independent of specification. You can specify the same language in multiple ways and some are more readable than others
Re:Coconut: Simple, elegant, Pythonic functional programming by dekhn on Thursday November 04, 2021 @10:58AM (#29108003)
Do you know of a more readable regular language parser than regular expressions? News to me.