Last active
February 17, 2022 15:55
-
-
Save chemacortes/5f3c3e1ce2985011674b12cdde1f40df to your computer and use it in GitHub Desktop.
Scala parallel collections
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//> 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)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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