CF Functional Programming Net Visual Basic CJava Visual
从C#到F# Functional Programming . Net西安社区
Visual Basic
C#/Java Visual Basic
F# C#/Java Visual Basic
Scala F# C#/Java Visual Basic
Haskell Scala F# C#/Java Visual Basic
Lisp Haskell Scala F# C#/Java Visual Basic
A language that doesn’t affect the way you think about programming, is not worth knowing Alan Perlis
关于F# 来自微�研究院,自 2010年起就被VISUAL STUDIO支持。 ��者: Don Syme 开源,代�托管在GIT HUB上 git hub: https: //github. com/fsharp 跨平台,支持. NET CORE Mac、Linux、Windows 函数式�程�言 Functional Programming 11
C#和F#的�法区� public class Customer { public Customer(string name, int age) { _name = name; _age = age; } private readonly string _name; private readonly int _age; public string Name { get { return _name; } } public int Age { get { return _age; } } } 13
C#和F#的�法区� public class Customer { public Customer(string name, int age) { _name = name; _age = age; } private readonly string _name; private readonly int _age; public string Name { get { return _name; } } 代�已�有了��,真的需要�些大括号�? public int Age { get { return _age; } } } 15
C#和F#的�法区� public class Customer = public Customer(string name, int age) _name = name; _age = age; private readonly string _name; private readonly int _age; public string Name = 去掉大括号,用’=’来分割代�段 get { return _name; } public int Age = 16 get { return _age; } }
C#和F#的�法区� public class Customer = public Customer(string name, int age) _name = name; _age = age; private readonly string _name; private readonly int _age; public string Name = get { return _name; } 去掉多余的空行 public int Age = get { return _age; } } 17
C#和F#的�法区� public class Customer = public Customer(string name, int age) _name = name; _age = age; private readonly string _name; private readonly int _age; public string Name = get { return _name; } 重复太多 public int Age = get { return _age; } } 19
C#和F#的�法区� public class Customer = public Customer(string name, int age) … public string Name = get { return name; } public int Age = get { return age; } 直接使用构造函数的参数 } 20
C#和F#的�法区� public class Customer = public Customer(string name, int age) … public string Name = get { return name; } public int Age = get { return age; } } 构造函数有没有�独存在的必要性? 22
C#和F#的�法区� public class Customer(string name, int age) = public string Name = get { return name; } public int Age = get { return age; } } 构造函数移到�的定�中去 23
C#和F#的�法区� public class Customer(string name, int age) = public string Name = get { return name; } public int Age = get { return age; } } 作�一个 immutable的�,每一个属性都是 只�的,去掉 get怎么�? 25
C#和F#的�法区� public class Customer(string name, int age) = public string Name = return name; 去掉get public int Age = return age; } 26
C#和F#的�法区� public class Customer(string name, int age) = public string Name = return name; public int Age = return age; } ;能不能去掉? 27
C#和F#的�法区� public class Customer(string name, int age) = public string Name = return name public int Age = return age } 去掉; 28
C#和F#的�法区� public class Customer(string name, int age) = public string Name = return name public int Age = return age return 能不能去掉? } 29
C#和F#的�法区� public class Customer(string name, int age) = public string Name = name public int Age = age } 去掉return, 每一段代�的最后一句默��返回� 30
C#和F#的�法区� public class Customer(string name, int age) = public string Name = name public int Age = age } Immutable,默� public 31
C#和F#的�法区� class Customer(string name, int age) = string Name = name int Age = age } 去掉public 32
C#和F#的�法区� class Customer(string name, int age) = string Name = name int Age = age } 重复声明,���器帮我�推断 34
C#和F#的�法区� class Customer(string name, int age) = Name = name Age = age } 35
C#和F#的�法区� class Customer(string name, int age) = member this. Name = name member this. Age = age } 表示�是一个�例成� 36
C#和F#的�法区� class Customer(name: string, age: int) = member this. Name = name member this. Age = age } 38
C#和F#的�法区� type Customer(name: string, age: int) = member this. Name = name member this. Age = age } type替� class 40
C#和F#的�法区� public class Customer(string name, int age) { public string Name => Name; public int Age => Age; } C#7 �法支持 41
函数式�程 Class Inheritance for loop Method Dependency injection 43
函数式�程 Function 44
函数式�程思想-�合 public void Function. A() { //. . . } public void Function. B() { //. . . } �是函数式�程�? public void Function. C() { //. . } public void Do. Something() { Function. A(); Function. B(); Function. C(); } 49
函数式�程思想-函数 let add 1 x = x + 1 add 1 : x: int -> int let add x y = x + y 52
函数式�程思想-函数 let add 1 x = x + 1 let add x y = x + y add 1 : int -> int add : int -> int 53
函数式�程思想-�合函数 let add 1 x = x + 1 let multiply 2 x = x * 2 let add 1 Then. Multiply 2 x = add 1 >> multiply 2 let result = add 1 Then. Multiply 2 10 57
函数式�程思想-�合函数 let add 1 x = x + 1 let multiply 2 x = x * 2 let add 1 Then. Multiply 2 x = add 1 >> multiply 2 let result = add 1 Then. Multiply 2 10 58
函数式�程思想-�合函数 let add 1 x = x + 1 let multiply 2 x = x * 2 let add 1 Then. Multiply 2 x = add 1 >> multiply 2 let result = add 1 Then. Multiply 2 10 59
函数式�程思想-�合函数 let add 1 x = x + 1 let multiply 2 x = x * 2 let add 1 Then. Multiply 2 x = add 1 >> multiply 2 let result = add 1 Then. Multiply 2 10 60
函数式�程思想-管道符 let add 1 x = x + 1 let square x = x * x 5 add 1 6 square 36 let add 1 Then. Square x = x |> add 1 |> square 61
函数式�程思想-管道符 input add 1 input subtract 1 input square input divide. By 62 input subtract. By output
函数式�程思想-管道符 //定�每一步 let add 1 x = x + 1 let subtract 1 x = x - 1 let square x = x * x let divide. By x y = x / y let subtact. By x y = x - y 63
函数式�程思想-管道符 //定�每一步 let add 1 x = x + 1 let subtract 1 x = x - 1 let square x = x * x let divide. By x y = x / y let subtact. By x y = x - y //利用管道符�合每一步 input |> add 1 |> subtract 1 |> square 1 |> divide. By 2 |> subtract. By 1 64
函数式�程思想-�接 input function. A input output function. B output 66
函数式�程思想-�接 input function. A input 1 input 2 output function. B output 67
函数式�程思想-CURRY Haskell Curry 69
函数式�程思想-CURRY input 1 input 2 function output //Currying input 1 Curried function input 2 function output 70
函数式�程思想-CURRY public int Add. With. Two. Parameters(int x, int y) { return x + y; } 71
函数式�程思想-CURRY public int Add. With. Two. Parameters(int x, int y) { return x + y; } //Currying public Func<int, int> Add. With. Two. Parameters(int x) { Func<int, int> sub. Function = y => x + y; return sub. Function; } 72
函数式�程思想-CURRY let add x y = x + y //Currying let add' x = fun y -> x +y 73
函数式�程思想-PARTIAL APPLICATION Partial application 74
函数式�程思想-PARTIAL APPLICATION let add x y = x + y x: int -> y: int -> int 75
函数式�程思想-PARTIAL APPLICATION let add x y = x + y int -> int //�入第一个参数 let add' = add 1 int -> int 76
函数式�程思想-PARTIAL APPLICATION let add x y = x + y int -> int //�入第一个参数 let add' = add 1 int -> int //�入第二个参数 let add'' = add' 2 3 77
函数式�程思想-�接 input 1 input 2 input function. B function. A output 78
函数式�程思想-�接 input function. A input 1 input 2 input function. A input output function. B output 1 output 2 function. B output 79
函数式�程思想-OPTION Option 80
函数式�程思想-OPTION public Customer Get. Customer. By. Id(int id) { //. . . return null; } int -> Customer 81
函数式�程思想-OPTION public Customer Get. Customer. By. Id(int id) { //. . . return null; } int -> Customer public Option<Customer> Get. Customer. By. Id(int id) { //. . . return null; } 82 int -> Option<Customer>
函数式�程思想-OPTION type Option<'a> = | Some of 'a | None 83
函数式�程思想-OPTION let example input = let x = do. Something input if x <> null then let y = do. Something. Else x if y <> null then let z = do. AThird. Thing y if z <> null then let result = z result else null 84
函数式�程思想-OPTION let example input = let x = do. Something input if x. has. Value then let y = do. Something. Else x if y. has. Value then let z = do. AThird. Thing y if z. has. Value then let result = z result else None 85
函数式�程思想-BIND let example input = let x = do. Something input if x. has. Value then let y = do. Something. Else x if y. has. Value then let z = do. AThird. Thing y if z. has. Value then let result = z result else None Pyramid of doom 86
函数式�程思想-BIND let example input = let x = do. Something input if x. has. Value then let y = do. Something. Else x if y. has. Value then let z = do. AThird. Thing y if z. has. Value then let result = z result else None 87
函数式�程思想-BIND let if. Some. Do f opt = if opt. has. Value then f opt. Value else None 88
函数式�程思想-BIND let if. Some. Do f opt = if opt. has. Value then f opt. Value else None let example input = do. Something input |> if. Some. Do do. Something. Else |> if. Some. Do do. Third. Thing |> if. Some. Do … 89
函数式�程思想-MONADS Monads 90
函数式�程思想-MONADS input function. A output 1 output 2 bind input function. B output 1 output 2 let bind next. Function opt = if opt. has. Value then next. Function opt. Value else None 91
函数式�程思想-MONADS input output 1 output 2 do. Something bind input do. Something. Else output 1 output 2 bind input do. Third. Thing output 1 output 2 92
函数式�程思想-MONADS let example input = do. Something input |> bind do. Something. Else |> bind do. Third. Thing |> bind … 93
函数式�程思想-MONADS let task. Example input = let task. X = start. Task input task. X. When. Finished (fun x -> let task. Y = start. Another. Task x task. Y. When. Finished (fun y -> let task. Z = start. Third. Task y task. Z. When. Finished (fun z -> //. . . ) ) ) 94
函数式�程思想-MONADS let task. Example input = let task. X = start. Task input task. X. When. Finished (fun x -> let task. Y = start. Another. Task x task. Y. When. Finished (fun y -> let task. Z = start. Third. Task y task. Z. When. Finished (fun z -> //. . . ) ) ) bind let task. Bind f task = task. When. Finished (fun task. Result -> f task. Result) 95
函数式�程思想-MONADS let task. Bind f task = task. When. Finished (fun task. Result -> f task. Result) bind let task. Example input = start. Task input |> task. Bind start. Another. Task |> task. Bind start. Third. Task |> task. Bind … 96
函数式�程思想-MONADS public string Update. Customer() { var request = receive. Request(); validate. Request(request); update. DB(request); send. Email(request); return "OK" } 97
函数式�程思想-MONADS public string Update. Customer() { var request = receive. Request(); validate. Request(request); update. DB(request); send. Email(request); 理想 return "OK" } 98
函数式�程思想-MONADS public string Update. Customer() { var request = receive. Request(); var is. Valid = validate. Request(request); if(!is. Valid){ throw new Exception("request is invalid"); } try{ var result = update. DB(request); if(!result){ throw new Exception("update failed"); } } catch{ throw new Exception("db error"); } var is. Success = send. Email(request); if(!is. Success){ throw new Exception("send email failed"); } return "OK" } �� 99
函数式�程思想-MONADS type Result<'a> = | Success of 'a | Failure of string bind函数 let result. Bind f input= if input. name = “” then Failure “Name must not be blank” else if input. Email = “” then Failure “Email must not be blank” else Success input 100
函数式�程思想-MONADS let result. Bind f input= if input. name = “” then Failure “Name must not be blank” else if input. Email = “” then Failure “Email must not be blank” else Success input 使用bind函数 let update. Customer = receive. Request() |> result. Bind validate. Request |> result. Bind update. Db. From. Request |> result. Bind send. Email 101
- Slides: 103