News for hackers. Stuff that matters.
Re:Attitudes That Lead to Maintainable Code by lmm on Wednesday March 08, 2017 @11:01AM (#13821099)
Sure, but to my mind monads are common and broadly understood, at least compared to all the alternatives for achieving the same kind of thing (e.g. AOP shudder).
Re:Attitudes That Lead to Maintainable Code by hueving on Wednesday March 08, 2017 @12:45PM (#13821912)
> but to my mind monads are common and broadly understood

Wow, you need to talk to more developers. Not just in the sense to broaden your views, but to improve your skills as a developer. Code is written to be read by humans (and orthogonally executed by machines). If your mental model of what is broadly understood by programmers is that far off, you are likely churning out lots of unmaintainable code.

Functional programming makes up a tiny slice of the programming world and most people that aren't doing functional programming (and even some who are) don't have a strong grasp of monads.

Re:Attitudes That Lead to Maintainable Code by AnimalMuppet on Wednesday March 08, 2017 @12:15PM (#13821646)
Within FP, sure, monads are common and (at least should be) broadly understood. Outside of FP, though, monads are neither common nor well understood.

You make these broad statements about monads as if FP was the whole programming world. It's not. It's probably less than 5% of it.

Re:Attitudes That Lead to Maintainable Code by smaddox on Wednesday March 08, 2017 @01:22PM (#13822294)
But isn't typical high-quality OOP code essentially Monadic code? State is encapsulated in an object and transformed by method calls. Method calls can be chained, and state can be encapsulated and extracted using constructors and accessors, respectively. The basic operations are all the same. OOP is just less strict with how you can transform objects, i.e. function/method purity is not enforced.
Re:Attitudes That Lead to Maintainable Code by catnaroek on Wednesday March 08, 2017 @02:46PM (#13823101)
> But isn't typical high-quality OOP code essentially Monadic code?

Nope.

> State is encapsulated in an object and transformed by method calls.

Monads aren't objects (which necessarily have a runtime manifestation). Monads (as used in functional programming) are abstract type constructors (which normally don't have a runtime manifestation).

> Method calls can be chained, and state can be encapsulated and extracted using constructors and accessors, respectively.

Monadic bind satisfies three concrete, clearly defined algebraic laws. The use of monads in functional programming arose from the field of denotational semantics, which gives “programs” (by which functional language semanticists mean “expressions”) precise mathematical meanings in a compositional manner.

Re:Attitudes That Lead to Maintainable Code by smaddox on Wednesday March 08, 2017 @09:17PM (#13826305)
I understand what a Monad is. My point is that high-quality OOP code typically adheres to the rules of Monads.

I didn't say Monads were objects. I simply talked through the most common properties and operations of OOP code, in order to show how they are closely related to the operations on Monads.

I understand that OOP code does not, in general, satisfy the constraints of Monads. Hence, why I specified high-quality OOP code, which, in contrast, typically does.

Re:Attitudes That Lead to Maintainable Code by catnaroek on Thursday March 09, 2017 @12:22AM (#13827188)
> My point is that high-quality OOP code typically adheres to the rules of Monads.

Last I checked, there are only three laws: left/right identity and associativity, and they are just algebraic laws that have nothing to do with your code being “good” or “bad”. Or do you have some other meaning of “rules of monads” in mind?

Furthermore, every higher-order strict language is already monadic, without you having to do anything special. Thus, monads aren't opt-in, or even opt-out; they're there whether you like it or not. This monadic structure permeates the language, regardless of whether you write “good” or “bad” programs in it. For more info, see here: https://news.ycombinator.com/item?id=13823145

Re:Attitudes That Lead to Maintainable Code by smaddox on Saturday March 11, 2017 @10:57AM (#13845799)
Yes, those are the laws. The theorems are more interesting from a maintaining-software perspective, though.

Yes, all higher-order strict languages can be embedded in a monadic model. However, there's an important practical difference between a single Monad with the entire application or computer's state encapsulated (course grain encapsulation), and many Monads with fine-grained encapsulation. High-quality OOP code can be embedded in the latter, and that's where the maintainability comes from.

--- Perhaps, if you focused more on understanding the point I'm trying to make instead of proving me wrong, this conversation would be more beneficial to both of us.

Re:Attitudes That Lead to Maintainable Code by catnaroek on Wednesday March 08, 2017 @02:52PM (#13823145)
Computation in every higher-order, strict language has an intrinsic monadic structure. That includes pretty much every object-oriented language in existence. (Ironically, it doesn't include Haskell.)
Re:Attitudes That Lead to Maintainable Code by AnimalMuppet on Wednesday March 08, 2017 @03:08PM (#13823295)
ELI5: What do you mean here by "higher-order"?

And, by "intrinsic", do you mean that "the language does that, but the programmer doesn't have to do anything to make it happen, or even know"? If so, doesn't this contradict your answer to smaddox?

Re:Attitudes That Lead to Maintainable Code by marcosdumay on Wednesday March 08, 2017 @03:23PM (#13823442)
I imagine the GP is talking about how code in procedural languages is equivalent to monadic code on pure languages.

If so, I don't know why the "higher-order" restriction is there. It seems unneeded. Also, it about is as much something "the programmer doesn't have to do anything or even know" as monadic code on languages that have specialized syntactic sugar, like Haskell. It creates plenty of details the programmer has to think about, the only difference is that in procedural languages all the details are always there, while in pure ones the detail set changes all the time.

Re:Attitudes That Lead to Maintainable Code by catnaroek on Wednesday March 08, 2017 @03:27PM (#13823469)
I'm not talking about “pure languages” (whatever that is) at all.
Re:Attitudes That Lead to Maintainable Code by catnaroek on Wednesday March 08, 2017 @03:20PM (#13823420)
> What do you mean here by "higher-order"?

Procedures as first-class values that you can store in variables, pass as arguments to other procedures, etc.

> And, by "intrinsic", do you mean that "the language does that, but the programmer doesn't have to do anything to make it happen, or even know"?

Yes. Although, IMO, programmers benefit from knowing it, for more or less the same reasons civil engineers benefit from knowing the laws of nature.

> If so, doesn't this contradict your answer to smaddox?

No. Monads, as abstract type constructors, are already hardcoded into the result type of every computation. (This is the difference between, say, an int like “2”, and a computation that produces an int like “1 + 1” or “fix (\x -> x + 1)”.) So hardcoded, in fact, that there doesn't have to be syntax for it, because there's no way you can not say it.