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 / UIDemoActivity.scala
Last active April 16, 2017 05:01
How to force to execute Future callback function in Android UIThread.
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)
@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 / gist:6343272
Last active December 21, 2015 17:49
awscala.ec2.Instanceに、単純なssh/scpをscala-ssh非依存で実行させてみるてすつ。
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 {
@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