7. F1 F2 F3
int int int string
F0
Left-to-Right
F1(input)
.Pipe(F2)
.Pipe(F3);
Left-to-Right
Top-to-Bottom
Pipe
input
F1(input).Pipe(F2).Pipe(F3);
8. 순수 함수는 블랙박스다.
일관성(참조 투명성)
부수 효과 Free
입력 출력함수부수 효과
(Side Effect)
변경
외부 데이터
변경 참조
비순수 함수
호출
입력 값만으로 출력 값이 결정된다.
블랙박스
입력 인수를 전달하여
출력 결과를 반환한다.
함수 내부
관심
입력과 출력에만 집중한다.
Declarative Programming
10. int Divide(int x, int y)
{
return x / y;
}
DivideByZeroException
How?
11. int Divide(int x, int y)
{
try
{
return x / y;
}
catch (DivideByZeroException excep)
{
…
}
return ?
}
?
bool TryDivide(int x, int y, out int result)
{
try
{
result = x / y;
}
catch (DivideByZeroException excep)
{
result = ?
return false;
}
return true;
}
?
int Divide(int x, int y)
{
if (y == 0)
throw new DivideByZeroException
return x / y;
}
?
12. int ReferentiallyTransparent(int x)
{
return x + 1;
}
int globalValue = 0;
int ReferentiallyOpaque(int x)
{
globalValue += 1;
return x + globalValue;
}
ReferentiallyTransparent(x) ReferentiallyTransparent(x)
ReferentiallyOpaque(x) ReferentiallyOpaque(x)
x 값이 변하지 않는다면,
13. x 값이 변하지 않는다면,
ReferentiallyTransparent(x) ReferentiallyTransparent(x)
항상
같은 결과 값
같은
인자 값
ReferentiallyTransparent(6) ReferentiallyTransparent(6) 7
ReferentiallyTransparent(5) ReferentiallyTransparent(4 + 1) 6
int ReferentiallyTransparent(int x)
{
return x + 1;
}
14. x 값이 변하지 않는다면,
ReferentiallyOpaque(6) ReferentiallyOpaque(6) 7
ReferentiallyOpaque(5) ReferentiallyOpaque(4 + 1) 6
항상
같은 결과 값
같은
인자 값
ReferentiallyOpaque(x) ReferentiallyOpaque(x)
int globalValue = 0;
int ReferentiallyOpaque(int x)
{
globalValue += 1;
return x + globalValue;
}
15. The function
always evaluates
the same result value
given the same argument value(s).
ReferentiallyTransparent(x) ReferentiallyTransparent(x)
x 값이 변하지 않는다면,
참조 투명성(일관성)을 갖는다.
(Referential Transparency)
16. 외부
데이터 변경
참조
타입 비순수 함수(매개 변수, …)
{
…
return …;
}
변경
외부
비순수 함수
호출
int ReferentiallyTransparent(int x)
{
return x + 1;
}
int globalValue = 0;
int ReferentiallyOpaque(int x)
{
globalValue += 1;
return x + globalValue;
}
부수 효과
(Side Effect)
함수의 실행이
외부에 영향을 끼치거나 참조하는 것.
17. 전역적 추론(Reasoning)이 필요하다.
미루어 생각하여 논함
코드를 이해하기 위해서
타입 비순수 함수(매개 변수, …)
{
…
return …;
}
외부
데이터
외부
비순수 함수
변경
호출
변경
참조
부수 효과
(Side Effect)
18. 외부
데이터 변경
참조
타입 순수 함수(매개 변수, …)
{
…
return …;
}
int ReferentiallyTransparent(int x)
{
return x + 1;
}
int globalValue = 0;
int ReferentiallyOpaque(int x)
{
globalValue += 1;
return x + globalValue;
}
변경
외부
비순수 함수
호출
부수 효과
(Side Effect)
19. 결과만
타입 순수 함수(매개 변수, …)
{
…
return …;
}
int ReferentiallyTransparent(int x)
{
return x + 1;
}
int globalValue = 0;
int ReferentiallyOpaque(int x)
{
globalValue += 1;
return x + globalValue;
}
외부
순수 함수
호출
부수 효과
(Side Effect)
참조
21. 결과만
Evaluation of the result
does not cause
any semantically observable side effect.
타입 순수 함수(매개 변수, …)
{
…
return …;
}
외부
순수 함수
호출
부수 효과
(Side Effect)
참조
부수 효과가 없다.
(Side Effects)
22. It has no side effects.
It is consistent.
(Referential Transparency)
This is
a property of expressions in general
and not just functions.
1 + 6
(this is not a function, but this is a expression.)
Pure FunctionMathematical Function …
23. 입력 출력함수
입력 값만으로
부수 효과
(Side Effect) 출력 값이 결정된다.
함수
외부
함수
외부
관심
(입력을 어떻게 얻을 것인가?)
관심
(결과로 무엇을 할 것인가?)
입력과 출력에만 집중한다.
Declarative Programming
24. int Divide(int x, int y)
{
if (y == 0)
throw new DivideByZeroException
return x / y;
}
int Divide(int x, int y)
{
try
{
return x / y;
}
catch (DivideByZeroException excep)
{
…
}
return ?
}
bool TryDivide(int x, int y, out int result)
{
try
{
result = x / y;
}
catch (DivideByZeroException excep)
{
result = ?
return false;
}
return true;
}
35. _canvas
FromEventPattern
(TextChanged)
이벤트
Select
(GetPosition)
마우스 위치
Buffer
(3)
연속 데이터
Select
(new Polygon)
그리기 객체
Subscribe
(Add)
그리기
Observable
.FromEventPattern<MouseButtonEventHandler, MouseButtonEventArgs>(
handler => _canvas.MouseLeftButtonDown += handler,
handler => _canvas.MouseLeftButtonDown -= handler)
.Select(e => e.EventArgs.GetPosition(_canvas))
.Buffer(3)
.Select((IList<Point> points) => new Polygon
{
Points = new PointCollection(points),
Stroke = Brushes.Blue,
StrokeThickness = 3
})
.Subscribe(t => _canvas.Children.Add(t));
36. Select
(object string)
타입 변경
Select
(object string)
타입 변경
FromEventPattern
(TextChanged)
이벤트
FromEventPattern
(TextChanged)
이벤트
색상
Subscribe
색상
Subscribe
활성화
Subscribe
CombineLatest
textBoxPW 조건
Select
(Length > 3)
textBoxID 조건
Select
(Regex.Match)