Skip to main content
10 votes
Accepted

Hamming distance between two strings

When you have a function where not every possible input value can be mapped to one of the possible output values, you have three options: Allow fewer inputs, allow more outputs, or declare that your ...
Sara J's user avatar
  • 3,471
7 votes
Accepted

Monad object in C++

Answers to your questions. Have I defined a Monad object for C++? Yes. Despite the name and its association with pure functional programming languages, it's not really that special. You can write ...
G. Sliepen's user avatar
  • 58.3k
6 votes
Accepted

Leap year check in Haskell, using pattern matching or bind operator

The first one is a lot more readable, whereas the second one uses a "hack". I would go with the first one, except that I would use rem, which is a little bit faster....
Zeta's user avatar
  • 19.3k
6 votes

Struct implementation of immutable Maybe<T> monad

Your code formatting is mind-blowing. It is so unconventional, that it looks chaotic (even though there is probably a system to it). I had a really hard time reviewing it, even though the business ...
Nikita B's user avatar
  • 13k
6 votes
Accepted

Simple DFA using the State Monad

Use traverse instead of foldM + accumulate In a fold, you usually use a function that reduces (or folds) your input into a ...
Zeta's user avatar
  • 19.3k
5 votes
Accepted

Finding minimum scalar product using ST Monad

Somewhat old question, but still it's a pity it hasn't been answered :). Putting aside the asymptotically better solution suggested in the comments, and focusing just on the code: Pluses: Top-level ...
Petr's user avatar
  • 3,070
5 votes
Accepted

F# Either computation expression with while loop

There are a few things I would probably add or change about your implementation. When I create a new computation builder, I typically put the logic in a module, and then just call the functions in ...
Aaron M. Eshbach's user avatar
5 votes
Accepted

CompletableFuture Chain (Monad)

One thing that i'd like to propose is to replace CompletableFuture with home-brewed builder class that wraps CompletableFuture ...
nndru's user avatar
  • 166
5 votes
Accepted

Transforming XML to HTML with XSLT

Because every filename can have side effects, they are stored in string option types. I don't understand what you mean by that, but it's not particularly important for the rest of the question. Your ...
Mark Seemann's user avatar
  • 1,758
5 votes

Hamming distance between two strings

To address your title question ("Is it wrong..."), no, I wouldn't say that it's wrong. Using more top-level functions is nearly always a great way to decompose your problem into smaller, ...
bisserlis's user avatar
  • 3,281
4 votes

Struct implementation of immutable Maybe<T> monad

I'd like to expand on @NikitaB's answer, more specifically: Your code formatting is mind-blowing. It is so unconventional, that it looks chaotic (even though there is probably a system to it). Let'...
Mathieu Guindon's user avatar
4 votes
Accepted

Haskell limit number of recursions

Sheriff voice: Halt! You are breaking the laws! Your monad breaks the right- and left-identity law of >>=: ...
Zeta's user avatar
  • 19.3k
4 votes

Determining whether (x,y) points in the input constitute a function

Let's condense this down. What you're trying to check is that for any given x, the y values you're told are all the same. Now let's try to express this as succinctly as possible: Let's start with a ...
Vogel612's user avatar
  • 25.2k
4 votes
Accepted

Monadic list for a more functional approach to C#

Just a few points that I've noticed: Readonly property can be declared directly like this: private List<T> Items { get; } It is not clear if this class is ...
Xiaoy312's user avatar
  • 3,136
4 votes
Accepted

Loading Files from a Directory in Haskell

The loadData function looks just fine to me. I don't see a way it could be any more elegant given what it's doing. I do actually like the way you have separated ...
Fyodor Soikin's user avatar
4 votes
Accepted

Ackermann-Péter function call count using Writer monad

The first thing that jumps out is these lines: let (secondArg, count) = runWriter $ ackCount m (n-1) tell (Sum $ getSum count) You're not ...
ShapeOfMatter's user avatar
3 votes
Accepted

Haskell Sudoku Solver

hlint gives a bunch of valid advice. mapM_ collapses 9 lines into one thrice. gets and ...
Gurkenglas's user avatar
  • 3,724
3 votes
Accepted

Monad Transformers

The loop in the second one is precisely what combinators like sequence and map are meant to capture and factor away. Thus, I ...
Li-yao Xia's user avatar
3 votes
Accepted

Monad Implementation in Typescript

...
Stefan Hanke's user avatar
3 votes

Using monads and monad transformers (simple matching engine)

First of all, I would suggest using phantom types to track the side of an order. The following implementation is over-engineered using type families, but as you seemed to want a fancy solution, you ...
bartavelle's user avatar
3 votes
Accepted

Writing monadic Haskell to evaluate arithmetic expression

Update: Zeta suggested that what I gave was not really a review. So I present a review first: The data type EvalErr has both ...
sshine's user avatar
  • 741
3 votes
Accepted

C++17 Either implementation for error handling

It seems to me that you go a very long and very complicated way to do exactly what std::variant does; since you're tagging your question with ...
papagaga's user avatar
  • 5,757
3 votes
Accepted

Composite expected in C++

Design review I’m skeptical of the idea that anyone would ever need a type that returns a value or one of multiple error types. That seems a bit excessive. I mean, imagine a square root function that ...
indi's user avatar
  • 11.4k
2 votes

Algebraic Effect Monad to separate pure and impure code

This is not a direct and full answer, but whereas I did not end up getting the opportunity I was hoping for to try this out on a C# project (yet), I did hit a perfect case for one in an F# project. I ...
Kazark's user avatar
  • 273
2 votes

Leap year check in Haskell, using pattern matching or bind operator

In general, I'd go for the second sort of approach. It scales better. I'd eliminate four names and the hack like so, at the cost of fst and ...
Gurkenglas's user avatar
  • 3,724
2 votes

Haskell Sudoku Solver

Efficiency-wise, I don't think your data structure is ideal. Your operations to replace rows and columns will cause a lot of copying, and you also perform a lot of random access into the lists, which ...
Jules's user avatar
  • 421
2 votes
Accepted

Typescript monad for data being loaded

Since this is Code Review, and not Stack Overflow where you'd just want an answer... first a couple comments on your code: First off, this is not a monad with 4 shapes. This is a monad with 6 shapes. ...
Gerrit0's user avatar
  • 3,456
2 votes
Accepted

Monad to sample without replacement

This is an interesting problem, and you've written an interesting solution. With respect to your inefficient urn "store" -- the mapping of immutable urn references (...
K. A. Buhr's user avatar
2 votes
Accepted

How can I use the State monad effectively in Haskell?

I won't be reviewing this code for efficiency, since I'm not quite so comfortable with analyzing some combination of State monad, immutable data structures, and laziness. However, there are a few ...
cole's user avatar
  • 616
2 votes
Accepted

Hangman game in Haskell, written using a state monad

The below is pretty casual, sorry it's not better organized or cited. You're not using the bound attribute values in instance Show GameVars. Instead of ...
ShapeOfMatter's user avatar

Only top scored, non community-wiki answers of a minimum length are eligible