Skip to content

Instantly share code, notes, and snippets.

@dsyme
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save dsyme/99e208bf1801a47f96d1 to your computer and use it in GitHub Desktop.
Save dsyme/99e208bf1801a47f96d1 to your computer and use it in GitHub Desktop.
For John
type Building =
{ left : int
right : int
height : int }
let buildings = []
let getHeight building = building.height
let getWidth building = building.right - building.left
let getArea building = getHeight building * getWidth building
type Op =
{ map : Building -> int
reduce : int -> int -> int
zero : int }
let maxHeight =
{ map = getHeight
reduce =
(fun l r ->
if l < r then l
else r)
zero = System.Int32.MinValue }
let minWidth =
{ map = getWidth
reduce =
(fun l r ->
if l > r then l
else r)
zero = System.Int32.MaxValue }
let totalArea =
{ map = getArea
reduce = (+)
zero = 0 }
let fs = [ maxHeight; minWidth; totalArea ]
// Use the foldBack pattern:
// foldBack (extractor >> combiner) elements state
let limits =
[ for f in fs -> List.foldBack (f.map >> f.reduce) buildings f.zero ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment