Is there a function in scala or scalaz that applies the function to the list if the list is not empty. Otherwise, it returns some default value. The function should apply to the list itself not to the list items. That is, it does the following:
implicit class RichList[A, M[A] <: Iterable[A]](list: M[A]) { def convertOrElse[B](fn: M[A] => B, whenEmpty: B) = { if (!list.isEmpty) fn(list) else whenEmpty } }
Example usage: mylist.convertOrElse ("prefix" + _.mkString (","), "")
I remember seeing it somewhere, but maybe it was on a blog or some such. At the moment I canβt find it in the scalar, but I canβt even find the latest documentation on the scalaz on the Internet (this is another problem :)
I wrote my own implicit, as you can see, but I do not want to add my own implications when the corresponding import of the skalaz will do the trick.
tksfz source share