This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| typealias JSON = [String:String] | |
| protocol Decoder { | |
| associatedtype T | |
| func decode(json: JSON) -> T | |
| } | |
| protocol Decodable { | |
| static func decode<D: Decoder>(json: JSON, decoder: D) -> D.T where D.T == Self | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func fix<T, U>(_ f: @escaping (@escaping (T) -> U) -> (T) -> U) -> (T) -> U | |
| { | |
| return { f(fix(f))($0) } | |
| } | |
| let fib: (Int) -> Int = fix { f in { n in if n < 2 { return 1 } else { return f(n - 1) + f(n - 2) } } } | |
| fib(1) | |
| fib(2) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public protocol SequenceProxy: Sequence { | |
| associatedtype Element | |
| var elements: [Element] { get } | |
| } | |
| public extension SequenceProxy { | |
| func makeIterator() -> IndexingIterator<Array<Element>> { | |
| return elements.makeIterator() | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| let a = true | |
| let b = false | |
| let c = true | |
| // MARK: - 2個のパターン | |
| // OK | |
| switch (a, b) { | |
| case (true, _): | |
| break |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def id[A](a: A): A = a | |
| val eDouble1: Double = id(2.123345) | |
| id(2) // Int | |
| val eDouble2: Double = id(2) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import RealmSwift | |
| import Result | |
| // MARK: - | |
| public protocol _Read {} | |
| public struct Read: _Read { } | |
| public protocol _Write {} | |
| public struct Write: _Write { } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| trait Applicative[F[_]] extends Functor[F] { | |
| // この2つが必要 | |
| def point[A](a: A): F[A] | |
| def ap[A, B](a: F[A])(f: F[A => B]): F[B] | |
| // pureはpointのalias? | |
| def pure[A](a: A): F[A] = point(a) | |
| // 以下はap/pointを使って実装できるもの | |
| def apF[A,B](f: => F[A => B]): F[A] => F[B] = ap(_)(f) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| enum Maybe<A> { | |
| case some(A) | |
| case none | |
| func map<B>(_ f: (A) -> B) -> Maybe<B> { | |
| switch self { | |
| case .some(let a): | |
| return .some(f(a)) | |
| case .none: | |
| return .none |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import scala.language.higherKinds | |
| trait InvariantFunctor[F[_]] { | |
| def xmap[A, B](fa: F[A])(f: A => B, g: B => A): F[B] | |
| } | |
| trait Functor[F[_]] extends InvariantFunctor[F] { | |
| def map[A, B](fa: F[A])(f: A => B): F[B] | |
| def xmap[A, B](fa: F[A])(f: A => B, g: B => A): F[B] = map(fa)(f) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| postfix operator % | |
| public postfix func %(int: Int) -> CGFloat { | |
| return CGFloat(int) / 100.0 | |
| } | |
| public postfix func %(int: Int8) -> CGFloat { | |
| return CGFloat(int) / 100.0 | |
| } |