Last time we dealt with the processing of optional values, it turned out that using the elegant means of the Scala library incorrectly, we can continue to shoot our legs.filter and map , this can be the limit, because with a certain imagination one can implement a lot of algorithms only on these functions. However, these methods may not be optimal or produce results that are impossible for the domain, which, however, will have to be processed.for(;;) . Someone, especially those who wrote in C, thinks that without such a cycle you cannot write programs at all, the more advanced ones try to write analogs for while with the removal of the initial conditions and the cycle step into external code, for example: var i = 0 //  var! while(i < n) { //  - ,   i i = i+1 } for(val i = 0; i< n; i++) is for(i <- 0 until n) ,java.sql.ResultSet ( Enumeration also rolls). It is not Iterable, that is, there is no transparent conversion to JavaConversions , and you have to work with it only in imperative form or copy it into the contents into an intermediate mutable collection, alternatively, build immutable through the builder. Fearfully.Iterator from a ResultSet , which is at least Traversable : val resultSetIterator = Iterator.continually(resultSet).takeWhile(_.next()) exists , exists , forall and find . I often watched: option.map(myValueIsGoodEnough).getOrElse(false)  option.exists(goodEnoughValue) //   `option exists goodEnoughValue`  iterable.filter(goodEnough).headOption  iterable.find(goodEnough)  iterable.foldLeft(true)(_ && goodEnough)  iterable.forall(goodEnough) map , filter and foldLeft will check the entire collection, no matter how long it is, while exists , find and forall will end as soon as they find a suitable (or inappropriate) element and will not generate an additional intermediate collection.span , dropWhile and takeWhile . Using them may also not be obvious in practice, because the filter works successfully for these cases, too, really less efficiently, because it does not know that, at some point, it is no longer necessary to check the elements: they all fit or don’t fit. For example, I myself until recently, removed the beginning and end of a sequence of exchange rates that are not included in the interval, through filter , and not through dropWhile and takeWhile .fold , foldLeft and foldRight very powerful and they can be used to solve completely different tasks.foldLeft , with its help you can get the sum of the elements of the collection: iterable.foldLeft(0) { (accum, elem) => accum + elem }  iterable.foldLeft(0) { _ + _ }  (0 /: iterable)(_ + _) Numeric , you can write more simply: iterable.sum min and max functions are also available (special craftsmen like to sort the collection and take the head / tail, well, we haven't seen anything like this).for(;;) and play with indices, for(i <- 1 until seq.size) { result ++= combine(seq(i-1), seq(i)) }  seq.zip(seq.drop(1)).map(combine) //    list.zip(list.tail).map(combine) // ,      seq.sliding(2).map(calculateDelta) 
Source: https://habr.com/ru/post/184310/
All Articles