-
Notifications
You must be signed in to change notification settings - Fork 0
/
plan.txt
179 lines (141 loc) · 8.64 KB
/
plan.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
Up next:
(15 min) Add test to show Java 8 mutability checks for lambdas
(30 min) Double check all examples
(10 min) Research: Does pattern matching require immutability?
Complexity examples:
- Shared state
- I don't know what influences the behavior of this function.
- I'm not sure how to recreate the steps that lead to the execution of this function (that produced an error)
- I don't know if this test will behave the same if I run it alone or with the full test suite
- Mutable state
- I don't know what effects this method has
- It seems that this test affects a subsequent test
- It seems like the order of these things are important, but I'm not sure why
- Running that method caused a problem somewhere else
- I'm not sure if I can cache this
- I have to mark this context as 'dirty'
(Outside your program)
- I'm not sure how to reproduce that computer
- I have to be careful about the shared development database
-
- Sequencing
- These things depend on each other - why do I have to hand-order them?
- I have to order these statements in a specific way
- I'm not sure if I can safely reorder these statements
* Do I need to add anything to the Von Neumann Languages page?
* Can I have more examples of converting code to pure func? - maybe a slide?
* Where to add the item about FP's core value being simplicity
* Expressions vs Statements - check for more
* Do we still want the 'FP Big Idea' sections?
* Add Java 8 examples in the slides
* Improve fold examples / visual representation
* Flesh out orders example with sorting, dup. removal, etc.
* Pattern matching should say 'not limited by type'
* Big code example comes after currying
* Create test isolation problem
Review notes from order processor:
/**
* Let's have an example in which the domain is:
*
* - Customers, InventoryItems, Orders, OrderLines
*
* we:
*
* - Need to read a file of orders.
* - Exemplify: Higher Order function for ensuring cleanup
* - Need to remove duplicate orders.
* - Exemplify: List methods for identifying uniques.
* - Need to convert to domain objects.
* - Exemplify: Map to domain objects
* - Need to validate the orders.
* - Exemplify: Higher Order function for validation, pattern matching, type usage (Invalid, Valid orders)
* - Need to normalize the formatting of certain fields, say phone number
* - Exemplify: Immutability
* - Need to report the invalid orders to a file and via email.
* - Exemplify: Partition
* - Need to total the order amounts.
* - Exemplify: Fold
* - Need to store the values in a database.
* - Exemplify: Higher Order function for ensuring cleanup
*
* Look at tracker remote import for examples.
*
* - Object mother for test data, with defs
* - Exemplify: immutability
*/
Notes:
- Tonight's presentation
- Going to see simple examples in a few languages, will not get too far into languages themselves
- Going to focus on FP fundamentals
- A Leaky Abstraction ?
- Allows for fine-grained control over program performance
- Von Neumann Languages
- Von Neumann Architecture (1945) - Control unit, arithmetic logic unit, memory communicate over buses. Keeps programs and data in main memory.
- FP made me look at imperative languages differently
- Saw certain things as complexity, which I didn't see before
- Understood FP in terms of its power to simplify
Compexity != difficulty != non-familiarity
- Mathematical properties: describe mapping things to categories / property of discovering behavior when mapped to other categories
- Why is parallelization hard?
- Mutability, shared state
- Not pure functions
- Referential transparency: analyzable in isolation
- (Somewhere): Example about proofs - what if we change a 'given'?
- Declarative programming: a trend as computers get faster
- Immutability: Spring, constructor injection, spring + scala
- Purity: 100% not realistic
- Higher order functions: FP big idea - simplifying things by getting rid of extraneous concepts/divisions: statements vs. expressions, functions vs. values. Remember - everything is an object?
- Language examples:
- Compromises (Streams, ranges, functional interfaces, defining functions as curryable, partial application requires special syntax, etc.)
- Code example:
- Show basic overview
- Show order processor
- Immutability, higher order functions
- Show test isolation problem
- Show test speed problem
- Show production code speed problem
- Pattern matching
- Immutability allows deconstruction / a thing is identified by its initial values
- Show unapply usage
- Practical advice
- Focus on ref. transp., immutability, pure functions
- Try it with test code
- Turn on random run order
- Turn on parallel execution
- Functional thinking
- Test data grouped by context
- Test data as defs
- (Somewhere) FP Big idea: making errors inexpressible
Guidelines:
- Beware the narrow focus. For each point, think more broadly to see if I'm omitting or missing something.
- Do NOT get 'ahead of my knowledge'.
- Practice the presentation to see how the time limit feels.
- Throw in a joke or two.
- When in doubt, lean on clarifying and exemplifying concepts rather than expounding on benefits
- Make small claims.
- Realize that the goal is just to impart a little knowledge. 90 minutes isn't going to change the world.
- Think about how I would explain it to a team member.
- Write the slide first, then read more - helps build confidence because I will find that I know more than I think.
Quotes:
"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 fp
"An expression is said to be referentially transparent if it can be replaced with its value without changing the behavior of a program (in other words, yielding a program that has the same effects and output on the same input)." - wikipedia fp
"For example, when programming in C, there will be a performance penalty for including a call to an expensive function inside a loop, even if the function call could be moved outside of the loop without changing the results of the program. The programmer would be forced to perform manual code motion of the call, possibly at the expense of source code readability. However, if the compiler is able to determine that the function call is referentially transparent, it can perform this transformation automatically." - wikipedia referential transparency
"Purely functional functions (or expressions) have no side effects (memory or I/O). This means that pure functions have several useful properties, many of which can be used to optimize the code:
"If the result of a pure expression is not used, it can be removed without affecting other expressions.
"If a pure function is called with arguments that cause no side-effects, the result is constant with respect to that argument list (sometimes called referential transparency), i.e. if the pure function is again called with the same arguments, the same result will be returned (this can enable caching optimizations such as memoization).
"If there is no data dependency between two pure expressions, then their order can be reversed, or they can be performed in parallel and they cannot interfere with one another (in other terms, the evaluation of any pure expression is thread-safe).
"If the entire language does not allow side-effects, then any evaluation strategy can be used; this gives the compiler freedom to reorder or combine the evaluation of expressions in a program (for example, using deforestation)." - wikipedia FP
"Functional programming is very much about using mathematics to reason about programs. To do so, one needs a formalism that describe the programs and how one can make proofs about properties they might have." - Daniel C. Sobral
Examples:
Tests which fail when run in Idea but pass in Maven due to test class ordering
Tests which cannot be run by themselves due to poor test isolation
Tests which have to be careful about cleaning up after themselves because of shared resources
Tests which are flaky due to sensitivity to outside influences such as the date
Use of closures to guarantee 'before and after' behavior
Idempotency desired, e.g. calculate new bank balance. Separate calculation from storage/retrieval.
Writing code without '='
Memoizing in groovy
How it all can fall down if something is mutable
Tasks:
- Read "Functional Thinking"
- Read chapter on Monads in Haskell book