Skip to content

Instantly share code, notes, and snippets.

@isaacabraham
Last active August 30, 2018 10:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save isaacabraham/882236bc83a35a02fdd9f7984ad0ccf8 to your computer and use it in GitHub Desktop.
Save isaacabraham/882236bc83a35a02fdd9f7984ad0ccf8 to your computer and use it in GitHub Desktop.
Test if the behaviour of two functions are the same.
// Some dummy data types
type Thing = Red | Yellow | Blue
type Foo = { Number : PositiveInt; Other : Thing }
/// First way
let groupByThenSortAndHead data =
data
|> Array.groupBy(fun x -> x.Other)
|> Array.map (
snd
>> Seq.sortByDescending(fun x -> x.Number)
>> Seq.head)
/// Second way
let sortThenDistinct data =
data
|> Array.sortByDescending(fun x -> x.Number)
|> Array.distinctBy(fun x -> x.Other)
/// Create a "test" function which returns true or false if some property holds.
let bothMethodsAreTheSame data =
groupByThenSortAndHead data = sortThenDistinct data
/// FsCheck tests the compareThem function with random data
Check.Quick bothMethodsAreTheSame
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment