I was reading about fold techniques in Programming in Scala
book and came across this snippet:
def reverseLeft[T](xs:List[T]) = (List[T]() /: xs) {
(y,ys) => ys :: y
}
As you can see, it was done using foldLeft
or /:
operator. Curious how it would look like if I did it using :\
, I came up with this:
def reverseRight[T](xs:List[T]) = (xs :\ List[T]()) {
(y,ys) => ys ::: List(y)
}
As I understand it, :::
doesn't seem to be as fast as ::
and has a linear cost depending on the size of the operand list. Admittedly, I don't have a background in CS and no prior FP experience. So my questions are:
- How do you recognise/distinguish between foldLeft/foldRight in problem approaches?
- Is there a better way of doing this without using
:::
?