Skip to content

Instantly share code, notes, and snippets.

@mrkaspa
Created May 10, 2018 19:53
Show Gist options
  • Save mrkaspa/553fe794cbd1a774324d6fbfc9bba395 to your computer and use it in GitHub Desktop.
Save mrkaspa/553fe794cbd1a774324d6fbfc9bba395 to your computer and use it in GitHub Desktop.
Functors in fsharp
type SumFunctor<'T> = {
sumFn : 'T -> 'T -> 'T
diffFn : 'T -> 'T -> 'T
}
let createSumFunctor monoidOp negMonoidOp =
{
sumFn = monoidOp
diffFn = negMonoidOp
}
let sumable { sumFn = sumFn } a =
sumFn a a
let sum1 (a : float) =
let s : SumFunctor<float> = createSumFunctor (+) (-)
sumable s a
let sum2 (a : int) =
let s : SumFunctor<int> = createSumFunctor (+) (-)
sumable s a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment