score:55
You can use the zipWithIndex
from Iterable trait:
for ((line, i) <- Source.fromFile(args(0)).getLines().zipWithIndex) {
println(i, line)
}
score:10
As already answered by others, if you want your index to start from 0, you can use zipWithIndex
:
for ((elem, i) <- collection.zipWithIndex) {
println(i, elem)
}
Because zipWithIndex
creates a copy of the collection if called on the collection itself, you may want to call it to a view
of the colleciton instead: collection.view.zipWithIndex
.
Nonetheless, Python's enumerate
has an optional parameter to set the start value of your index. In scala, you can do:
for ((elem, i) <- collection.zip(Stream from 1) {
println(i, elem)
}
For a longer discussion, read https://alvinalexander.com/scala/how-to-use-zipwithindex-create-for-loop-counters-scala-cookbook.
Source: stackoverflow.com
Related Query
- Is there a Scala equivalent for the python enumerate?
- Is there a Scala equivalent of the Python list unpack (a.k.a. "*") operator?
- What is the equivalent Scala for the following Python statement?
- How to get the Scala equivalent code for the Python code
- Is there a Python equivalent for Scala's Option or Either?
- Is there a way to handle the last case differently in a Scala for loop?
- Is there a tool for Scala to clean all the unused imports from all the code files?
- Is there a ruby equivalent to the Scala Option?
- What's the idiomatic equivalent in Scala for C#'s event
- Scala or Java equivalent of Ruby factory_girl or Python factory_boy (convenient factory pattern for unit testing)
- Is there an analog in Scala for the Rails "returning" method?
- Is there in scala the equivalent of the c# nameof?
- Scala 2.12: What is the equivalent of Java 8 method reference for universally quantified SAM trait?
- Is there an overview of the nsc compiler API for Scala 2.11?
- Is there an inject equivalent for Haskell in the context of free monads
- Is there an equivalent of Python's difflib.SequenceMatcher for Scala
- Are there any python or scala tools to connect the spark/shark
- What is the equivalent scala code to this python code: "%i" % float(9.04E09)
- Is there any way to describe the type in scala 2 for case classes companion-objects of particular type?
- Is there any way for ficus lib to inject in scala case class either Long or String depending on the value in configuration
- What is the equivalent of GeoPy for Scala or Java?
- What is the Java-compatibile equivalent in Scala for <? extends Foo>
- Is there a way to force waiting for the end of a java process to end in Scala or Spark?
- scala - equivalent case class for the text parsing that has dynamic number of fields
- Is there a way for a Scala base class to get informed when all the derived classes have been constructed? (guess not)
- Is there a way in scala for printing the size of a list beetween a function chaining?
- What is the equivalent log4j statement in scala for the below java code
- Is there a Java / Scala implementation for entering Hbse Puts that actually shows the result of the transaction
- Scala [2.11.6] -- is there an elegant way to create cache-keys for objects based on an Int/Long and the full class name?
- What will be the scala equivalent of the following python code?
More Query from same tag
- Scala - type erasure?
- Error "scala.reflect.internal.MissingRequirementError: object scala.runtime in compiler mirror not found" when compiling Play framework tests
- How to execute system commands in scala
- Scala - MaxBins error - Decision Tree - Categorical variables
- Scala: Regex that matches everything up to a certain character
- Handling messages with actors
- Cant find the ID mapping using json4s
- Unable to use a wildcard in map
- Scala: reflection with different argument types
- Moving an element to the front of a list in Scala
- How can we write to a Kafka topic and read from a kafka topic locally from IDE
- closing file pointer in Scala in Finally
- How to specify complex form validations in Play 2?
- Scala iterate over map and turn singleton List into just the singleton
- How to sequence these future functions in reactivemongo?
- Inserting 0 for an auto-incrementing column in mysql
- Scala, Specs2, Mockito and null return values
- Creating rdbms DDL from scala classes
- shapeless port to scala-js: create artifact with few external dependencies
- Getting location of cross-project resources
- Is there a Scala like Clojurescript? aka Integrated Scala Single Page Application
- Connecting Slick 3.2.1 to MySQL using slick.jdbc.MySQLProfile
- Spark as execution engine with Hive
- Type safe String interpolation in Scala
- Scala syntax to access property of an option inline and chain "OrElse"?
- How to compose routes in actor's receive with runRoute?
- Akka Play testing with @NamedCache injections
- Scala: Affine Transformations
- Error defining empty node in tree data structure in scala
- How can I keep a multi-threaded Java/Scala application running?