Questions tagged [monad]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
2
votes
1answer
157 views

FP Free Monad vs OOP Dependency Injection

I've written my first moderately large project in functional style (in F#) and can see the advantages. The main challenge was to achieve the "Onion" architecture i.e. large and "smart" pure core / ...
3
votes
1answer
124 views

How can the `log` function be used for formal verification

In JavaScript I want to create a log function: function log(string) { console.log(string) } Obviously this causes side effects; it prints to the screen. And I have no control over its ...
1
vote
2answers
504 views

What's the proper way to think about state monads?

I've been using a functional approach in my programming of late. I know that mutability and state changes are big no nos within the paradigm. I now find myself working with databases and other data ...
12
votes
4answers
1k views

When programming in Functional style, do you have a single application state that you weave through the application logic?

How do I construct a system that has all of the following: Using pure functions with immutable objects. Only pass into a function data that the function it needs, no more (i.e. no big application ...
7
votes
1answer
212 views

IO Monadic code: standard vs flipped function composition

Below is a line that handles socket connections in a simple Haskell program. mainLoop :: Socket -> IO () mainLoop sock = accept sock >>= forkIO . handle . fst >> mainLoop sock The "...
2
votes
0answers
311 views

Clojure: Decomposing Logging, Metrics, and Business Logic from a Function

I was reading the post A Modern Architecture for FP that included a code snippet that the author wanted to decompose further. I don't know Haskell but I recognize enough to know that I've written many ...
0
votes
2answers
269 views

Monads in JavaScript

A monad is an object that has: A transformation that takes a type and produces a new type. In C# we call such a transformation a "generic type". We have a generic type M<T>, we have a ...
6
votes
4answers
1k views

Minimal programmer's definition of a monad

I am trying to formulate a definition of a monad without needing mathematical terms or Haskell to understand. Can a monad be thought of as a function that accepts a value and wraps it such that it ...
14
votes
2answers
4k views

What is a Comonad and how are they useful?

Recently I've been dusting off my knowledge on how Monads work. I've also been introduced to the concept of a 'Comonad', which is described as the inverse dual of a monad. However, I am impossible to ...
3
votes
2answers
154 views

Implementing a key-sensitive monad bind for key-value Map

Based on the List monad I set out to define Monad instances for the Map type from Data.Map, to perform chained unions and folds on Maps like lists, but with the efficient sorting and merging of Maps: ...
4
votes
3answers
212 views

Verbose Return Types

I've recently been writing some code that deals with 3rd parties -- obviously errors will happen so I'm using Either/Maybe monads where appropriate. As this is C# I'm also using async Tasks too. My ...
14
votes
1answer
1k views

How does the Free monad and Reactive Extensions correlate?

I come from a C# background, where LINQ evolved into Rx.NET, but always had some interest in FP. After some introduction to monads and some side-projects in F#, I was ready to try and step to the next ...
0
votes
0answers
168 views

History of the Maybe monad

A lot of languages feature something like Haskell's Maybe monad these days. I was wondering about its historic origin. Wikipedia has no problem explaining what a monad, or specifically the Maybe monad ...
7
votes
3answers
2k views

Should databases be viewed as Monads?

Because any kind of persistence updates/inserts/deletes represents in some sense a kind of state change in a database, it makes me wonder whether databases can be considered monads. We say the same ...
14
votes
4answers
1k views

What programming problems do Monads solve? [closed]

I've read a lot of posts that explain what monads are, how unit and bind work, some of them plunging straight into category theory so abstract (for me at least) that makes the eyes bleed, some ...
6
votes
2answers
1k views

Why does Scala name monadic composition as “for comprehension”?

Not sure if it's an appropriate question, but here it goes. I know Haskell's do notation pretty well. And I realized that Scala's "for comprehension" really is just mostly the same as do notation in ...
6
votes
3answers
981 views

What is the purpose of wrapped values in Haskell?

I've recently read an article about Functors, Applicatives and Monads in Haskell and it concludes with these statements: functors: you apply a function to a wrapped value using fmap or <$> ...
17
votes
4answers
1k views

Is the benefit of the IO monad pattern for handling side effects purely academic?

Sorry for yet another FP + side effects question, but I couldn't find an existing one which quite answered this for me. My (limited) understanding of functional programming is that state/side effects ...
4
votes
1answer
779 views

How to manage the state in a GUI app with Haskell

I am using wxHaskell to create a simple GUI that has typical components like Buttons, Panels, etc. When some of these components perform an action (like callback), the generic status of the ...
0
votes
1answer
235 views

How is the UnitOfWork pattern related to Monadic programming?

Looking over the definition of the Unit of Work pattern it seems very much like what a programmer would get if they implemented a Monad such as an IO or Transaction Monad. What makes the unit of work ...
6
votes
1answer
170 views

When we say a monad 'encapsulates a computation' - is this just saying 'wraps a functional transformation of data'?

When I think of the word 'computation' - my mind jumps to lambda calculus or operations on a state machine representing a CPU. It is quite a broad definition. Now some people talk about monads as '...
2
votes
1answer
1k views

When does a Monad become a hammer?

I realize my precursory understanding on Monads is severely lacking in detail considering my knowledge comes mostly from Douglas Crockford's Monads and Gonads talk and complicated with my sevear ...
95
votes
2answers
21k views

What is the “Free Monad + Interpreter” pattern?

I've seen people talking about Free Monad with Interpreter, particularly in the context of data-access. What is this pattern? When might I want to use it? How does it work, and how would I implement ...
1
vote
1answer
457 views

Picking a card from a shuffled deck

I'm pretty new to Haskell although I did some ML many moons ago. I'm trying to set up a deck of playing cards, shuffle them and then deal them. I have the deck and shuffle sorted (of a fashion) but I'...
20
votes
2answers
2k views

Are monads a viable (maybe preferable) alternative to inheritance hierarchies?

I'm going to use a language-agnostic description of monads like this, first describing monoids: A monoid is (roughly) a set of functions that take some type as a parameter and return the same type. ...
1
vote
1answer
309 views

What is this programming style? “Monoid-ic”?

In a moderately old blog post, Conal Elliot makes an interesting (if less than serious) argument that C is a purely functional language, by drawing a parallel between the combination of the C ...
2
votes
2answers
2k views

Why does a monad use “return” or “unit” rather than “lift”?

This is partly genuine curiosity, and partly a check on my understanding. I'm probably missing the point. In Haskell, why does a monad use operations called return or unit to describe putting a type ...
3
votes
2answers
171 views

Why doesn't monad take `(M a -> M b)`?

All apologies -- I'm still very much on the outside of Haskell looking in. Why does the bind for a monad have this signature: M a -> (a -> M b) -> M b and not M a -> (M a -> M b) -> M b i.e. ...
2
votes
1answer
330 views

Is this a monad in Java? (part 2)

My first try was on stackoverflow. I'm picking up on the answer there to improve my monad: StackOverflow - Is this a monad in Java? My goal is to write an example of a monad. I'm not trying to solve ...
0
votes
1answer
894 views

Either Monad and Exceptional Circumstances [duplicate]

I have a function returning an Either such as GetUserFromDb(int id). If the database is offline, should I catch the error in the function and wrap it in a failure / Left case or should I let it ...
13
votes
2answers
5k views

Why is the Scala Option type not called Maybe, just as in Haskell? [closed]

Why is the Scala Option type not called Maybe, just as in Haskell? Maybe makes a lot more "semantic sense" to me, but maybe Option has different behaviour I am not aware of. Is there any particular ...
9
votes
1answer
1k views

Better to use error monad with validation in your monadic functions, or implement your own monad with validation directly in your bind?

I'm wondering what's better design wise for usability/maintainability, and what's better as far as fitting with the community. Given the data model: type Name = String data Amount = Out | Some | ...
8
votes
1answer
653 views

What monad is the opposite of the error monad in haskell

In the error monad, the first failure halts any execution further just carrying the fault through any following binds. What monad halts on success only carrying forward successes, and basically ...
10
votes
1answer
804 views

Futures/Monads vs Events

In an application framework when performance impact can be ignored (10-20 events per second at max), what is more maintainable and flexible to use as a preferred medium for communication between ...
46
votes
5answers
5k views

Critique of the IO monad being viewed as a state monad operating on the world

The IO monad in Haskell is often explained as a state monad where the state is the world. So a value of type IO a monad is viewed as something like worldState -> (a, worldState). Some time ago I ...
28
votes
3answers
6k views

Different ways to see a monad

While learning Haskell I have faced a lot of tutorials trying to explain what are monads and why monads are important in Haskell. Each of them used analogies so it would be easier to catch the meaning....
8
votes
3answers
2k views

How Does The Maybe Monad Relate To The Option Type?

I was doing a presentation on F# and was discussing the Option type when someone in the audience asked me if the Option type is F#'s implementation of the maybe monad. I know that's not the case but ...
33
votes
7answers
10k views

Maybe monad vs exceptions

I wonder what are the advantages of Maybe monad over exceptions? It looks like Maybe is just explicit (and rather space-consuming) way of try..catch syntax. update Please note that I'm intentionally ...
4
votes
2answers
760 views

are f# computational expressions a form of aspect oriented programming?

are monads, or more specifically f# computational expressions, a form of aspect oriented programming? Update: f# workflow builders have methods other than bind and unit. They have hooks for lots of ...
9
votes
6answers
1k views

As a practitioner, why should I care about Haskell? What is a monad and why do I need it? [closed]

I just do not get what problem they solve.