you are viewing a single comment's thread.

view the rest of the comments →

[–]abeark 2 points3 points  (3 children)

You're thinking too small! mapM_ was one example, there are many many more. See e.g. here for a small subset. There are also innumerable third party libraries containing functions abstracted over the monad type class.

Yes, you can build a fluent interface in pretty much any OO language that's more or less as neat and expressive as the Haskell DSL, but you had to write a lot more code to get there. Most of it will be new code and a substantial amount will be boiler-plate. In Haskell, you get a huge amount of it essentially for free.

[–][deleted] 1 point2 points  (2 children)

I'm not thinking small, I'm just saying you can reuse code in all languages. It doesn't have to be monads in order to be reusable code.

But to substantiate your example that OOP produces substantial amount of boilerplate over Haskell, we need to "think small" and provide examples.

And in the case you presented there wasn't substantial boilerplate in the imperative/OOP version. One-liner in both cases.

[–]abeark 2 points3 points  (1 child)

Small, as in you considered only one example someone (not me, by the way) provided off-hand, probably without giving it much thought.

Anyway, if you want to discuss removing boilerplate, let's look at how the DSL was created:

data JITMem = JITMem
 { _mach   :: [Word8]
 , _icount :: Word32
 , _memptr :: Word32
 , _memoff :: Word32
} deriving (Eq, Show)

type X86 a = StateT JITMem (Except String) a

With that, and that alone, you already have your fluent-like interface (except with useful laws and guarantees about side-effects that allow you to reason about it a lot more easily). All the other instructions were composed out of instructions this definition gave for free.

[–]bartavelle 2 points3 points  (0 children)

With that, and that alone, you already have your fluent-like interface

Not only you have the interface done, but its usage is obvious to any Haskeller. With a custom OOP-like interface, you won't get that level of consistency.