This is some of my first real F# code ever. I have done a bit of reading and watched a few videos however. I chose to do a code kata for string calculator to try it out.
The kata I was working on is here by Roy Osherove, though I may have strayed a bit.
I am pretty happy with this, but wonder if there are things that could be better, or more idiomatic F#. I am specifically wondering if there is a better option to having the overloaded Add members. I'm also curious about the last test and exception handling best practices.
module Tests
open Xunit
open System
type Calculator() =
let delimiters = ",\n"
member x.Add (m:int, n:int list) =
match n with
|[] -> m
|y::ys ->
if y < 0 then failwith "No Negative Numbers"
x.Add((m+y),ys)
member x.Add (y:string) =
let numList = List.map (fun x -> x.ToString() |> Convert.ToInt32) (delimiters.ToCharArray() |> y.Split |> Seq.toList)
x.Add(0,numList)
[<Fact>]
let ReturnsNotNull() =
let calc = new Calculator()
Assert.NotNull (calc.Add "0,0")
[<Fact>]
let ReturnsZeroWhenZeros() =
let calc = new Calculator()
Assert.Equal(0,(calc.Add "0,0"))
[<Fact>]
let ReturnsOneWhenShouldBeOneOnLeft() =
let calc = new Calculator()
Assert.Equal(1,(calc.Add "1,0"))
[<Fact>]
let ReturnsOneWhenShouldBeOneOnRight() =
let calc = new Calculator()
Assert.Equal(1,(calc.Add "0,1"))
[<Fact>]
let ReturnsElevenWithStringOfNumbersThatTotalEleven() =
let calc = new Calculator()
Assert.Equal(11,(calc.Add "0,1,1,1,1,1,6"))
[<Fact>]
let ReturnsElevenWithStringOfNumbersThatTotalElevenDelimitedByNewLine() =
let calc = new Calculator()
Assert.Equal(11,(calc.Add "0,1,1\n1,2\n6"))
[<Fact>]
let ReturnsElevenWithStringOfNumbersThatTotalElevenDelimitedByNewLineNoNegativeNumbers() =
let calc = new Calculator()
try
calc.Add("0,1,-1\n1,2\n6") |> ignore
Assert.False true
with
| _ -> Assert.True true