Skip to content

Instantly share code, notes, and snippets.

View taisukeoe's full-sized avatar

Taisuke Oe taisukeoe

  • Japan Scala Association, Inc.
  • Tokyo
  • X @OE_uia
View GitHub Profile
@taisukeoe
taisukeoe / MyErrors.scala
Last active August 19, 2019 18:05
MonadErrorのコレクションに対し、Error値を蓄積しながらsequenceする(Applicative合成する)方法
import cats.
import cats.implicits._
case class MyError(errorMsg: String)
object MyError {
implicit val s: Semigroup[MyError] = new cats.kernel.Semigroup[MyError] {
override def combine(t1: MyError, t2: MyError): MyError = MyError(t1.errorMsg + "\n" + t2.errorMsg)
}
}
@taisukeoe
taisukeoe / readerT_sample.scala
Last active July 25, 2019 09:41
Reader[Future[X]]をうまく扱うためには、ReaderTでいいと思うよ (248回 rpscalaのフォローアップ)
import cats._
import cats.data.{Kleisli, Reader, ReaderT}
import cats.implicits._
import scala.concurrent.{ExecutionContext, Future}
object ReaderStyle extends App {
def someCalc(i: Int): Int = ???
import ExecutionContext.Implicits.global
@taisukeoe
taisukeoe / ax_2.12.scala
Last active February 9, 2019 20:29
ax:A#X = a.x
Welcome to Scala 2.12.8 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_201).
Type in expressions for evaluation. Or try :help.
scala> trait A {val x:AnyRef = null;type X = x.type}
defined trait A
scala> val a = new A{}
a: A = $anon$1@36417a54
scala> val ax:a.type#X = a.x
@taisukeoe
taisukeoe / 201607.md
Last active July 20, 2016 13:03
多相な関数の定義から学ぶ、型クラスデザインパターン

#多相な関数の定義から学ぶ、型クラスデザインパターン

         twitter: @OE_uia github: taisukeoe

##多相な関数

  • 複数の異なる型に対して適用可能な関数
  • どの型に対して定義されているか、静的に決定できる(定義されてない型の値を渡すとコンパイルエラーになる)
@taisukeoe
taisukeoe / cofreeEqual.scala
Last active May 11, 2016 03:29
CofreeEqual with Implicit Function Types?
```scala
implicit def cofreeEqual[A, F[_]](implicit A: Equal[A], F: Equal[F[Cofree[F, A]]]): Equal[Cofree[F, A]] =
Equal.equal{ (a, b) =>
A.equal(a.head, b.head) && F.equal(a.tail, b.tail)
}
scala> import scalaz._,Scalaz._
import scalaz._
import Scalaz._
@taisukeoe
taisukeoe / log.txt
Created January 12, 2016 18:09
ImageIO is not found in Android
01-13 02:59:43.556 21402-21402/com.example.android.displayingbitmaps E/AndroidRuntime: FATAL EXCEPTION: main
01-13 02:59:43.556 21402-21402/com.example.android.displayingbitmaps E/AndroidRuntime: Process: com.example.android.displayingbitmaps, PID: 21402
01-13 02:59:43.556 21402-21402/com.example.android.displayingbitmaps E/AndroidRuntime: java.lang.NoClassDefFoundError: Failed resolution of: Ljavax/imageio/ImageIO;
01-13 02:59:43.556 21402-21402/com.example.android.displayingbitmaps E/AndroidRuntime: at org.canova.image.recordreader.BaseImageRecordReader.<clinit>(BaseImageRecordReader.java:72)
01-13 02:59:43.556 21402-21402/com.example.android.displayingbitmaps E/AndroidRuntime: at com.example.android.displayingbitmaps.ui.ImageGridActivity.trainMLP(ImageGridActivity.java:99)
01-13 02:59:43.556 21402-21402/com.example.android.displayingbitmaps E/AndroidRuntime: at com.example.android.displayingbitmaps.ui.ImageGridActivity.onCreate(ImageGridActivity.java:72)
01-13 02:59:43.556 21402-21402/com.examp
@taisukeoe
taisukeoe / projection.scala
Last active September 2, 2015 05:09
Row wise operations got weired
scala> val ndArray = (1d to 8d by 1).mkNDArray(Array(2,2,2),NDOrdering.C)
ndArray: org.nd4j.linalg.api.ndarray.INDArray =
[[[1.00,2.00]
[3.00,4.00]]
[[5.00,6.00]
[7.00,8.00]]]
scala> ndArray.sliceP.map(s => s.rowP.map(r => r*r))
res12: org.nd4j.linalg.api.ndarray.INDArray =
[[[1.00,2.00]
@taisukeoe
taisukeoe / scalaz-streams-Task-es.scala
Last active August 29, 2015 14:26
scalaz-streamsのTask.startは、既存のTaskの実行Threadも変更できそう
import streams._
import util._
implicit val s = Strategy.fromExecutionContext(scala.concurrent.ExecutionContext.Implicits.global)
val t = Task(println(Thread.currentThread.getName))
// Exiting paste mode, now interpreting.
import streams._
import util._
s: streams.util.Strategy = streams.util.Strategy$$anon$1@60d8175
@taisukeoe
taisukeoe / callback2func.scala
Last active August 29, 2015 14:25
Break down Callback which has two ore more functions into the same number of PartialFunction
trait Callback{
def onSet(s:Set[String]):Unit
def onList(l:List[String]):Unit
}
def funcWithCallback(s:String,callback:Callback):Unit = ???
def funcAsFuture(s:String):Future[Traversable[String]] = {
val p = Promise[Traversable[String]]()
val f = p.future
funcWithCallback(s,new Callback{
@taisukeoe
taisukeoe / build.sbt
Created April 20, 2015 14:21
FizzBuzzのbench
jmhSettings