Guest User

Untitled

a guest
Jun 20th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.50 KB | None | 0 0
  1. module Implem04
  2.  
  3. open System
  4.  
  5. type Age = int
  6. type FirstName = string
  7. type LastName = string
  8.  
  9. type Person =
  10. { FirstName : FirstName
  11. LastName : LastName
  12. Age : Age }
  13.  
  14. type ConsultantSkills =
  15. | CSharp
  16. | FSharp
  17. | Java
  18. | Finance
  19. | ECommerce
  20.  
  21. type ManagerBU =
  22. | SGCIB
  23. | Natixis
  24. | BNP
  25.  
  26. type Collaborator =
  27. | Consultant of Person * (ConsultantSkills list)
  28. | Manager of Person * ManagerBU
  29.  
  30. let isNull a =
  31. a |> List.fold (fun state str -> state || String.IsNullOrWhiteSpace(str) ) false
  32.  
  33. let createPerson firstName lastName age =
  34. if ( age < 0 ) || (isNull [firstName; lastName]) then
  35. None
  36. else
  37. { FirstName = firstName ; LastName = lastName ; Age = age } |> Some
  38.  
  39. let createConsultant firstName lastName age skills =
  40. createPerson firstName lastName age
  41. |> Option.map (fun person -> Consultant (person,skills) )
  42.  
  43. let createManager firstName lastName age bu =
  44. createPerson firstName lastName age
  45. |> Option.map (fun person -> Manager (person,bu))
  46.  
  47. let whoAreYou collaborator =
  48. match collaborator with
  49. | Consultant (p,_) -> printfn "I am a %i years old, consultant. My name is %s %s" p.Age p.FirstName p.LastName
  50. | Manager (p,_) -> printfn "I am a %i years old, manager. My name is %s %s" p.Age p.FirstName p.LastName
  51. collaborator
  52.  
  53. let whatDoYouDo collaborator =
  54. match collaborator with
  55. | Manager (_,businessUnit) -> printfn "I handle business on %s" <| sprintf "%A" businessUnit
  56. | Consultant (_,skills) ->
  57. match skills with
  58. | [] -> printfn "I have no skills yet !" // I handle the case where skills is an empty list !
  59. | _ -> // For any other case !
  60. let skillsString = List.reduce (fun acc elem -> acc + ", " + elem) ( skills |> List.map (sprintf "%A") )
  61. printfn "I work on %s" skillsString
  62. collaborator
  63.  
  64. let customPrint collaborator =
  65. printfn "***********************"
  66. collaborator
  67. |> (whoAreYou >> whatDoYouDo) |> ignore
  68. printfn "***********************"
  69.  
  70. [<EntryPoint>]
  71. let main argv =
  72. let createConsultant' skills (firstName,lastName,age) =
  73. createConsultant firstName lastName age skills
  74.  
  75. let createCsharpConsultant = createConsultant' [CSharp]
  76.  
  77. // We create some collaborators with C# skills
  78. [
  79. "Patrick","Sebastien",30
  80. "Julien","LePasBeau", 40
  81. "Foo","Bar",25
  82. ]
  83. |> List.map (fun info -> createCsharpConsultant info )
  84. |> List.iter (Option.map customPrint >> ignore)
  85. 0
Add Comment
Please, Sign In to add comment