#多相な関数の定義から学ぶ、型クラスデザインパターン
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 |
import scala.concurrent._ | |
import ExecutionContext.Implicits.global | |
import android.util.Log | |
import android.widget.TextView | |
class UIDemoActivity extends Activity { | |
override def onCreate(bundle: Bundle) { | |
super.onCreate(bundle) | |
val tv = new TextView(this) |
```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 |
def process(command: String, keyPairFile: File, sshOption: String = "-o StrictHostKeyChecking=no -t -t"): Either[String, String] = { | |
import sys.process._ | |
try { | |
Right(s"echo ${command}" #&& "echo exit" #> s"ssh ${sshOption} -i ${keyPairFile.getAbsolutePath} ec2-user@${publicDN}" !!) | |
} catch { case e: Exception => Left(e.toString) } | |
} | |
def scp(file: File, keyPairFile: File, scpOption: String = "-o StrictHostKeyChecking=no"): Either[String, String] = { | |
import sys.process._ | |
try { |
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 |