Skip to main content
Share Your Experience: Take the 2024 Developer Survey
23 votes

Return total balance from unsorted purchase items that may have specials

Paraphrasing Kent Beck (IIRC), I'd design my types so that concerns that change together are kept together, whereas concerns that change separately should be kept as separate as possible. With that ...
Mark Seemann's user avatar
  • 1,758
9 votes

Return total balance from unsorted purchase items that may have specials

Changing the data structure You're right to worry about the data structures. As Linus Torvals said: git actually has a simple design, with stable and reasonably well-documented data structures. In ...
Quentin Pradet's user avatar
8 votes
Accepted

You can't sink me

You are correct in your statement that this is a pretty standard OOP implementation. The key signs are lots of mutable state and explicit loops. It's hard to give a line-by-line critique because a ...
Alex's user avatar
  • 339
8 votes
Accepted

Doing an ROP style bind for two functions on the same input in a pipeline

I actually wouldn't bother defining a bind2 as this can lead to an explosion, bind3, 4, 5, 6. Instead I would define a simple computation builder to do this. So using your code from above. ...
Colin Bull's user avatar
7 votes

Swap even/odd characters

The solution should look like (pseudocode, I know little F# too ;) ): String |> ChunksOf 2 |> Map Reverse |> Flatten An example explains clearly <...
Caridorc's user avatar
  • 27.5k
7 votes
Accepted

American Checkers

One of the many benefits of F# is that it's a multiparadigmatic language; while it embraces a functional-first ideal, it clearly also enables you to write object-oriented code. This is useful if you'...
Mark Seemann's user avatar
  • 1,758
6 votes
Accepted

Swap even/odd characters

Building off Caridorc's pseudocode, this function would do it: ...
asibahi's user avatar
  • 927
6 votes
Accepted

Recursive implementation of a zip function in F#

Based upon my comment on that answer I've toyed a little to make a recursive sequence expression with roughly the same performance (on my computer at least) as ...
Sehnsucht's user avatar
  • 380
6 votes
Accepted

F# function to concatenate some DSL scripts with indentation

I'll start with the code and explain after ...
Sehnsucht's user avatar
  • 380
6 votes

You can't sink me

Imperative Programming vs. Functional Programming AlexFoxGill brought up the point that your code is structured a lot like something you'd write in an imperative language, however, the major ...
Ethan Bierlein's user avatar
6 votes

Printing a table

Firstly, sprintf doesn't only take one argument, that wouldn't be very useful. It takes as many arguments (and of corresponding types) as are mentioned in the ...
Fyodor Soikin's user avatar
6 votes
Accepted

Compute a value (and update a CSV record), based on prior value

First question - no, there is no way to get rid of this "initial" value completely. There is, however, a way to make it more intuitive that the values is "missing" - the ...
Fyodor Soikin's user avatar
6 votes
Accepted

Caesar cipher in F#

Disclaimer: I know almost no F#. But I know Haskell. So I think I should be able to give some insight. An F# expert can give you more, of course. Re-use of functions One thing that strikes me as odd ...
Zeta's user avatar
  • 19.3k
6 votes

Functionally find the index of the minimum value in an array

I think there's no need to use the match keyword here because you're not actually doing any pattern matching, only shadowing the name ...
TheQuickBrownFox's user avatar
6 votes
Accepted

Query a product from a list of brands

Unfortunately, there is at least one blatant issue with the program in its current state: unused arguments. Let's have a look at it and find out why that's important. Before that, a short disclaimer, ...
Zeta's user avatar
  • 19.3k
5 votes
Accepted

Console input prompt loop in F#

You can use Seq.initInfinite and Seq.find to do this, though I'm not sure it's actually better: ...
svick's user avatar
  • 24.3k
5 votes
Accepted

Anagram finder in F#

This looks like a good start, so the following is by no means a criticism, but there are various refactorings you can perform to make the code smaller, and more generic. As a general comment, I'd ...
Mark Seemann's user avatar
  • 1,758
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

F# function to concatenate some DSL scripts with indentation

In general, the techniques and logic appear to be fine. My suggestions for making this more idiomatic would include: Extract out the tuple into a record. Using a ...
Reed Copsey's user avatar
5 votes

Doing an ROP style bind for two functions on the same input in a pipeline

If you are chaining a series of results and combining them in various ways, then creating a computation expression (Colin's answer) is definitely the best approach. For fun, let's look at two other ...
Grundoon's user avatar
  • 501
5 votes
Accepted

Wild and changing business rules implemented with functional programming

The original requirement How can you tame these wild business rules? How can you build a system that will be flexible enough to handle both the complexity and the need for change? And how can you ...
Mark Seemann's user avatar
  • 1,758
5 votes
Accepted

Event sourcing using functional programming

The key concept in event sourcing is having an 'updater' function that takes an event and an old state, and returns a new state. Let's capture that as a type. The type can be very generic: ...
Yawar's user avatar
  • 215
5 votes

Implement array element swap in a functional language

First and foremost: while mutation may be generally frowned upon in F#, in some cases (like this one) it's acceptable to make a tradeoff of immutability vs. performance or maintainability. In this ...
Der Kommissar's user avatar
5 votes
Accepted

Wordpress RSS Parser

The Rss helper functions are inside the parse function, even though they don't use any of the state inside that scope. Consider ...
TheQuickBrownFox's user avatar
5 votes

Wordpress RSS Parser

So TheQuickBrownFox is spot on when they mention the potential exception with Seq.Cast<'T>, and I want to talk about that and introduce a couple new operators ...
Der Kommissar's user avatar
5 votes
Accepted

Validate a password mostly to NIST-2017 standards

I don't know F# at all, but I really like the code. You almost successfully separated the high-level code (first file) from the low-level code (second file). You could follow this principle also for ...
Roland Illig's user avatar
  • 21.3k
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

Implementing class equality/comparison in F#

There's a suggestion for [<AllowNullLiteral>]: I think this is a bad idea. You wrote a class that is a lat/long pair, as a result, it's a standard ...
Der Kommissar's user avatar
5 votes
Accepted

Verifying that three cards make a "Set"

I feel there should be some way to iterate over the four properties instead of writing four separate lines. You can use a higher-order function that takes in a function that returns a card property. ...
TheQuickBrownFox's user avatar

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