Skip to content

Instantly share code, notes, and snippets.

@ukitaka
ukitaka / InvariantFunctor.scala
Created February 10, 2017 02:02
InvariantFunctor
// see: http://comonad.com/reader/2008/rotten-bananas/
trait InvariantFunctor[F[_]] {
def xmap[A, B](fa: F[A])(f: A => B, g: B => A): F[B]
}
implicit val optionalInvariantFunctor = new InvariantFunctor[Option] {
def xmap[A, B](fa: Option[A])(f: A => B, g: B => A): Option[B] = fa match {
case Some(a) => Some(f(a))
case _ => None
}
@ukitaka
ukitaka / convert_to_srgb.md
Created February 9, 2017 10:13
Storyboardとxibのcolor spaceをsRGBに統一する

calibratedRGB → sRGB

% find . -name "*.storyboard" | xargs sed -i "" '-e s/colorSpace="calibratedRGB"/colorSpace="custom" customColorSpace="sRGB"/g'
% find . -name "*.xib" | xargs sed -i "" '-e s/colorSpace="calibratedRGB"/colorSpace="custom" customColorSpace="sRGB"/g'

calibratedWhite → sRGB

@ukitaka
ukitaka / ComposeFunctor.scala
Created February 9, 2017 02:27
Functorの合成
import scala.language.higherKinds
trait Functor[F[+_]] {
def map[A, B](a: F[A])(f: A => B): F[B]
}
implicit val optionalFunctor = new Functor[Option] {
def map[A, B](a: Option[A])(f: A => B): Option[B] = a.map(f)
}
@ukitaka
ukitaka / LazyRecoverable.swift
Last active February 8, 2017 02:10
Recoverable
func const<A, B>(_ a: A) -> (B) -> A {
return { _ in a }
}
struct LazyRecoverable<A> {
private let original: A
private var updatef: ((A) -> A)?
private var mutableCopy: A
init(_ original: A) {
@ukitaka
ukitaka / utf8mb4.swift
Last active February 6, 2017 08:10
utf8mb4
extension UTF8 {
static func byteCount(_ input: UnicodeScalar) -> Int {
var count = 0
encode(input, into: { _ in count = count + 1 })
return count
}
static func is4Bytes(_ input: UnicodeScalar) -> Bool {
return byteCount(input) == 4
}
@ukitaka
ukitaka / KeyPath.swift
Created February 3, 2017 02:37
ExpressibleByStringLiteral
struct KeyPath {
let key: String
init(_ key: String) {
self.key = key
}
}
extension KeyPath: ExpressibleByStringLiteral {
@ukitaka
ukitaka / Lazy.swift
Created January 25, 2017 10:17
Lazy
class Lazy<A> {
private let a: () -> A
private lazy var lazyA: A = {
return self.a()
}()
var value: A {
return lazyA
}
@ukitaka
ukitaka / Tuple.swift
Created January 24, 2017 05:51
Tuple has same named label
typealias MyTuple = (hoge: Int, hoge: String) // 同じ名前のラベルが置ける
let myTuple = MyTuple(hoge: 1, hoge: "Hello")
print(myTuple.hoge) // 1
func stringFunc(text: String) { }
// NG: 型推論はできない
// stringFunc(text: myTuple.hoge)
@ukitaka
ukitaka / PhantomType.swift
Last active January 19, 2017 09:45
PhantomTypeをSwiftでもなんとかして使いたい
protocol LoginState { }
final class Login: LoginState { }
final class Logout: LoginState { }
class LoginStateAction<State>: LoginStateActionType { }
protocol LoginStateActionType {
func loginState() -> LoginStateAction<Login>?
func logoutState() -> LoginStateAction<Logout>?
}
@ukitaka
ukitaka / TypeConforming.swift
Last active January 10, 2017 09:19
型制約をつけると具体型が必要になる.swift
protocol Creature { }
protocol Animal: Creature { }
struct Dog: Animal { }
struct Cat: Animal { }
// OK
class Hoge<A> { }
let hoge = Hoge<Animal>() // OK