内容简介:macOS High Sierra 10.13.3.NET Core SDK 2.1.101
Tuple 是 FP 語言常見的型別,且 C# 7.0 也開始有 Tuple ,讓我們藉由 F# 學習該如何活用 Tuple 。
Version
macOS High Sierra 10.13.3
.NET Core SDK 2.1.101
JetBrains Rider 2017.3.1
F# 4.1
Definition
Tuple
將不同型別的 value 整合成單一 value
在將不同型別整合成單一型別,F# 提供了 3 種方式:
-
Tuple
-
Record
-
Union
其中 Tuple 與 Record 提供類似 AND 的方式將不同型別加以整合,而 Union 則提供類似 OR 的方式將不同型別加以整合。
Record 與 Tuple 的差異在於 Record 為 named type,須事先定義型別,而 Tuple 則為 unmamed type,可直接使用,Type Inference 會幫我們推導出 Tuple 型別。
建立 Tuple
相同型別
let dinner = ("green eggs", "ham")
將 green eggs 與 ham 兩個 string 整合成一個 dinner tuple。
Type Inference 自動推導出 dinner tuple 為 string * string 。
Tuple 型別表示法,是以 * 串接各型別,因為 Tuple 概念是 AND,所以使用 * 。
不同型別
let dinner = ("green eggs", 16);
若只是將 tuple 用在相同型別,則顯示不出 tuple 的威力,相同型別大可使用 Array 或 List ,tuple 讓我們可以將 不同型別 包在同一個 tuple。
Type Inference 自動推導出 dinner tuple 為 string * int 。
let nested = (1, (2.0, 3M), (4L, "5", '6'))
甚至也可以巢狀的方式使用 tuple。
分解 Tuple
Tuple 將不同 value 整合單一 tuple 後,馬上會遇到另外一個問題:
Q:如何將單一 tuple 分解成 value?
F# 提供 3 種方式:
-
fst()與snd() - Let Binding
- Pattern Matching
fst() 與 snd()
let students = ("John", "Smith")
printfn "%s" (fst students)
printfn "%s" (snd students)
若只是兩個 element 的 tuple,可使用 fst() 與 snd() function 分解 tuple。
沒有 trd() ,但可自行建立 let trd (_, _, z) = z
Let Binding
let students = ("John", "Smith", "Jessie")
let student1, student2, student3 = students
printfn "%s, %s, %s" student1 student2 student3
直接使用 let ,會自動將 tuple 分解到各別 value,也沒 element 數限制。
let students = ("John", "Smith", "Jessie")
let student1, student2, _ = students
printfn "%s, %s" student1 student2
若只需部分 element,可使用 _ 忽略之。
let students = ("John", "Smith", "Jessie")
let print (student1, student2, student3) =
printfn "%s %s %s" student1 student2 student3
students
|> print
當 tuple 傳進 function 參數時,可直接分解成 value。
Pattern Matching
let students = ("John", "Smith", "Jessie")
let print students =
match students with
| (student1, student2, student3) -> printfn "%s %s %s" student1 student2 student3
students
|> print
亦可使用 Pattern Matching 對 tuple 加以分解。
活用 Tuple
將 Tuple 當成參數
let values = (2, 3, 4)
let add (x, y, z) = x + y + z
values
|> add
|> printfn "%d"
// 9
values 為 tuple,而 add() 為 int * int * int -> int 。
當 values 傳進 add() 時,會自動分解成 x 、 y 與 z 。
Tuple vs. Partial Function Application
let add1 x y z = x + y + z let add2 (x, y, z) = x + y + z
雖然都是 x + y + z ,但 add1() 與 add2() 意義完全不同。
add1() 為 int -> int -> int -> int 。
add2() 為 int * int * int -> int 。
add1() 可使用 Partial Function Application 方式使用,也就是若只傳一個參數 int ,將回傳 int -> int -> int function,若傳入兩個參數 int ,將回傳 init -> int function。
add2() 則必須一次傳入整個 tuple,不能只傳 部分參數 。
Q:我該使用 Tuple 或 Partial Function Application 呢?
- 完全視
設計考量與語意 - Partial Function Application 使用上會比 Tuple 靈活
- 若你不希望被當成 Partial Function Application 使用,則使用 Tuple。
將 Tuple 當成回傳值
let divRem a b = let x = a / b let y = a % b (x, y)
有些 function 會希望一次回傳多個值,如 兩數相除 ,會希望同時傳回 商數 與 餘數 。
傳統程式語言我們會回傳 Array 或 List ,但有相同型別的限制;在 OOP 我們會回傳 Object ,但必須事先建立 class;但在 FP 我們可回傳 Tuple 。
Conclusion
- Tuple 不用事先定義型別,可以直接使用,特別適合 function 只使用一次的
參數或回傳值型別 - 在 OOP,若我們想將
多個參數合併成單一參數,或將多回傳值合併成單一回傳值,我們必須使用Object,但常常糾結是否該特別開一個 class 型別;但在 FP,我們可以隨時使用 tuple,也能享受 compile 強型別的檢查
Reference
以上所述就是小编给大家介绍的《深入探討 F# 之 Tuple》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:- 【1】JavaScript 基础深入——数据类型深入理解与总结
- 深入理解 Java 函数式编程,第 5 部分: 深入解析 Monad
- 深入理解 HTTPS
- 深入理解 HTTPS
- 深入浅出Disruptor
- 深入了解 JSONP
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Learn Python the Hard Way
Zed Shaw / Example Product Manufacturer / 2011
This is a very beginner book for people who want to learn to code. If you can already code then the book will probably drive you insane. It's intended for people who have no coding chops to build up t......一起来看看 《Learn Python the Hard Way》 这本书的介绍吧!