by roflc0ptic · 6y ago · 0 comments
In the interview, they asked me, "What's the difference between fold left and fold right?" I said "Um, one of them starts on the left side of the data structure, one of them starts on the right. I never remember which is which." They said essentially: "okay. The answer I was looking for was that fold right is not stack safe." I protested weakly, and they dug in, so I graciously let it go.
But it's patently not true. A naive implementation of fold right in Scala isn't stack safe, but the actual implementation is stack safe. It's been stack safe since like 2010. So rather than interrogating my knowledge of functional programming, they're just asking me bullshit language trivia, and dinging me when I don't come up with their arbitrary wrong answer.
I wanted to work there before that, but the interview made me a little ambivalent, and then they just ghosted me. So I guess I dodged a bullet. But man, that was frustrating.
I’ve come to rationalize this as a risk-avoidance tactic, and I think the negative effects of reputations damage to the company are mostly unaccounted for.
It was a cheap way of figuring out I shouldn’t even consider working there.
I mean - all you're experiencing is the HR/recruitment side of the pipeline at that point. You can't really say that an entire company is shit just because some subset of its HR/recruiters do shitty things. How many CTO's actually give a shit about IC interviewing experience and dig into the entire methodology that recruiters use when interacting with candidates? (And actually keep track of what their recruiters do) I'm gonna give a wild guess and put 0 out there.
At a high level foldLeft and foldRight represent abstraction leakage. The Domain and Codomain for both functions are exactly the same. If there is no performance difference or side effects to consider then there is almost no point in having two fold functions as a plain fold has the exact same inputs and outputs as foldLeft or foldRight.
Typically for side effects you don't want to put it in a fold as you're integrating way too much IO with your functional computations. Rather functional languages tend to have something along the lines of "foreach" for using side effects on the content in a functor. That being said it's strange that scala has a foldLeft and a foldRight with identical performance characteristics. Maybe it's a legacy thing. I dunno, not a scala guy.
Just bringing that up. I still agree with you that the interview question sucked and that the interviewer was wrong. Personally I wouldn't have ever figured out that was what the interviewer was looking for even when I do possess the requisite knowledge.
This is not true.
> def List[A].foldLeft[B](z: B)(op: (B, A) => B): B
> def List[A].foldRight[B](z: B)(op: (A, B) => B): B
Notice the signature of the fold op: the arguments types are swapped. This is because fold left and right on a list [a, b], say, is the difference between:
(z op a) op b
and
a op (b op z)
(If this isn't compelling enough, consider [a, b, c].) Not all functions are associative. For example, consider a cryptographic hash function.
If you're saying that foldRight in scala exists aesthetic reasons... well can't argue with that. I figured it was more for legacy reasons as the original poster said that there use to be a performance difference.
As for the associative thing I mentioned side effects. Function composition is always associative. Unless you have side effects. See: https://www.wikiwand.com/en/Function_composition (search for associative)
I don't know much about crypto but I'm assuming you're referring to things that aren't side effect free. In general though a hash function should be associative under function composition as it is just a surjective mapping from domain to codomain.
Also can't exactly read that syntax nor the stuff below it. Not a scala dude. Maybe a more traditional map implementation in like python or JS pseudo code would make more sense to me, but that may be too much to ask.
edit:
I think you actually may be right. We're both a little wrong with our reasoning though. Function composition is not commutative. And FoldRight and FoldLeft reverses the order of composition which has an effect on operations that are not commutative.
// this makes a linked list ten million and one elements long, from zero to ten million
val longList = (0 to 10000000).toList
// this adds the elements
longList.foldRight(0)({ case (l, r) => l + r})
This does not stack overflow.
You write:
>> a plain fold has the exact same inputs and outputs as foldLeft or foldRight
but if you use fold over a collection of type T with the fold function in Scala, the result must be of type T. Fold left and fold right don't have this constraint. Further, this fold is only coherent if the operation is commutative, because it doesn't happen in any particular order. You can add integers in any order to make the same sum, but you can't add pages of a PDF in any order to make a PDF.
Framed via recursion schemes, I'm pretty sure foldLeft can be viewed as a catamorphism where foldRight is an anamorphism. You're consuming the tree from different directions. They may have the same domain and codomain (I'm a little hazy on that) but they're not apparently the same function. y = 2x and y = 3x both have the same domain and codomain, right? But the difference between them isn't aesthetic.
You're wrong about the operations possibly not being commutative; as you pointed out, whether the parameter list is (A, B) or (B, A) doesn't really matter, so it doesn't pose a problem.
An example is the subtraction operator: with just two elements, I can change `(a, b) => b - a` to `(b, a) => b - a` with with no difference in result. But there is no way I can write a subtraction function for foldLeft that gives the same results as a foldRight on subtraction in general. That is, I can write op such that a op b = b - a, but not that (a op b) op c = (c - b) - a. Nevertheless, I can still write op such that a op (b op c) = (c - b) - a due to some dual notion of commutativity.
You're still confused. There is no function composition here.
op in the example above is just some other function, like +. The associativity of + and function composition are true for totally unrelated reasons. Associativity of plus is an inductive argument that follows from the Peano axioms.
Function composition says:
(f o g) o h = f o (g o h)
as functions. It is true because unary function application "serializes" function applications. Formally, I mean:
((f o g) o h)(x)
= f(g(h(x)))
= (f o (g o h))(x)
Function composition has one value flowing through several functions. Fold has several values flowing through one function.They created a puzzle where you needed to ask for more information. Intentionally or not, they were discovering if they could communicate with you. That the communication failed doesn't mean your somehow less then them. It means you just have a different background.
Now to the problem, the problem people don't set good goals. It probably doesn't make sense to hire a carbon copy of yourself.