score:3
Accepted answer
For Scala 2.13
this would be the most general form.
import me.tongfei.progressbar.ProgressBar
def iterationModule[A](items: IterableOnce[A], f: A => Any): Unit = {
val (it, pb) =
if (items.knowSize != -1)
items.iterator -> new ProgressBar("Test", items.knowSize)
else {
val (iter1, iter2) = items.iterator.split
iter1 -> new ProgressBar("Test", iter2.size)
}
it.foreach { x =>
f(x)
pb.step()
}
}
Note: most of the changes are just to make the code more generic, but the general idea is just to create a function that wraps both the original function and the call to the ProgressBar.
Edit
A simplified solution for 2.11
def iterationModule[A](items: Seq[A], parallel: Boolean = false)
(f: A => Any): Unit = {
val pb = new ProgressBar("test", items.size)
val it = if (parallel) {
items.iterator.par
} else {
items.iterator
}
it.foreach { a =>
f(a)
pb.step()
}
}
Source: stackoverflow.com
Related Query
- Wrap higher order function into progress bar
- Function implicit parameters not any more so after passing it to a higher order function
- Implementing a higher order function that performs currying in scala
- ClassCastException in a higher order generic function
- Confusing Scala Higher order function call syntax
- Scala higher order function that filters even numbers
- Recursive higher order function type in Scala 3
- Tuple pattern matching on higher order function
- Scala Higher Order Function Little Confused
- how to calculate factorial of list using higher order function in scala
- Scala: Higher order function to return the union of sets
- Higher Order Function w/ Call By-Name?
- Wrap function implementations returning a specific type into another function programatically
- Passing a function with default parameter to a higher order function
- Type definitions and type mismatch in a higher order function
- Refactor to Higher order function in Scala?
- Scala - Perfect Number usage in higher order function - anything wrong?
- Scala: Converting for expression to higher order function
- Breakdown with existential type and higher order function
- Scala: Polymorphism in argument type of higher order function
- How are Function Values processed in Higher Order Methods without definition in Scala?
- Error for parentheses in higher order function definitions (Scala)
- Higher Order Function with Case Class Method
- Higher order function in scala
- Scala For loop to Higher Order Function
- Build generic reusable iteration module from higher order function
- How should I write this higher order function where the parameter func needs an implicit view?
- How to use @unchecked for an Option in a higher order function
- Higher Order Function with `Val` and `Def`
- Scala: 'class needs to be abstract' when implementing higher order function
More Query from same tag
- Creating a time-based chunking Enumeratee
- remove NULL columns in Spark SQL
- reading spatial data in spark 2 with megallen
- Spark History Server on S3A FileSystem: ClassNotFoundException
- Implement Scala-style String Interpolation In Scala
- Scala case class "explicitly exposing the state"
- Non-determinism of synchronous execution contexts (a.k.a. `parasitic`)
- yield a tuple in a for loop scala
- how to recognise specific text with selenium?
- In scala, why doesn't lower bound work well here?
- Where does set come from in "val Singleton4 : Set = set => set == 4"?
- Ref Updates And Fiber Triggers Using Cats Effect
- Spark Dataframe to Dataset of Java class
- Tableau Extract API return "Server did not call back us"
- Why doesn't Option have a fold method?
- 'not found' error but the function is defined
- how to validate connection strings of azure iot hub in spark streaming?
- Scala's "clean up" of type erasure
- For loop Spark dataframe
- Scala: Implicit Conversion From Generic Type to Second Generic Type
- Speeding up a method that iterates through text and creates a Map[Tuple2[String, String], Int] Scala
- How do I get hold of exceptions thrown in a Scala Future?
- Scala/Akka/Protobuf: Failed to serialize and deserialize message
- Split string by "|~|" in scala
- Spark calculate the percentage of appearances of words
- Scala can't find case class parameter in generic trait method
- Counting the number of threads in an ExecutionContext
- scala typesafe config - How to load conf file from classpath not from top level resources
- Future sql execution behavior while database connection is unstable
- How is scalaz able to do "A \/ B", and how can I do my own "B.??" or "A <??> B"