2019-10-01から1ヶ月間の記事一覧

なんかおわってた、Atom + Ensime

atom + Ensime 環境構築 大本 https://www.ncaq.net/2018/05/18/16/15/50/ とか org.ensime:ensime_2.12:1.0.0 No fallback URL found not found → https://github.com/ensime/ensime-atom/issues/286

emap either

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

scalaz参考

scalaz参考 leanpub.com eed3si9n.com

Option ++: ++

[https://www.scala-lang.org/api/current/scala/Option.html#++:B%3E:A:CC[B]] そもそも++ ++:の違いもあんまわからんかったのでこの機会にメモ どうやら2.13.0からCanBuildFromがいなくなることに関係している?そこ起因かわからんが、、、 関係はありそう…

map入れ子

val fas: Future[Seq[A]] = ... fas.map(_.map(a => ...)) for { as <- fas } yield for { a <- as } a ... for { as <- fas } yield as map(a => ...)

DBIO, DBIOAction, headOption, emap

そもそもscalazとかモナドとか全く理解していないのでそのお勉強はまたこんどして、 ひとまず使用例だけメモっとく val action = for { hogeOpt <- hogeDao.hoget.map(_.headOption) //*1 hoge<- hogeOpt.point[DBIO] emap (_ \/> new Exception("ほげありま…

Future[Option]#map(_.getOrElse(DBIO.failed...

def getOptionHoge: Future[Option[Hoge]] ={...} for { hoge <- getOptionHoge.map(_.getOrElse(DBIO.failed( シグネチャ /** Returns the option's value if the option is nonempty, otherwise * return the result of evaluating `default`. * * @param …

MonadErrorSyntax_2

).fold[B](ifEmpty: => B)(f: A => B): B OptionT(...).getOrElseF(default: => F[A])(implicit F: Monad[F]): F[A] ) emap ( _ \/> new Exception("error!!") ) ↓ \/> final def \/>[E](e: => E): E \/ A = o.toRight(self)(e) def OptionInstances#toRight…

Option#foreach

Noneはなにもしないよ /** Apply the given procedure $f to the option's value, * if it is nonempty. Otherwise, do nothing. * * @param f the procedure to apply. * @see map * @see flatMap */ @inline final def foreach[U](f: A => U) { if (!isEmp…

MonadErrorSyntax

)) map { case 1 => () } recoverWith { case e => Future.failed(new Exception("error!!")) } )) emap (_ == 1 either () or new Exception("error!!")) warn: adaptation of argument list by inserting () is deprecated either () => either (()) final…

型パラメーターの記号みたいなやつら

こういうの [A <: B] [A >: B] [+T] [-T] 参考 第21章:Scalaの型パラメータ http://qiita.com/f81@github/items/7a664df8f4b87d86e5d8 共変・反変・上限境界・下限境界の関係性まとめ http://qiita.com/mtoyoshi/items/bd0ad545935225419327 型境界 [A <: B] サブクラス or 同一 [A >: B] スーパー </:></:>…

toString mkString

mkStringを使いましょう。 final class StringBuilder(private val underlying: JavaStringBuilder) ... /** Returns a new String representing the data in this sequence. * * @note because toString is inherited from AnyRef and used for * many purp…

implicit classes

https://docs.scala-lang.org/overviews/core/implicit-classes.html ここみればだいたい分かるが、、、

CanBuildFrom

TraversableLikeの暗黙パラメータにおったやつ こんなん //A -> a private implicit class Converter(l: Traversable[A]) { //https://gamaspecial.hatenablog.com/entry/2019/10/11/165653 def to: Seq[a] = l.map( r => a( value = r.value ) )(collection…

Unit

val u1 = () // => u1: Unit = () val u2 = Unit // => u2: Unit.type = object scala.Unit Unitは、Unitオブジェクトらしい

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…

toNel

List#toNel NotEmptyListを返す。 NonEmptyListを使えば空リストでは無いことが保証されるのでhead, tailが必ず成功する。 reduceLeft, reduceRightでも同様に確実に成功する。 val hoges = List(hoge1, hoge2) //def toNel: Option[NonEmptyList[A]] = l.to…