Functional Programming Flashcards

1
Q

What is a high-order function?

A

Receives a function, returns a function or both.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is a Pure Function?

A

It is a function without side effects. Always returns the same output when the same input is defined.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is lazy evaluation?

A

Expression is not evaluated before the value is actually required.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is pattern matching?

A

?????

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is a value type?

A

The value never changes and behaves like a value.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is referential transparency?

A

The whole expression can be replaced by a simple value

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What does Steve McConnel wanted to say with “Program into the language” not “in the laguange?

A

Means that you should express your thoughts using the language. not allowing the language shape your thoughts.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What are few drawback object-oriented programming has that can be tackled by functional one?

A

Methods know too much (too many dependencies) which is very hard to manage in large projects.
Polymorphism increases the complexity a lot.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What happens to state properties of an object when its functions are converted to pure functions?

A

The properties must become readonly. otherwise we can’t ensure the same input produces the same output.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How to apply functional design to classes?

A

1 - Make public methods simple;
2 - Make them isolated;
3 - Do not expose complex functions;
4 - Let the consumer mix and match small functions;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What are the rules to create a pure function?

A

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;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is a tuple? By what was it superseded and when?

A

A tuple is a type that bundle values together. It was superseded by the ValueTuple on .net 4.7 and core 2.0.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is the problem with a tuple? How was it resolved?

A

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}.”);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is the main structual differences between tuple and value tuple? Why is not a good idea exposing a valueTuple in a public api?

A

Tuple are classes and ValueTuple is a struct. Not a good idea becuase valueTuple allows changing the values.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is Referential Transparency?

A

You can replace a method with its own produced output.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is Memoization?

A

Cache the results of a pure function to improve performance.

17
Q

What is dynamic programming?

A

In terms of mathematical optimization, dynamic programming usually refers to simplifying a decision by breaking it down into a sequence of decision steps over time.