Questions tagged [monad]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
1 vote
3 answers
334 views

Exceptions vs Monads

I am curious about the utility of something like monads in the C# world. My experience with these kinds of things is mainly through Rust but I'm a dotnet dev for work I was thinking about the ...
Jeremy Farmer's user avatar
0 votes
1 answer
123 views

If we don't use function composition, does Maybe remain a monad?

A monad is a monoid in the category of endofuctors. Category is a set of two things: Set of elements Set of binary operations between these elements. When we talk about the category endofunctors we ...
pro100tom's user avatar
  • 449
2 votes
1 answer
166 views

Struggling to find the relation between a Maybe structure and the endofunctor

Monad is a monoid in the category of endofunctors. Endofunctor is a functor that maps to itself. What does it mean? Well it means that, no matter which element in the set is taken as an input, the ...
pro100tom's user avatar
  • 449
2 votes
2 answers
576 views

What's the value of IO Monad?

When I'm writing code in the form of IO Monad, I wonder what's real value of it.For example I have a function as def something(in: In): IO[Out]; my function is a pure function that **returns an impure ...
Reza Same'ei's user avatar
0 votes
1 answer
148 views

Imperative parallels to Haskell's Monad operations

Would it be (mostly) correct to say that the following are the parallels to the Haskell Monad operations in the imperative world? Monad's >> ~ C/C++/JavaScript/etc. , operator do expressions ~ ...
P Varga's user avatar
  • 329
5 votes
1 answer
1k 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 / ...
KolA's user avatar
  • 605
3 votes
1 answer
166 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 ...
user10869858's user avatar
2 votes
2 answers
707 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 ...
Ucenna's user avatar
  • 1,292
17 votes
4 answers
2k 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 ...
Daisha Lynn's user avatar
8 votes
1 answer
300 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 "...
Michal Charemza's user avatar
3 votes
0 answers
407 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 ...
MonkeyWithDarts's user avatar
0 votes
3 answers
435 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 ...
52d6c6af's user avatar
  • 730
6 votes
4 answers
2k 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 ...
52d6c6af's user avatar
  • 730
20 votes
0 answers
8k 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 ...
Qqwy's user avatar
  • 4,759
4 votes
2 answers
235 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: ...
Herng Yi's user avatar
  • 323
4 votes
3 answers
300 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 ...
mamidon's user avatar
  • 142
17 votes
1 answer
2k 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 ...
MLProgrammer-CiM's user avatar
0 votes
0 answers
242 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 ...
Frank's user avatar
  • 14.4k
7 votes
3 answers
3k 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 ...
Gaurav Abbi's user avatar
15 votes
4 answers
2k 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 ...
Dummy Me's user avatar
  • 195
7 votes
2 answers
2k 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 ...
xji's user avatar
  • 771
7 votes
3 answers
2k 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 <$> ...
Adam Arold's user avatar
  • 1,180
21 votes
5 answers
3k 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 ...
Stu Cox's user avatar
  • 321
4 votes
1 answer
1k 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 ...
Randomize's user avatar
  • 189
1 vote
1 answer
522 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 ...
Kevin's user avatar
  • 31
6 votes
1 answer
177 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 '...
hawkeye's user avatar
  • 4,819
3 votes
1 answer
2k 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 ...
Sukima's user avatar
  • 186
102 votes
2 answers
25k 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 ...
Benjamin Hodgson's user avatar
1 vote
1 answer
579 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'...
Robbie Dee's user avatar
  • 9,737
20 votes
2 answers
3k 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. ...
sea-rob's user avatar
  • 6,841
1 vote
1 answer
344 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 ...
Alex Celeste's user avatar
7 votes
2 answers
3k 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 ...
sea-rob's user avatar
  • 6,841
3 votes
2 answers
208 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. ...
sea-rob's user avatar
  • 6,841
2 votes
1 answer
353 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 ...
sea-rob's user avatar
  • 6,841
0 votes
1 answer
1k 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 ...
Blair Davidson's user avatar
15 votes
2 answers
7k 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 ...
fnl's user avatar
  • 261
9 votes
1 answer
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 | ...
Jimmy Hoffa's user avatar
9 votes
1 answer
740 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 ...
Jimmy Hoffa's user avatar
10 votes
1 answer
905 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 ...
c69's user avatar
  • 1,358
47 votes
5 answers
6k 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 ...
Petr's user avatar
  • 5,517
30 votes
3 answers
7k 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....
Oni's user avatar
  • 957
9 votes
3 answers
4k 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 ...
Onorio Catenacci's user avatar
40 votes
7 answers
15k 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 ...
Vladimir's user avatar
  • 503
4 votes
2 answers
857 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 ...
Charles Lambert's user avatar
10 votes
6 answers
2k 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.
Job's user avatar
  • 6,459