Skip to content

Instantly share code, notes, and snippets.

@chemacortes
Last active February 17, 2022 15:55
Show Gist options
  • Save chemacortes/5f3c3e1ce2985011674b12cdde1f40df to your computer and use it in GitHub Desktop.
Save chemacortes/5f3c3e1ce2985011674b12cdde1f40df to your computer and use it in GitHub Desktop.
Scala parallel collections
#!/usr/bin/env -S scala-cli shebang
//> using scala "3.1.0"
//> using lib "org.scala-lang.modules::scala-parallel-collections:1.0.4"
import scala.collection.parallel.CollectionConverters._
def fact(n: BigInt) =
(n to 1 by -1).par.product
println(fact(500000))
/*
build.sbt:
libraryDependencies +=
"org.scala-lang.modules" %% "scala-parallel-collections" % "0.2.0"
*/
import scala.collection.parallel.CollectionConverters._
def fact(n: Int): BigInt = (n to 1 by -1).par.product
//> using scala 3.1.0
//> using lib org.scala-lang.modules::scala-parallel-collections:1.0.4
/*
Para ejecutar con scala-cli (https://scala-cli.virtuslab.org):
$ scala-cli fact_par.scala
*/
import scala.collection.parallel.CollectionConverters._
def fact(n: BigInt) = (n to 1 by -1).par.product
@main
def main = println(fact(500000))
#!/usr/bin/env -S scala-cli shebang
//> using scala 3.1.0
//> using lib org.scala-lang.modules::scala-parallel-collections:1.0.4
import scala.collection.parallel.immutable.ParSeq
def fact(n: BigInt): BigInt =
ParSeq.range(BigInt(1), n + 1).product
fact(500000).toString
.grouped(120)
.foreach(println)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment