This file contains 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
// main.rs | |
#[actix_web::main] | |
async fn main() -> std::io::Result<()> { | |
HttpServer::new(move || { | |
App::new() | |
.service(web::resource("/queue/{id}").route(web::get().to(queue_handler::handle))) | |
}).bind("0.0.0.0:8080")?.run().await | |
} | |
// queue_handler.rs |
This file contains 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
fn parse_to_f64(s: &str) -> f64 { | |
s.parse::<f64>().unwrap() | |
} | |
let result = std::panic::catch_unwind(|| { | |
println!("{}", parse_to_f64("12.34")); | |
}); | |
assert!(result.is_ok()); | |
let result = std::panic::catch_unwind(|| { | |
println!("{}", parse_to_f64("abcdef")); |
This file contains 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
fn square(s: &str) -> Result<f64, ParseFloatError> { | |
s.parse::<f64>().map (|i| i * i) | |
} | |
fn calculate(x: &str, y: &str) -> Result<f64, ParseFloatError> { | |
let x2 = square(x)?; | |
let y2 = square(y)?; | |
Ok((x2 + y2).sqrt()) | |
} |
This file contains 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 square(s: String): Try[Double] = Try { | |
val i = s.toDouble | |
i * i | |
} | |
def calculate(x: String, y: String): Try[Double] = { | |
for { | |
x <- square(x) | |
y <- square(y) | |
} yield Math.sqrt(x + y) |
This file contains 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
func square(s string) (float64, error) { | |
i, e := strconv.ParseFloat(s, 64) | |
return i * i, e | |
} | |
func calculate(x string, y string) (*float64, error) { | |
x2, e := square(x) | |
if e != nil { | |
return nil, e | |
} | |
y2, e := square(y) |
This file contains 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
pub fn unwrap(self) -> T { | |
match self { | |
Ok(t) => t, | |
Err(e) => unwrap_failed("called `Result::unwrap()` on an `Err` value", &e), | |
} |
This file contains 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
pub const fn unwrap(self) -> T { | |
match self { | |
Some(val) => val, | |
None => panic!("called `Option::unwrap()` on a `None` value"), | |
} | |
} | |
pub fn expect(self, msg: &str) -> T { | |
match self { | |
Some(val) => val, | |
None => expect_failed(msg), |
This file contains 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
data class Currency(val value: BigDecimal, val code: String) { | |
constructor(v: Int, code: String): this(BigDecimal(v), code) | |
fun convert(toCode: String): Currency { | |
return Currency(convert(this, toCode), toCode) | |
} | |
private fun convert(fromCurrency: Currency, toCode: String): BigDecimal { | |
//1 USD = 1.27082 CAD | |
return if (fromCurrency.code == "USD" && toCode == "CAD") { | |
fromCurrency.value * BigDecimal(1.27) | |
} else if (fromCurrency.code == "CAD" && toCode == "USD") { |
This file contains 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
object BadBidiFlow extends App { | |
implicit val system = ActorSystem() | |
implicit val ec = system.dispatcher | |
implicit val materializer = ActorMaterializer() | |
val source = Source.fromIterator(() => Seq("1", "2a", "3").toIterator) | |
val sink = Sink.foreach(println) | |
val endFlow = Flow.fromFunction[String, (String, Try[Int])]{ a => (a, Try(a.toInt)) } |
This file contains 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 java.time.LocalDate | |
import java.time.format.DateTimeFormatter | |
import java.time.temporal.TemporalAccessor | |
import org.scalameter.{Key, Warmer, _} | |
import scala.annotation.tailrec | |
import scala.util.{Failure, Success, Try} | |
/** |
NewerOlder