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
start1 = time.time() | |
ids = [(i, rbusy.remote(1.0), time.time() - start1) for i in range(5)] | |
end1 = time.time() - start1 | |
print('duration1: %6.3f MILLISECONDS' % (1000*end1)) | |
for i,id,t in list(ids): | |
print('%d: %s (time: %6.3f MILLISECONDS)' % (i, id, 1000*t)) | |
start2 = time.time() | |
for i,id,t in list(ids): | |
print('%d: %s (time: %6.3f MILLISECONDS)' % (i, ray.get(id), 1000*(time.time() - start2))) |
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
// With braces | |
trait Monoid2[A] { | |
def add(a1: A, a2: A): A | |
def zero: A | |
} | |
// Without braces | |
trait Monoid3[A]: | |
def add(a1: A, a2: A): A | |
def zero: A |
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
def m2(s: String): String = { | |
val result = s.toUpperCase | |
println(s"output: $result") | |
result | |
} | |
def m3(s: String): String = | |
val result = s.toUpperCase | |
println(s"output: $result") | |
result |
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
val o2: Option[Int] => Int = { | |
case Some(i) => i | |
case None => 0 | |
} | |
val o3: Option[Int] => Int = | |
case Some(i) => i | |
case None => 0 |
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
0 match { | |
case 0 => "zero" | |
case _ => "other value" | |
} | |
0 match | |
case 0 => "zero" | |
case _ => "other value" |
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
import scala.annotation.tailrec | |
@tailrec def loop(whileTrue: => Boolean)(f: => Unit): Unit = | |
f | |
if (whileTrue) loop(whileTrue)(f) | |
var i=5 | |
loop(i > 0) { | |
println(i) | |
i -= 1 |
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
for (i <- 0 until 5) println(i) // Original syntax | |
for i <- 0 until 5 do println(i) // New syntax | |
for i <- 0 until 5 yield 2*i | |
for i <- 0 until 10 | |
if i%2 == 0 | |
ii = 2*i | |
yield ii | |
val i = 10 | |
if (i < 10) println("yes") // Original syntax |
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
implicit final class ArrowAssoc[A](private val self: A) extends AnyVal { | |
@inline def -> [B](y: B): (A, B) = (self, y) | |
@deprecated("Use `->` instead...", "2.13.0") | |
def →[B](y: B): (A, B) = ->(y) | |
} |
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
// From https://github.com/deanwampler/programming-scala-book-code-examples/ | |
import scala.annotation.targetName | |
extension [A,B] (a: A) | |
@targetName("arrow2") def ~>(b: B): (A, B) = (a, b) | |
// Alternative, where the [B] goes on the method instead. Support for this alternative was | |
// introduced in Scala 3.0.0-RC1. This form is closer to what we're used to doing with | |
// normal parameterized types and methods that take additional type parameters. the type | |
// signature for the two ~> methods will be slightly different, but they work the same. |
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
scala> import scala.language.implicitConversions | |
scala> case class Dollars(amount: Double): | |
| override def toString = f"$$$amount%.2f" | |
| case class Percentage(amount: Double): | |
| override def toString = f"${(amount*100.0)}%.2f%%" | |
| case class Salary(gross: Dollars, taxes: Percentage): | |
| def net: Dollars = Dollars(gross.amount * (1.0 - taxes.amount)) | |
// defined case class Dollars | |
// defined case class Percentage | |
// defined case class Salary |