What is Functional
Programming?
Incomputer science, functional programming is a programming
paradigm, a style of building the structure and elements of computer
programs, that treats computation as the evaluation of mathematical
functions and avoids changing-state and mutable data. It is a
declarative programming paradigm, which means programming is
done with expressions. In functional code, the output value of a
function depends only on the arguments that are input to the function,
so calling a function f twice with the same value for an argument x will
produce the same result f(x) each time. Eliminating side effects, i.e.
changes in state that do not depend on the function inputs, can make
it much easier to understand and predict the behavior of a program,
which is one of the key motivations for the development of functional
programming.
- Wikipedia, en
4.
What is Functional
Programming?
コンピュータサイエンスでは、functionalprogramming は、構造と要素
を組み立てるスタイル形式のコンピュータプログラムであり、処理
(computation)を数学的な関数の評価として扱い、ステートの変更
やデータの変化を避ける。それは、宣言的プログラミングパラダイムで
あり、プログラミングを表現式として行う。関数のコードでは、関数の
出力結果は、その関数への引数が唯一依存し、関数 f を引数 x で2度
呼び出した場合でも、それぞれの結果は毎度同じ f(x) であるものとす
る。副作用の排除、すなわちその関数の入力に依存しないステートの変
更、はプログラムの挙動を推測しやすく理解しやすくする事が可能にな
り、それが、functional programming で開発するモチベーションの
の一つである。
- Wikipedia En, - 翻訳:吉川
頭の体操
• Dictionary のArrayから、それぞれの key に対す
る値の和を求めた Dictionary を求めよ。
let outputs = reduce(inputs, [String:Int]()) { (a, b) in
var d = a
for (k, v) in b {
d[k] = (d[k] ?? 0) + v
}
return d
}
println(outputs) // [e: 10, w: 7]
e の和 と w の和を求める
たぶん Functional な方
法があるにちがいない
let inputs = [["e":5], ["w":4], ["e":3], ["w":3, "e": 2]]
Currying
カリー化 (currying, カリー化された=curried)とは、複数の引数をとる関
数を、引数が「もとの関数の最初の引数」で戻り値が「もとの関数の残り
の引数を取り結果を返す関数」であるような関数にすること(あるいはそ
の関数のこと)である。
(略)
ごく簡単な例として、f(a, b) = c という関数 f があるときに、F(a) = g(こ
こで、g は g(b) = c となる関数である)という関数 F が、f のカリー化
である。
- Wikipedia, ja
22.
Currying
In mathematics andcomputer science, currying is the
technique of translating the evaluation of a function that
takes multiple arguments (or a tuple of arguments) into
evaluating a sequence of functions, each with a single
argument (partial application). It was introduced by Moses
Schönfinkel and later developed by Haskell Curry.
- Wikipedia, en
Add
• これまでの加算
func add(x:Int, y: Int) -> Int {
return x + y
}
add(2, 3) // 5
• Functional ちっくな加算
func add(x: Int) -> (Int -> Int) {
return { y in return x + y }
}
add(2)(3) // 5
let add2 = add(2)
add2(3) // 5
25.
Currying
https://github.com/typelift/swiftz/blob/master/swiftz/Curry.swift
public func curry<A,B,C>(f:(A,B) -> C) -> A -> B -> C {
return { a in { b in f(a,b) } }
}
public func curry<A,B,C,D>(f: (A,B,C) -> D) -> A -> B -> C -> D {
return { a in { b in { c in f(a,b,c) } } }
}
public func curry<A,B,C,D,E>(f: (A,B,C,D) -> E) -> A -> B -> C -> D -> E
return { a in { b in { c in { d in f(a,b,c,d) } } } }
}
curry(+)(100)(2) // 102
curry(+)("Hello, ")("World") // "Hello, World"
26.
What curry<A,B,C>
cando for you?
func add(x: Int, y: Int) -> Int {
return x + y
}
public func curry<A,B,C>(f: (A,B) -> C) -> A -> B -> C {
return { a in { b in f(a,b) } }
}
curry(add)(100)(3) // 103
let add100 = curry(add)(100)
add100(3) // 103
27.
What’s gonna happento
Object Oriented
Programming?
Are they going to extinct?
28.
User Interface
• UIWindow,NSWindow は class なので参照!
• Foundation ぽい所は Functional
• UIやモデルなどプロパティやステートの更新を必要
とする部分は Object Oriented
var window = UIWindow(frame: UIScreen.mainScreen().applicationFrame)
window.backgroundColor = UIColor.whiteColor() // do not copy