#多相な関数の定義から学ぶ、型クラスデザインパターン
twitter: @OE_uia github: taisukeoe
##多相な関数
- 複数の異なる型に対して適用可能な関数
- どの型に対して定義されているか、静的に決定できる(定義されてない型の値を渡すとコンパイルエラーになる)
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) | |
} | |
} |
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 |
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 |
```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._ |
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 |
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] |
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 |
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{ |
jmhSettings |