News for hackers. Stuff that matters.
Re:Coconut: Simple, elegant, Pythonic functional programming by pelagicAustral on Thursday November 04, 2021 @07:33AM (#29105760)
I always feel like functional programming languages start with some math major looking at traditional OOP code and thinking "ITS TOO READABLE!!"
Re:Coconut: Simple, elegant, Pythonic functional programming by dharmaturtle on Thursday November 04, 2021 @09:32AM (#29106945)
Kinda the other way around honestly. FP predates OO by decades. If you think of "traditional OOP" as C-style syntax, then you could conceivably think of C-style syntax as an attempt to make FP more readable.

Personally, I prefer FP-style (ML in particular) syntax. I think the fact that OOP languages are copying ML features like pattern matching, preference for immutability/call-by-value, and "do-notation" as a testament to FP's power.

Side note about syntax, honestly it's just glue. `<$>`, `|>`, `<*>`, and all the other symbols are just boring operations that are used so often that it's preferable to use symbols so programmers can focus on the meat. In other words:

    functionA <$> functionB |> functionC <*> functionD
is easier to read than

    functionA(map(functionB(pipe(functionC(bind(functionD))))))
Re:Coconut: Simple, elegant, Pythonic functional programming by pietrrrek on Thursday November 04, 2021 @08:08AM (#29106086)
My personal experience from working with functional languages at my university is that there is a certain familiarity-floor which has to be reached before you start to see the language as elegant. This doesn't even translate between languages - I've now worked with F#, Haskell, LISP and Erlang and all of them have required some practice before their elegance was visible to me, even though certain solutions seem to have a similar "shape" across these languages.
Re:Coconut: Simple, elegant, Pythonic functional programming by cdrini on Thursday November 04, 2021 @10:09AM (#29107380)
This is kind of a surprisingly myopic/inflammatory comment to see on HackerNews. Both systems have their pros and cons, and people tend to be biased to prefer the one they learned first or worked extensively with first. And you don't have to choose; most languages nowadays support elements from both paradigms.

But honestly there's nothing even to argue, cause the comment doesn't really say anything.

Re:Coconut: Simple, elegant, Pythonic functional programming by bayesian_horse on Thursday November 04, 2021 @11:01AM (#29108044)
Most languages I know don't really shine in both paradigms. Python even comes close, but it is shunned in OOP circles because of dynamic typing and loses street-cred in the FP hoods for its syntax.

C# feels clunky in both. F# is good in FP, has good interop with OOP, but why would you?

It definitely seems easier (or is more popular) to start people on imperative programming styles, because it is easier to make analogies. From there it's a quick jump to OOP.

Re:Coconut: Simple, elegant, Pythonic functional programming by anoctopus on Thursday November 04, 2021 @01:13PM (#29109850)
Python loses cred in FP circles for being rather opinionated against FP. Map/filter being second-class to list comprehensions, reduce and partial being in functools, and no multi-line lambdas are the obvious/well-known examples of this, but more generally python has first class functions but doesn't make creating and manipulating functions ergonomic.
Re:Coconut: Simple, elegant, Pythonic functional programming by gilch on Saturday November 06, 2021 @07:19PM (#29134795)
Where is the "no multi-line lambdas" myth coming from? I see this one a lot and its wrong. Python's lambdas can have as many lines as you want.

Did you mean to say that lambda expressions can't contain statements? But no Python expressions can! But functional programming is all done with expressions anyway, to the point where functional languages don't even have statements, so what's the problem?

Re:Coconut: Simple, elegant, Pythonic functional programming by anoctopus on Tuesday November 09, 2021 @01:31PM (#29165428)
No statements means no locals, which is sometimes bad for clarity and/or performance. Functional langs without statements have let-bindings.
Re:Coconut: Simple, elegant, Pythonic functional programming by gilch on Wednesday November 10, 2021 @08:12PM (#29183261)
That's really not true either. The lambda parameters are locals [a]. A let-binding is just a lambda that you call immediately. You can use defaults so the value doesn't have to appear as an argument [b]. Also, Python has assignment expressions now [c], and comprehensions are expressions which can have their own locals [d].

    >>> (lambda a, b=2:
    ...   print(a, a, b, b, c:=3, c, *[[d,d] for d in [4]][0])
    ... )(1)
    1 1 2 2 3 3 4 4
That's four different ways to get locals with no statements.
Re:Coconut: Simple, elegant, Pythonic functional programming by ReleaseCandidat on Thursday November 04, 2021 @11:56AM (#29108740)
> It definitely seems easier (or is more popular) to start people on imperative programming styles, because it is easier to make analogies

There are simply _way_ more 'imperative' programmers/teachersTtutorials/books/... than 'functional' ones.

Re:Coconut: Simple, elegant, Pythonic functional programming by bayesian_horse on Friday November 05, 2021 @03:22AM (#29116309)
Thinking back to my days of starting programming, it was a lot easier to get my head around "do this then that" rather than working in expressions and functional paradigms.

When teaching/instructing others, this impression only deepened.

It took a long while to even understand why functional programming has any appeal. I know that some university courses start out with scheme or lisp as a first language. But I suspect they get away with it because they have a captive audience and one that is already selected for high math tolerance.

Re:Coconut: Simple, elegant, Pythonic functional programming by ranit on Thursday November 04, 2021 @12:52PM (#29109526)
My wild guess is that GP comment was meant to "provoke" thoughtful comments in defence of FP. And regardless of GP's intend, it really succeeded in that regard - just looking at some of the responses.
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.