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 ...
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 ...
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 ...
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:
...
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 [...] ...
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 ...
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 ...
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
...
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 ...
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.
...
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.
...
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.
...
7
votes
Accepted
F# inventory system
Some things:
let mutable itemCount = -1;
for _ in this.Items do
itemCount <- itemCount + 1
can become
...
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 ...
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. ...
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
<...
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'...
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 ...
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 ...
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
...
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 ...
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
...
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 ...
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 ...
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:
...
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 ...
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 ...
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 ...
6
votes
Accepted
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
f# × 364functional-programming × 84
beginner × 51
programming-challenge × 26
algorithm × 20
.net × 19
c# × 16
recursion × 16
performance × 12
game × 12
strings × 11
primes × 10
image × 9
asynchronous × 9
playing-cards × 8
sorting × 7
immutability × 7
unit-testing × 6
combinatorics × 6
random × 5
file-system × 5
array × 4
parsing × 4
json × 4
validation × 4