Skip to content

Instantly share code, notes, and snippets.

@essic
Last active June 18, 2018 15:42
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 essic/a2dfc8dd65ce61067635ae924313feed to your computer and use it in GitHub Desktop.
Save essic/a2dfc8dd65ce61067635ae924313feed to your computer and use it in GitHub Desktop.
// In here we want to show informations about collaborators in some consulting organization.
module BasicDemo
//Basic import, useful to access "String" by example"
open System
//This is type alias
type Age = int
type FirstName = string
type LastName = string
//This is a product type : Record
type Person =
{ FirstName : FirstName
LastName : LastName
Age : Age }
//This is a sum type
type ConsultantSkills =
| CSharp
| FSharp
| Java
| Finance
| ECommerce
//This is a sum type
type ManagerBU =
| SGCIB
| Natixis
| BNP
//This is a sum type
type Collaborator =
// 'Consultant' is a data contructor for 'Collaborator' type
// This data constructor takes as parameter a 'Person' record and a list of 'ConsultantSkills'
| Consultant of Person * (ConsultantSkills list)
// 'Manager' is another data constructor for 'Collaborator' type
// This data constructor takes a 'Person' record as well and a 'ManagerBU' value
| Manager of Person * ManagerBU
// ( |> ) : 'T1 -> ('T1 -> 'U) -> 'U
// function returns a Collaborator which is a Consultant
let createConsultant (firstName:string) (lastName:string) (age:int) (skills: ConsultantSkills list) : Collaborator =
//Consultant ({ FirstName = firstName ; LastName = lastName ; Age = age } , skills )
({ FirstName = firstName ; LastName = lastName ; Age = age } , skills) |> Consultant
// function returns a Collaborator which is a Manager
let createManager (firstName:FirstName) (lastName:LastName) (age:Age) (bu:ManagerBU) : Collaborator =
Manager ({ FirstName = firstName ; LastName = lastName ; Age = age } , bu)
// function to pretty print 'age', 'firtname' and 'lastname' of a Collaborator
let whoAreYou (collaborator:Collaborator) : Collaborator =
match collaborator with
| Consultant (p,_) -> printfn "I am a %i years old, consultant. My name is %s %s" p.Age p.FirstName p.LastName
| Manager (p,_) -> printfn "I am a %i years old, manager. My name is %s %s" p.Age p.FirstName p.LastName
collaborator
let whatDoYouDo (collaborator:Collaborator) : Collaborator =
match collaborator with
| Manager (_,businessUnit) -> printfn "I handle business on %s" <| sprintf "%A" businessUnit
| Consultant (_,skills) ->
match skills with
| [] -> printfn "I have no skills yet !" // I handle the case where skills is an empty list !
| _ -> // For any other case !
let skillsString = List.reduce (fun acc elem -> acc + ", " + elem) ( skills |> List.map (sprintf "%A") )
printfn "I work on %s" skillsString
collaborator
let customPrint (collaborator:Collaborator) : Unit =
printfn "***********************"
collaborator
|> (whoAreYou >> whatDoYouDo) |> ignore
printfn "***********************"
[<EntryPoint>]
let main argv =
// We create some collaborators
let collaborators =
[ createConsultant "Aly-Bocar" "Cisse" 32 [CSharp;FSharp]
createManager "Luffy" "Dragon" 20 SGCIB
createConsultant "Nobody" "Ulysse" 15 [] ]
//Let's iterate on every Collaborator and print !
collaborators
// List.iter : ('T -> unit) -> 'T list -> unit
|> List.iter customPrint
0
// In here we want to show informations about collaborators in some consulting organization.
module BasicDemo
//Basic import, useful to access "String" by example"
open System
//This is type alias
type Age = int
type FirstName = string
type LastName = string
//This is a product type : Record
type Person =
{ FirstName : FirstName
LastName : LastName
Age : Age }
//This is a sum type
type ConsultantSkills =
| CSharp
| FSharp
| Java
| Finance
| ECommerce
//This is a sum type
type ManagerBU =
| SGCIB
| Natixis
| BNP
//This is a sum type
type Collaborator =
// 'Consultant' is a data contructor for 'Collaborator' type
// This data constructor takes as parameter a 'Person' record and a list of 'ConsultantSkills'
| Consultant of Person * (ConsultantSkills list)
// 'Manager' is another data constructor for 'Collaborator' type
// This data constructor takes a 'Person' record as well and a 'ManagerBU' value
| Manager of Person * ManagerBU
// ( |> ) : 'T1 -> ('T1 -> 'U) -> 'U
// function returns a Collaborator which is a Consultant
let createConsultant firstName lastName age skills =
({ FirstName = firstName ; LastName = lastName ; Age = age } , skills) |> Consultant
// function returns a Collaborator which is a Manager
let createManager firstName lastName age bu =
Manager ({ FirstName = firstName ; LastName = lastName ; Age = age } , bu)
// function to pretty print 'age', 'firtname' and 'lastname' of a Collaborator
let whoAreYou collaborator =
match collaborator with
| Consultant (p,_) -> printfn "I am a %i years old, consultant. My name is %s %s" p.Age p.FirstName p.LastName
| Manager (p,_) -> printfn "I am a %i years old, manager. My name is %s %s" p.Age p.FirstName p.LastName
collaborator
let whatDoYouDo collaborator =
match collaborator with
| Manager (_,businessUnit) -> printfn "I handle business on %s" <| sprintf "%A" businessUnit
| Consultant (_,skills) ->
match skills with
| [] -> printfn "I have no skills yet !" // I handle the case where skills is an empty list !
| _ -> // For any other case !
let skillsString = List.reduce (fun acc elem -> acc + ", " + elem) ( skills |> List.map (sprintf "%A") )
printfn "I work on %s" skillsString
collaborator
let customPrint collaborator =
printfn "***********************"
collaborator
|> (whoAreYou >> whatDoYouDo) |> ignore
printfn "***********************"
[<EntryPoint>]
let main argv =
// We create some collaborators
let collaborators =
[ createConsultant "Aly-Bocar" "Cisse" 32 [CSharp;FSharp]
createManager "Luffy" "Dragon" 20 SGCIB
createConsultant "Nobody" "Ulysse" 15 [] ]
//Let's iterate on every Collaborator and print !
// List.iter : ('T -> unit) -> 'T list -> unit
collaborators
|> List.iter customPrint
0
// In here we want to show informations about collaborators in some consulting organization.
module BasicDemo
//Basic import, useful to access "String" by example"
open System
//This is type alias
type Age = int
type FirstName = string
type LastName = string
//This is a product type : Record
type Person =
{ FirstName : FirstName
LastName : LastName
Age : Age }
//This is a sum type
type ConsultantSkills =
| CSharp
| FSharp
| Java
| Finance
| ECommerce
//This is a sum type
type ManagerBU =
| SGCIB
| Natixis
| BNP
//This is a sum type
type Collaborator =
// 'Consultant' is a data contructor for 'Collaborator' type
// This data constructor takes as parameter a 'Person' record and a list of 'ConsultantSkills'
| Consultant of Person * (ConsultantSkills list)
// 'Manager' is another data constructor for 'Collaborator' type
// This data constructor takes a 'Person' record as well and a 'ManagerBU' value
| Manager of Person * ManagerBU
// ( |> ) : 'T1 -> ('T1 -> 'U) -> 'U
let isNull a =
//List.fold : ('State -> 'T -> 'State) -> 'State -> 'T list -> 'State
a |> List.fold (fun state str -> state || String.IsNullOrWhiteSpace(str) ) false
let createPerson firstName lastName age =
if ( age < 0 ) || (isNull [firstName; lastName]) then
None
else
{ FirstName = firstName ; LastName = lastName ; Age = age } |> Some
// function returns a Collaborator which is a Consultant
let createConsultant firstName lastName age skills =
createPerson firstName lastName age
|> Option.map (fun person -> Consultant (person,skills) )
// function returns a Collaborator which is a Manager
let createManager firstName lastName age bu =
createPerson firstName lastName age
|> Option.map (fun person -> Manager (person,bu))
// function to pretty print 'age', 'firtname' and 'lastname' of a Collaborator
let whoAreYou collaborator =
match collaborator with
| Consultant (p,_) -> printfn "I am a %i years old, consultant. My name is %s %s" p.Age p.FirstName p.LastName
| Manager (p,_) -> printfn "I am a %i years old, manager. My name is %s %s" p.Age p.FirstName p.LastName
collaborator
let whatDoYouDo collaborator =
match collaborator with
| Manager (_,businessUnit) -> printfn "I handle business on %s" <| sprintf "%A" businessUnit
| Consultant (_,skills) ->
match skills with
| [] -> printfn "I have no skills yet !" // I handle the case where skills is an empty list !
| _ -> // For any other case !
let skillsString = List.reduce (fun acc elem -> acc + ", " + elem) ( skills |> List.map (sprintf "%A") )
printfn "I work on %s" skillsString
collaborator
let customPrint collaborator =
printfn "***********************"
collaborator
|> (whoAreYou >> whatDoYouDo) |> ignore
printfn "***********************"
[<EntryPoint>]
let main argv =
// We create some collaborators
let collaborators =
[ createConsultant "Aly-Bocar" "Cisse" 32 [CSharp;FSharp]
createManager "Luffy" "Dragon" 20 SGCIB
createConsultant "Nobody" "Ulysse" 15 []
createManager "Kevin" "" 25 Natixis
createConsultant null "Something" 30 [Java]
createConsultant "Gandalf" "The Gray" -200 [Finance] ]
//Let's iterate on every Collaborator and print !
collaborators
|> List.iter (Option.map customPrint >> ignore)
0
// In here we want to show informations about collaborators in some consulting organization.
module BasicDemo
//Basic import, useful to access "String" by example"
open System
//This is type alias
type Age = int
type FirstName = string
type LastName = string
//This is a product type : Record
type Person =
{ FirstName : FirstName
LastName : LastName
Age : Age }
//This is a sum type
type ConsultantSkills =
| CSharp
| FSharp
| Java
| Finance
| ECommerce
//This is a sum type
type ManagerBU =
| SGCIB
| Natixis
| BNP
//This is a sum type
type Collaborator =
// 'Consultant' is a data contructor for 'Collaborator' type
// This data constructor takes as parameter a 'Person' record and a list of 'ConsultantSkills'
| Consultant of Person * (ConsultantSkills list)
// 'Manager' is another data constructor for 'Collaborator' type
// This data constructor takes a 'Person' record as well and a 'ManagerBU' value
| Manager of Person * ManagerBU
// ( |> ) : 'T1 -> ('T1 -> 'U) -> 'U
let isNull a =
//List.fold : ('State -> 'T -> 'State) -> 'State -> 'T list -> 'State
a |> List.fold (fun state str -> state || String.IsNullOrWhiteSpace(str) ) false
let createPerson firstName lastName age =
if ( age < 0 ) || (isNull [firstName; lastName]) then
None
else
{ FirstName = firstName ; LastName = lastName ; Age = age } |> Some
// function returns a Collaborator which is a Consultant
let createConsultant firstName lastName age skills =
createPerson firstName lastName age
|> Option.map (fun person -> Consultant (person,skills) )
// function returns a Collaborator which is a Manager
let createManager firstName lastName age bu =
createPerson firstName lastName age
|> Option.map (fun person -> Manager (person,bu))
// function to pretty print 'age', 'firtname' and 'lastname' of a Collaborator
let whoAreYou collaborator =
match collaborator with
| Consultant (p,_) -> printfn "I am a %i years old, consultant. My name is %s %s" p.Age p.FirstName p.LastName
| Manager (p,_) -> printfn "I am a %i years old, manager. My name is %s %s" p.Age p.FirstName p.LastName
collaborator
let whatDoYouDo collaborator =
match collaborator with
| Manager (_,businessUnit) -> printfn "I handle business on %s" <| sprintf "%A" businessUnit
| Consultant (_,skills) ->
match skills with
| [] -> printfn "I have no skills yet !" // I handle the case where skills is an empty list !
| _ -> // For any other case !
let skillsString = List.reduce (fun acc elem -> acc + ", " + elem) ( skills |> List.map (sprintf "%A") )
printfn "I work on %s" skillsString
collaborator
let customPrint collaborator =
printfn "***********************"
collaborator
|> (whoAreYou >> whatDoYouDo) |> ignore
printfn "***********************"
[<EntryPoint>]
let main argv =
// We create some collaborators
let collaborators =
[ createConsultant "Aly-Bocar" "Cisse" 32 [CSharp;FSharp]
createManager "Luffy" "Dragon" 20 SGCIB
createConsultant "Nobody" "Ulysse" 15 []
createManager "Kevin" "" 25 Natixis
createConsultant null "Something" 30 [Java]
createConsultant "Gandalf" "The Gray" -200 [Finance] ]
//Let's iterate on every Collaborator and print !
collaborators
|> List.iter (Option.map customPrint >> ignore)
printfn "--- New EXAMPLE ! ---"
let createConsultant' skills (firstName,lastName,age) =
createConsultant firstName lastName age skills
let createCsharpConsultant = createConsultant' [CSharp]
[
"Patrick","Sebastien",30
"Julien","LePasBeau", 40
"Foo","Bar",25
]
|> List.map (fun info -> createCsharpConsultant info )
|> List.iter (Option.map customPrint >> ignore)
0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment