Guest User

Map Parts to groupings based on column/wide layout using F#

a guest
Apr 27th, 2012
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 0.68 KB | None | 0 0
  1. // The record type Part
  2. type Part = {
  3.     Heading : string;
  4.     Text : string;
  5.     IsColumnLayout : bool;
  6. }
  7.  
  8. // Make some test data
  9. let r = { Heading="Wide part"; Text="This is a wide part"; IsColumnLayout=false }
  10. let g = { Heading="Column part"; Text="This is a column"; IsColumnLayout=true }
  11.  
  12. // A test list
  13. let parts = [r; r; g; g; g; r; g]
  14.  
  15. let b = true
  16. let c = not b
  17.  
  18. // The actual function, using pattern matching
  19. let rec mapParts = function
  20.     | p :: r :: tail when p.IsColumnLayout && r.IsColumnLayout -> (p, Some(r)) :: mapParts tail
  21.     | p :: tail -> (p, None) :: mapParts tail
  22.     | _ -> []
  23.  
  24. //Test it
  25. parts |> mapParts |> Seq.iter (printf "%A\r\n___\r\n")
Advertisement
Add Comment
Please, Sign In to add comment