Kyle Simpson
June 15, 2022
78

# On the job interview... Composition

In this talk, we’re going to pretend we’re on a job interview, and have been given a single starting function. Using only this function, we have to build up many more functions. Can we pass the interview and get the job!?

June 15, 2022

## Transcript

2. ### Hello, welcome to Inter-Dot Web Solutions! I’m Maria, and I’ll

be conducting your technical screen interview today.
3. ### Thanks, I’m excited to be here and ready to jump

into the technical interview!
4. ### Let’s jump right in, then. I’m going to give you

a single function, and we’ll use that function throughout the interview.
5. ### Can you tell me what this function does? Can you

write some code that uses it?
6. ### Fantastic, well done! Now, what if we wanted to compose

more than two functions, like this?

8. ### Great. But is there another way you could use “composeTwo”

to express the composition?

11. ### Indeed, nice. “f”, “g”, and “h” functions all produce the

same output. But are they the same kind or shape?

13. ### We can use equational reasoning to substitute the deﬁnition of

“composeTwo” inline, and compare.
14. ### What do we know about the diﬀerences between how “f”

would execute compared to either “g” or “h”?
15. ### “f” will have a call-stack depth of just 2, but

both “g” and “h” will have call-stack depth of 3.
16. ### You’re doing great! Now let’s add in a fourth function

as part of the composition.
17. ### Seems like it’s going to become very cumbersome to keep

nesting calls to “composeTwo” as we add more steps to the composition.
18. ### Any ideas on how we could deﬁne a “compose” function

using “composeTwo”, but which can compose as many functions as we need?

21. ### Looping through the function arguments in reverse, and for each

iteration deﬁning a new “f” that is the composition of the previous “f” and the preceding function argument.
22. ### Instead of looping through the function arguments in reverse, could

you loop forward and still compose?

24. ### Wonderful. And just like before, what are the diﬀerences in

shape of these?
25. ### But now the call-stack depth is 4 for both “g”

and “h”, whereas it’s still just 2 for your “f”.
26. ### These loops you’ve written, they look like reductions to me.

Can you express them with reduce utilities?

31. ### Huh. Usually linear loop-based algorithms end up with lower call-stack

depth than recursive algorithms, but not here.
32. ### Any idea how we could make a deﬁnition for the

general “compose” that doesn’t have the higher call-stack depth?

34. ### I think the problem is that by using the “composeTwo”

function, we’re lazily deferring the actual function calls.
35. ### Instead, if we eagerly compute each intermediate composition result, and

then pass that onto the next, it should keep the max call-stack depth at 2.
36. ### OK, that sounds good. Can you do that with loops,

reductions, AND recursion?

38. ### The loop and reduction implementations are now call-stack depth of

2. But the recursion one is still call-stack depth of 4.
39. ### Perfect. Well, I have to say, you breezed through this

technical screen interview. We’ll be in touch!