设为首页 加入收藏

TOP

Swift 4.0 废弃的柯里化
2017-10-09 13:35:06 】 浏览:3059
Tags:Swift 4.0 废弃 柯里化

// 柯里化 

// http://www.jianshu.com/p/6eaacadafa1a                               Swift 2.0 柯里化方法 (废弃)

// http://www.swifthumb.com/thread-16079-1-1.html                      Swift 3.0 柯里化常用方法推荐

// http://www.ruanyifeng.com/blog/2012/04/functional_programming.html  函数式编程

/*

 Curried function declaration syntax func foo(x: Int)(y: Int) is of limited usefulness and creates a lot of language and implementation complexity. We should remove it.

 

 (函数的 currying 特性的使用场景并不大,但他会增加很多语言的复杂性,所以需要删除它)

 */

 

 1 class Currying
 2 
 3 {
 4 
 5     // uncurried:普通函数
 6 
 7     // 接收多个参数的函数(与类相关的函数,统称为方法,但是这里就直接说函数了,方便理解)
 8 
 9     func add(a: Int, b: Int, c: Int) -> Int{
10 
11         print("\(a) + \(b) + \(c)")
12 
13         return a + b + c
14 
15     }
16 
17     
18 
19     // curried:柯里化函数 --> 本质函数式编程思想
20 
21     func addCur(_ a: Int) -> (Int) -> (Int) -> Int{
22 
23         return {
24 
25             b in
26 
27             return {
28 
29                 c in
30 
31                     a + b + c
32 
33             }
34 
35         
36 
37         }
38 
39     }
40 
41 }
42 
43  
44 
45 let curry = Currying()
46 
47 var number = Currying.addCur(curry)(12)(23)(12)
48 
49 print(number)
50 
51  
52 
53 // NO.2
54 
55 let datePrint:(Int)->(Int)->(String)->Void =
56 
57 {
58 
59     month in
60 
61     print("\(month)月")
62 
63     return{
64 
65         day in
66 
67         print("\(day)日")
68 
69         return{
70 
71             action in
72 
73             print("\(action)")
74 
75         }
76 
77     }
78 
79 }
80 
81 let actionPrint = datePrint(2016)(11)
82 
83 actionPrint("写详细")
84 
85  

 

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇字符串常用方法 下一篇Swift 学习指引

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目