scala

Try( 分岐

? Try(...) match { case Success(v) => Some(v) case Failure(e) => None } Try(new URL(url)).fold( e => None, v => Some(v) ) Try(new URL(url)).toEither.left.map { e => None }.toOption

くそコード

await(try Future.failed(new Exception("error")) catch { case e: Throwable => Future.successful(()) }) must equalTo(()) 結果 [error] java.lang.Exception: error はいくそ

Option#contains

だまされそうになった object Option { ... /** Tests whether the option contains a given value as an element. * * @example {{{ * // Returns true because Some instance contains string "something" which equals "something". * Some("something") …

val (t1, t2): (Rep[A], Rep[Option[B]]) ... if ((t2.map(_.B.isDefined) && t1.A < t2.flatMap(_.B)) == LiteralColumn[Boolean](true)) { t2.flatMap(_.B).get } else { t1.A } ↓ val (t1, t2): (Rep[A], Rep[Option[B]]) ... t2.flatMap(_.B).fold(t1.A)…

import scalaz.syntax.std.string._ ... val l: String = ... val v = l.parseLong // Validation[scala.NumberFormatException, Long] fold v.fold(f, s) ... /** Catamorphism. Run the first given function if failure, otherwise, the second given fun…

slick gropBy をめもる

/** Partition this query into a query of pairs of a key and a nested query * containing the elements for the key, according to some discriminator * function. */ def groupBy[K, T, G, P](f: E => K)(implicit kshape: Shape[_

void method は mockしなくていい?してはいけない?

stackoverflow.com

object は mock できない

tech.aainc.co.jp

// 1 _ <- "A" match { case "A" => case _ => ... // 2 _ <- "A" match { case "A" => "a" case "B" => "b" case "C"が追加された場合、//2ではビルド検知できる

}.runFold(Seq.empty[String])((acc, x) => x +: acc).map(_.sortBy(/** sort **/)) }.runWith(Sink.seq).map(_.sortBy(/** sort **/))

ExecutionContext

環境 Scala 2.12.7 ExcetionContextって もろもろの暗黙パラメータとして渡されるやつで、非同期を良しなにしてくれるやつ ....

PartialFunction

specs2のAnyMatchers#beLikeを使ってみようと思ったらPartialFunctionなるものがでてきたので調べた /** matches if the value returns a successful result when applied to a PartialFunction */ def beLike[T](pattern: PartialFunction[T, MatchResult[_…

beLike

/** matches if the value returns a successful result when applied to a PartialFunction */ def beLike[T](pattern: PartialFunction[T, MatchResult[_]]) = new Matcher[T] { def apply[S

calss A @Inject() ( val hoge: Hoge ) extends B { ... } trait B { val hoge: Hoge ... }

https://www.playframework.com/documentation/ja/2.2.x/ScalaJsonCombinators

implicit

Tech Tips: Scala: implicit の使い方

class, abstract class, trait, object

class abstract trait object 抽象メソッド x o o x インスタンス o x x o コンストラクタ引数 o o x x 継承の集約 サブクラスは1個、traitはいっぱい //1こめはtraitでもextends trait ATrait class AClass extends ATrait abstract class と taritの使い…

yield for / OptionT#run

yield for のリファクタメモしとく ついでにOptionTうんぬん、 // action: OptionT[DBIO, DBIOAction[Int, ... val action = for { i <- OptionT(DBIO.successful(Option(1))) } yield for { ii <- DBIO.successful(i) } yield ii // DBIO[Option[DBIO[Int..…

Future[Option[Future

val fof = Future[Option[Future[Int]]] val fo1 = fof.flatMap({ case None => Future.successful(None) case Some(a) => a.map(Some(_)) }) val fo2 = fof.flatMap(opt => Future.sequence(opt.toSeq).map(_.headOption)) // Option拡張されんやつ fof.fla…

Traitを

Scalaトレイト メモ(Hishidama's Scala trait Memo) Class,Abstract Class,Trait,Objectの違いまとめ - うなの日記

ops.collect

しあわせになれる @inline final def collect[B](pf: PartialFunction[A, B]): Option[B] = if (!isEmpty) pf.lift(this.get) else None

emap either

hoge().emap(r => /** 条件 **/ either /** 結果 **/ or /** 結果 **/) hoge() flatMap { r => if /** 条件 **/ Future.successful(/** 結果 **/) else Future.failed(/** 結果 **/)}

Trait

先ほど例で使用したfunc変数は「(Int,Int) => Int」という型を宣言していましたが、次のように記述しても同様の関数オブジェクトが定義可能です。 ``` scala> val func:Function2[Int,Int,Int] = (x:Int, y:Int) => x + yfunc: (Int, Int) => Int = <function2> ```この</function2>…

型パラメータ

型パラメータ def post: Action[Request] = Action.async(DtoParser.parse[Request]) { implicit request => final def async[A](bodyParser: BodyParser[A])(block: R[A] => Future[Result]): Action[A] = composeAction(new Action[A] { def executionCont…