Skip to content

Instantly share code, notes, and snippets.

@ukitaka
ukitaka / Type check前のAST
Last active October 27, 2017 08:36
Opening existentials
(source_file
(protocol "Animal" requirement signature=<null>
(func_decl "bark()"
(parameter_list
(parameter "self"))
(parameter_list)))
(struct_decl "Dog" inherits: <null>
(func_decl "bark()"
(parameter_list
(parameter "self"))
@ukitaka
ukitaka / overload.swift
Created October 25, 2017 07:46
overload
protocol Animal {}
struct Dog: Animal {}
func hoge(_ any: Any) {
print("Any")
}
func hoge(_ dogOpt: Dog?) {
print("Optional<Dog>")
@ukitaka
ukitaka / take.rs
Created September 5, 2017 15:02
take.rs
#[test]
fn take_works_well() {
let a: &[i32] = &[1,2,3];
let t = a.take(2);
assert_eq!(t.len(), 2);
assert_eq!(t[0], 1);
assert_eq!(t[1], 2);
}
#[test]
@ukitaka
ukitaka / Decl.pu
Created August 1, 2017 02:04
AST.swift
@startuml
Decl <|-- IfConfigDecl
Decl <|-- ValueDecl
ValueDecl <|-- TypeDecl
TypeDecl <|-- GenericTypeDecl
GenericTypeDecl <|-- TypeAliasDecl
GenericTypeDecl <|-- NominalTypeDecl
NominalTypeDecl <|-- EnumDecl
NominalTypeDecl <|-- StructDecl
@ukitaka
ukitaka / LReader.swift
Created July 28, 2017 06:07
LReader (LIOのReader版)
struct LReader<S1, S2, E, A> {
let run: (E) -> A
init(_ run: @escaping (E) -> A) {
self.run = run
}
func map<B>(_ f: @escaping (A) -> B) -> LReader<S1, S2, E, B> {
return LReader<S1, S2, E, B> { env in f(self.run(env)) }
}
trait Functor[F[_]] {
def map[A, B](fa: F[A])(f: A => B): F[B]
}
trait Monad[F[_]] extends Functor[F] {
def apply[A](a: A): F[A]
def flatMap[A, B](fa: F[A])(f: A => F[B]): F[B]
def map[A, B](fa: F[A])(f: A => B): F[B] = flatMap(fa)(a => apply(f(a)))
}
let o = Observable.of(1,2,3)
.map { n -> Int in
print("map1 - \(n)")
return n
}
.asDriver(onErrorJustReturn: 0)
.map { n -> Int in
print("map2 - \(n)")
return n
}
trait From[T, S] {
def from(t: T): S
}
object From {
def from[T, S](t: T)(implicit F: From[T, S]): S = F.from(t)
}
implicit val fromIntToString = new From[Int, String] {
def from(i: Int): String = i.toString
@ukitaka
ukitaka / Union.scala
Created May 9, 2017 09:56
Union.scala
sealed trait Union[A, B]
case class Left[A, B](a: A) extends Union[A, B]
case class Right[A, B](b: B) extends Union[A, B]
type |[A, B] = Union[A, B]
implicit def liftLeft[A, B](a: A) = Left[A, B](a)
implicit def liftRight[A, B](b: B) = Right[A, B](b)
var intString: Int | String = 1
@ukitaka
ukitaka / InsertIfNotExists.scala
Created April 29, 2017 02:33
InsertIfNotExists.scala
trait InsertIfNotExists[T <: AbstractTable[_]] {
val driver: JdbcProfile
import driver.api._
val tableQuery: TableQuery[T]
def exists(e: T#TableElementType): Rep[Boolean]
def insertIfNotExists(e: T#TableElementType)(implicit executionContext: ExecutionContext): DBIO[Unit] = for {