### re: Daily Challenge #81 - Even or Odd VIEW POST

F#

``````module DailyChallenge

open System.Text.RegularExpressions

let tuple2Map f (x, y) = (f x, f y)

let (|Greater|Smaller|Equal|) (x, y) =
if x > y then Greater
elif x < y then Smaller
else Equal

let evenOrOdd (nums : string) : string =
nums.Split(' ')
|> Array.partition (fun n -> Regex.Match(n, "[02468]\$").Success)
|> tuple2Map (Array.sumBy (int))
|> function
| Greater -> "Even is greater than Odd"
| Smaller -> "Odd is greater than Even"
| Equal -> "Even and Odd are the same"
``````

Tried to have a bit of fun with this one:

• Input strings are assumed to be of the form "1 2 3" etc.
• To save a `map` call for converting the strings to numbers, I used regular expressions for the partitioning.
• Implemented a little helper called `tuple2Map` for applying a function to both elements of a 2-tuple.
• Used an active pattern for deciding which of the sums is greater.

Tests:

``````module DailyChallengeTest

open FsUnit.Xunit
open Xunit
open DailyChallenge

[<Fact>]
let ``evens > odds``() =
evenOrOdd "1 2" |> should equal "Even is greater than Odd"

[<Fact>]
let ``evens < odds``() =
evenOrOdd "1 2 3" |> should equal "Odd is greater than Even"

[<Fact>]
let ``evens = odds``() =
evenOrOdd "1 3 4" |> should equal "Even and Odd are the same"
``````
code of conduct - report abuse