Skip to content

Commit d53f3d6

Browse files
committed
add note
1 parent f76a180 commit d53f3d6

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

_code/CppCon/2023/thinking_functionally _in_c++.md

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,71 @@ date: 2024-10-29
44
url: https://www.bilibili.com/video/BV1BC4y1R7iL?p=19
55
---
66

7+
A different ways of thinking about a problem
8+
9+
ref: The forgotten functional pattern by Ben Deane for deeply functional topic
10+
11+
Paradigms:
12+
13+
- Imperative or Procedual: roots with c
14+
- Object Oriented: roots with simula
15+
- Functional: STL, Lambdas, Ranges
16+
17+
## Identifying code functionally
18+
19+
Functional Code Categories:
20+
21+
- Actions: Depend on when or how many times they are called. Observable changes occur.
22+
- Calculation: Depend only on their inputs and not when or how they are called. Calling them with the same inputs always results in the same outputs. No observable changes occur.
23+
- Data: Unchanging records od events. Used as input to calculations and actions. Records the results of calculations adn actions.
24+
25+
- Actions
26+
- Allow input to programs that is unknown when the program was written
27+
- Performing an action has consequences
28+
- Affect how a program executes
29+
- Calculation
30+
- Reliable
31+
- Encapsulated, has no effect outside of itself
32+
- Thread safe, since it is entirely salf contained, no ordering or locking is necessary
33+
- Data
34+
- Fundamental building block
35+
- Immutable, data doesn't change
36+
- Transparent, you can look at data and see what it is
37+
- Open to interpretation, data can mean different things to different components without changing value
38+
- Used by calculations and actions to communicate with other calculations and actions
39+
40+
Data is all about context
41+
42+
Breaking down a problem to actions, calculations and data.
43+
44+
std::copy_if
45+
46+
## Functions as Data
47+
48+
Passing functions to functions
49+
50+
Return functions from functions
51+
52+
currying: transform a function of several arguments to a function of a single argument.
53+
54+
RAII
55+
56+
Call C API through the lambda
57+
58+
std::forward
59+
60+
```cpp
61+
auto Oven = [](auto handle)
62+
{
63+
return [h=handle](auto func, auto&&... args) mutable
64+
{
65+
return func(h,std::forward<decltype(args)>(args)...);
66+
}
67+
}
68+
```
69+
70+
Creating an object wrapper using a lambda.
71+
72+
## Composable Functions
73+
774

0 commit comments

Comments
 (0)