Hot answers tagged

21 votes
Accepted

First real-world F# application - how "good"/idiomatic is it? (long!)

Overall I think it's fine. A couple of suggestions that leap out, based on a quick scan. CAVEAT: This code was written in the browser and not compiled -- sorry for any errors, but you should get the ...
user avatar
  • 501
20 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 ...
user avatar
  • 1,728
11 votes
Accepted

Detecting properly nested parenthesis using functional programming

The algorithm is overly complicated, and incorrect: for example, it will fail on the input (())(). I feel like it could ... use a more functional approach to ...
user avatar
  • 16.1k
11 votes
Accepted

Reading input from console in F# (as a sequence of lines)

What you have there is a value, but you probably want it to be a function: ...
user avatar
  • 1,728
11 votes
Accepted

Mock/Stub out filesystem in F# for unit testing

Even though the SOLID principles are known to be principles related to Object-Oriented Design, I'd still take a cue from the Dependency Inversion Principle. As APPP states it (ch. 11): "clients [...] ...
user avatar
  • 1,728
10 votes
Accepted

Project Euler 6: Difference between sum of squares and square of sum

Looks good to me. Couple of things to consider though: You could split out two functions, squareOfSum and sumOfSquares. I ...
user avatar
  • 16.1k
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 ...
user avatar
8 votes

Reading input from console in F# (as a sequence of lines)

Mark's response does answer as to the warning you received. An alternate approach could be ...
user avatar
  • 803
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 ...
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. ...
user avatar
7 votes

Splitting a sequence into equal segments

Functional-first style tends to prefer immutability over mutability, so the fact that you're using ref can sometimes signify that the code could be more idiomatic. ...
user avatar
  • 256
7 votes

Is this Bridges code F# idiomatic?

module Core This sounds way too general to me. Unless this code is in a namespace that you're not showing here, you should use a more descriptive name. ...
user avatar
  • 24k
7 votes
Accepted

F# inventory system

Some things: let mutable itemCount = -1; for _ in this.Items do itemCount <- itemCount + 1 can become ...
user avatar
7 votes
Accepted

Prime factorization in F#

(Apologies in advance for any bad F# syntax in this answer) One issue is that you're doing things statefully. In a functional style, you want functions to be pure, as much as possible- i.e. their ...
user avatar
  • 5,714
7 votes
Accepted

Unit testing F# code without using an existing test library (MSTest, xUnit, etc.)

Overall this looks ok, just a couple nitpicks: In assertAreEqual, you're using string concatenation and then passing the resulting string to formatting function. ...
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 <...
user avatar
  • 27k
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'...
user avatar
  • 1,728
6 votes

First real-world F# application - how "good"/idiomatic is it? (long!)

I don't really have any substantive comments about the code, just minor notes about the syntax. It's just an opinion, but I think the custom !<> operator is ...
user avatar
  • 256
6 votes

Poker Hands Kata in F#

I like this! Your use of discriminated unions and active patterns is quite elegant, I think. (Although exploiting that compare on discriminated unions does what it ...
user avatar
6 votes
Accepted

Locating a bitmap inside another (larger) bitmap with F#

try ... with | _ -> false is bad. Like, very bad. The equivalent in C# is ...
user avatar
  • 16.1k
6 votes
Accepted

CRUD (database layer) for F# with TypeProviders

new() = new Item() This is infinite recursion, in order to call the parameterless constructor, you call the parameterless constructor. It's not quite clear to me ...
user avatar
  • 24k
6 votes
Accepted

Generic build script using Fake

All of the match expressions can be simplified. For example, match fileExists nugetPackagesFile with | t when t = true -> ... | _ -> ... Can be ...
user avatar
  • 16.1k
6 votes

Mock/Stub out filesystem in F# for unit testing

I think that Mark's answer gives a great alternative perspective here. That said, I would probably write something that's quite similar to what you did here. There is a couple of small tricks that I ...
user avatar
6 votes
Accepted

Utopian Tree in F#

A few points: The value i on the second line is not used. It is customary to use underscore to name such values. The expression ...
user avatar
6 votes
Accepted

Small game written in F#

Overall, I like the code. The types are defined nicely, and the organization makes sense to me. I'm still looking in changes that could work and benefit the code, but so far, I've come up with this: ...
user avatar
  • 982
6 votes
Accepted

Find unique variants of a product

If I understand correctly, you're looking for the Cartesian product of the attributes in each attribute category. To get the Cartesian product, I've adapted* Eric Lippert's solution from his blog ...
user avatar
  • 16.1k
6 votes
Accepted

F#zzBuzz: Learning the easy way

First off, I don't see a real need for these two variables: let valueMod3 = value % 3 let valueMod5 = value % 5 To me, they just seem a little unnecessary. In ...
user avatar
6 votes
Accepted

Project Euler Problems #1-#5

After working for far too long on this, here is a review focusing mainly on performance issues, and possibly introducing some non functional stuff along the way. Lets take it problem by problem. But ...
user avatar
  • 11.5k
6 votes
Accepted

Swap even/odd characters

Building off Caridorc's pseudocode, this function would do it: ...
user avatar
  • 897

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