Showing posts with label Python. Show all posts
Showing posts with label Python. Show all posts

What are advantages & disadvantages of pure functions?

What are advantages & disadvantages of pure functions?



Advantages are:

- they are easy to read and test.
- memoization.
- easy to parallelize

Disadvantage:

- some functions need to perform i/o, which breaks this approach

What is a pure function?

What is a pure function?



Answer: A function with no side-effects that is a function of just it's inputs. Also returns the same results, given the same inputs.

What is memoization?

What is memoization?



Answer: Storing the output of a perfect function for future use to save on overhead of running it again, b/c you know the same input will generate the same output.

What are higher order functions?

What are higher order functions?



Answer: Functions that take other functions as arguments, or that return functions as results.

Why use 'with' when working with files?

Why use 'with' when working with files?



Answer: It creates a temp file object that's only live during the indented scope, and is closed automatically, even if there's an exception that kicks you out of that scope.

Why use an empty tuple?

Why use an empty tuple?



Answer: You can have a function that returns a populated tuple if a condition is met, or an empty one of it isn't

Why use a tuple?

Why use a tuple?



Answer: When u need an immutable data structure, and when the order of elements is important (like coordinates)