Functional Reactive Programming in C++
Functional Reactive Programming in C++
Ivan Čukić
[email protected]
http://cukic.co
Functional design Reactive programming CODE: meetingcpp
Disclaimer
2
Functional design Reactive programming CODE: meetingcpp
Disclaimer
3
.
FUNCTIONAL DESIGN
Higher-order functions
Purity and referential transparency
Functional design Reactive programming CODE: meetingcpp
Higher-order functions
Purity
Immutable state
...
6
Functional design Reactive programming CODE: meetingcpp
Higher-order functions
f i n d _ i f ( b e g i n ( cs ) , end ( cs ) , is_error ) ;
7
Functional design Reactive programming CODE: meetingcpp
Higher-order functions
class functional_object {
public :
r e s u l t operator() ( ... ) const
{
...
}
};
8
Functional design Reactive programming CODE: meetingcpp
Word frequency
9
Functional design Reactive programming CODE: meetingcpp
Word frequency
9
Functional design Reactive programming CODE: meetingcpp
Word frequency
10
Functional design Reactive programming CODE: meetingcpp
11
Functional design Reactive programming CODE: meetingcpp
data | transfo r m (
[ ] ( char c ) {
return isalnum ( c ) ? c : ’\n ’ ;
})
12
Functional design Reactive programming CODE: meetingcpp
13
Functional design Reactive programming CODE: meetingcpp
14
Functional design Reactive programming CODE: meetingcpp
... | sort
| group_by ( equal_to < > ( ) )
{ a a a a a ... },
{ as as as as as . . . } ,
{ at at at at at . . . }
15
Functional design Reactive programming CODE: meetingcpp
{ a a a a a ... },
{ and and and and and . . . }
{ as as as as as ... },
( 181, a ),
( 163, and ) ,
( 39 , as ) ,
16
Functional design Reactive programming CODE: meetingcpp
( 181, a ),
( 163, and ) ,
( 39 , as ) ,
... | sort
| reverse
| take ( 2 )
( 439, the ),
( 256, of ),
17
Functional design Reactive programming CODE: meetingcpp
i n t answer ( )
{
std::cout « "Calculating the result\n" ;
r e t u r n 42;
}
...
auto r e s u l t = answer() ;
...
$ ./a.out
Calculating the result
19
Functional design Reactive programming CODE: meetingcpp
i n t answer ( )
{
std::cout « "Calculating the result\n" ;
r e t u r n 42;
}
...
auto r e s u l t = 42 ;
...
$ ./a.out
20
Functional design Reactive programming CODE: meetingcpp
21
Functional design Reactive programming CODE: meetingcpp
21
Functional design Reactive programming CODE: meetingcpp
21
Functional design Reactive programming CODE: meetingcpp
21
Functional design Reactive programming CODE: meetingcpp
21
Functional design Reactive programming CODE: meetingcpp
21
Functional design Reactive programming CODE: meetingcpp
Object-oriented design
22
Functional design Reactive programming CODE: meetingcpp
Object-oriented design
23
Functional design Reactive programming CODE: meetingcpp
Object-oriented design
24
Functional design Reactive programming CODE: meetingcpp
Object-oriented design
25
Functional design Reactive programming CODE: meetingcpp
26
Functional design Reactive programming CODE: meetingcpp
27
Functional design Reactive programming CODE: meetingcpp
28
Functional design Reactive programming CODE: meetingcpp
c l a s s world {
world w i t h _ p o p u l a t i o n ( i n t ) && ;
};
auto brave_new_world =
world.with_population(6) ; // error!
auto new_world =
move(world).with_population(420) ;
29
Functional design Reactive programming CODE: meetingcpp
c l a s s world {
world w i t h _ p o p u l a t i o n ( i n t ) && ;
};
30
Functional design Reactive programming CODE: meetingcpp
31
REACTIVE PROGRAMMING
What is reactive?
Functional design Reactive programming CODE: meetingcpp
What is reactive?
33
.
Functional design Reactive programming CODE: meetingcpp
Reactive systems
responds quickly
resilient to failure
responsive under workload
based on message-passing
35
Functional design Reactive programming CODE: meetingcpp
What is reactive?
36
Functional design Reactive programming CODE: meetingcpp
Ways to be reactive?
C: event call-backs
Java: event listeners
C++/Qt: signals and slots
Threads for all!
37
Functional design Reactive programming CODE: meetingcpp
Ways to be reactive?
C: event call-backs
Java: event listeners
C++/Qt: signals and slots
Threads for all!
even IO streams?
37
Functional design Reactive programming CODE: meetingcpp
Design components:
38
Functional design Reactive programming CODE: meetingcpp
39
Functional design Reactive programming CODE: meetingcpp
40
Functional design Reactive programming CODE: meetingcpp
41
Functional design Reactive programming CODE: meetingcpp
42
Functional design Reactive programming CODE: meetingcpp
43
Functional design Reactive programming CODE: meetingcpp
44
Functional design Reactive programming CODE: meetingcpp
45
Functional design Reactive programming CODE: meetingcpp
What is a source?
46
Functional design Reactive programming CODE: meetingcpp
47
Functional design Reactive programming CODE: meetingcpp
Small demo
48
Functional design Reactive programming CODE: meetingcpp
Small demo
49
Functional design Reactive programming CODE: meetingcpp
50
Functional design Reactive programming CODE: meetingcpp
mouse_stream |
/ / Pass the events to the mouse cursor ,
/ / and pass them to the next
/ / t r a n s f o r m a t i o n i n the c h a i n
tee ( mouse_cursor −>move_to ) |
/ / p r o j e c t i n g the mouse c o o r d i n a t e s on
/ / the x a x i s
transform ( p r o j e c t _ o n _ x ) |
top_ruler_marker −>move_to ;
51
Functional design Reactive programming CODE: meetingcpp
mouse_stream |
tee ( mouse_cursor −>move_to ) |
/ / I f we want to do something more
/ / complex with both streams a f t e r
/ / f o r k i n g , tee i s not r e a d a b l e
fork (
transfo r m ( p r o j e c t _ o n _ x ) |
top_ruler_marker −>move_to ,
transfo r m ( p r o j e c t _ o n _ y ) |
l e f t _ r u l e r _ m a r k e r −>move_to
);
52
Functional design Reactive programming CODE: meetingcpp
p o i n t o p e r a t o r ( ) ( const p o i n t &new_point )
{
m_point . x = m_point . x * .99
+ new_point . x * . 0 1 ;
m_point . y = ... ;
r e t u r n m_point ;
}
private :
p o i n t m_point ;
...
}; 53
Functional design Reactive programming CODE: meetingcpp
54
Functional design Reactive programming CODE: meetingcpp
mouse_stream |
tee ( mouse_cursor −>move_to ) |
fork (
map ( p r o j e c t _ o n _ x ) |
top_ruler_marker −>move_to ,
map ( p r o j e c t _ o n _ y ) |
l e f t _ r u l e r _ m a r k e r −>move_to ,
map(gravity_object()) |
g r a v i t y _ m a r k e r −>move_to
);
55
Functional design Reactive programming CODE: meetingcpp
Stream filtering
56
Functional design Reactive programming CODE: meetingcpp
m_previous_point = new_point ;
return result ;
}
private :
p o i n t m_previous_point ;
};
57
Functional design Reactive programming CODE: meetingcpp
mouse_stream |
tee ( mouse_cursor −>move_to ) |
fork (
...
/ / or tr a n sf o r m ( . . . ) | f l a t t e n
flatmap(continuous_points()) |
filter ( point_filter ) |
f i l t e r _ m a r k e r −>move_to
);
58
Functional design Reactive programming CODE: meetingcpp
Separate components
Reusable, composable transformations
Asynchronousness built into the software design
59
Functional design Reactive programming CODE: meetingcpp
Microservices?
60
Functional design Reactive programming CODE: meetingcpp
Microservices?
61
Functional design Reactive programming CODE: meetingcpp
Microservices?
62
Functional design Reactive programming CODE: meetingcpp
Microservices?
KDE Frameworks 5
63
Functional design Reactive programming CODE: meetingcpp
Libraries
SObjectizer
C++ Actor Framework
ZeroMQ
RxCpp
64
Functional design Reactive programming CODE: meetingcpp
Kudos:
Discount code:
meetingcpp
65