Functional Programming Flashcards
What is a high-order function?
Receives a function, returns a function or both.
What is a Pure Function?
It is a function without side effects. Always returns the same output when the same input is defined.
What is lazy evaluation?
Expression is not evaluated before the value is actually required.
What is pattern matching?
?????
What is a value type?
The value never changes and behaves like a value.
What is referential transparency?
The whole expression can be replaced by a simple value
What does Steve McConnel wanted to say with “Program into the language” not “in the laguange?
Means that you should express your thoughts using the language. not allowing the language shape your thoughts.
What are few drawback object-oriented programming has that can be tackled by functional one?
Methods know too much (too many dependencies) which is very hard to manage in large projects.
Polymorphism increases the complexity a lot.
What happens to state properties of an object when its functions are converted to pure functions?
The properties must become readonly. otherwise we can’t ensure the same input produces the same output.
How to apply functional design to classes?
1 - Make public methods simple;
2 - Make them isolated;
3 - Do not expose complex functions;
4 - Let the consumer mix and match small functions;
What are the rules to create a pure function?
1 - Will never modify its argument;
2 - Will never call a mutator on its argument;
3 - Will not have an out argument or in/out (ref) argument;
4 - Will never throw an exception;
5 - Will only tell its result through the return value;
What is a tuple? By what was it superseded and when?
A tuple is a type that bundle values together. It was superseded by the ValueTuple on .net 4.7 and core 2.0.
What is the problem with a tuple? How was it resolved?
The problem is that it is hard to read. Solved by the the following syntax:
(double Sum, int Count) t2 = (4.5, 3);
Console.WriteLine($”Sum of {t2.Count} elements is {t2.Sum}.”);
What is the main structual differences between tuple and value tuple? Why is not a good idea exposing a valueTuple in a public api?
Tuple are classes and ValueTuple is a struct. Not a good idea becuase valueTuple allows changing the values.
What is Referential Transparency?
You can replace a method with its own produced output.