AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |
Back to Blog
Scala for loop11/11/2022 It offer the ability to iterate over a collection. Here is the good explanation from Scala spec, that for-loop is just a corner case of for-comprehension. I prefer map/flatmap/filter as I feel it is simpler to comprehend. A For Comprehension is a very powerful control structure of Scala language. Technically there is no for-loop statement in Scala. The choice between latter two? I’d say it is your personal preference. Conclusionĭiscourage to use for-loop, encourage to use for-comprehension or map/flatmap/filter, if you want to write more functional style code. if you’re looking for some Scala for loop examples technically called a for-comprehension or for-expression I hope these examples are. This page is a work in progress, and as of tonight I haven’t tested some of the examples, but. It is a control structure in Scala, and in this article, well talk about the forms. Scala for loop code#My guess, for-comprehension is probably added intentionally to Scala as Easter egg to lure developers to transit from imperative looping to declarative looping (functional programing). Besides having a bad memory, I haven’t been able to work with Scala much recently, so I’ve been putting together this list of for loop examples. Scala for loop lets us execute specific code a certain number of times. Iterates the loop till it satisfies the condition. Flows in when the given condition is true else falls out. Checks whether the given condition is true or not. Photo by Önder Örtel on Unsplash for-loop and for-comprehension in Scala A simple for-loop: for (x List(5,6).map((x, _))).flatMap(_.map(x => x._1 + x._2))Īs you can see, all for-comprehension is just the syntactic sugar for map/flatmap/filter chaining functions. What is Scala While Loops Like any other programming language such as C, C++, C, Java, etc., while loop functions the same way by following the below protocols.
0 Comments
Read More
Leave a Reply. |