" , " , , , . , , , "", . :
@tailrec
def update(arr:List[Char], replace:Char, replacement:Char, result:List[Char] = Nil):List[Char] = arr match {
case `replace` :: tail =>
update(tail, replace, replacement, replacement :: result)
case _ => result.reverse ::: arr
}
( List , ) replace char replacement.
, :
col.map { x => update(x, '.', ch) }
- mapUntil , ( Scalaz, , - ). , , :
def mapUntil[T](input:List[T])(f:(T => Option[T])) = {
@tailrec
def inner(xs:List[T], result:List[T]):List[T] = xs match {
case Nil => Nil
case head :: tail => f(head) match {
case None => (head :: result).reverse ::: tail
case Some(x) => inner(tail, x :: result)
}
}
inner(input, Nil)
}
, map, , , None, .
mapUntil(List(1,2,3,4)) {
case x if x >= 3 => None
case x => Some(x-1)
}
List[Int] = List(0, 1, 3, 4)
Scalaz, .