Skip to content

Instantly share code, notes, and snippets.

View hiroshi-maybe's full-sized avatar

Hiroshi Kori hiroshi-maybe

  • San Jose, CA, United States
View GitHub Profile
@hiroshi-maybe
hiroshi-maybe / mabe-monad.swift
Last active August 29, 2015 14:24
Maybe monad written in Swift
// from Swiftz
public class K1<A> { public init() {} }
protocol Monad {
typealias A
typealias B
typealias FB = K1<B>
func bind(A -> FB) -> FB;
static func ret(A) -> Self
}
@hiroshi-maybe
hiroshi-maybe / capture_test.swift
Created July 22, 2015 22:03
Swift research about capture in closure
var x: X? = X()
// leaks if closure1 is called, and it does not capture self by [unowned self]
//x!.closure1()
// if capture self, crashes because x is deallocated befor closure is called.
x!.method()
x = nil
extension NSTimer {
@hiroshi-maybe
hiroshi-maybe / eq.swift
Last active August 29, 2015 14:25
Infinite recursive call in Swift 2.0
protocol Eq {
typealias A = Self
func equalsTo(a: A) -> Bool
func notEqualsTo(a: A) -> Bool
}
extension Eq {
func equalsTo(a: A) -> Bool { return !self.notEqualsTo(a) }
func notEqualsTo(a: A) -> Bool { return self.equalsTo(a) }
}
@hiroshi-maybe
hiroshi-maybe / eq.hs
Last active August 29, 2015 14:25
Type class default implementation
class MyEq a where
equalsTo :: a -> a -> Bool
equalsTo x y = not (notEqualsTo x y)
notEqualsTo :: a -> a -> Bool
notEqualsTo x y = not (equalsTo x y)
instance MyEq Integer where
equalsTo x y = x == y
main = do putStrLn (show $ notEqualsTo (1::Integer) (2::Integer))
@hiroshi-maybe
hiroshi-maybe / StructVsClassTests.swift
Last active August 29, 2015 14:25
Swift 2.0 benchmark
import XCTest
//@testable import Playground
class StructVsClassTests: XCTestCase {
let times = 1000000
// 0.023 sec
// 0.002 sec on Release build
func testPerformanceStructConstructor() {
// This is an example of a performance test case.
@hiroshi-maybe
hiroshi-maybe / protocolExtension.swift
Created August 28, 2015 08:14
example of protocol extension
//: Playground - noun: a place where people can play
import UIKit
var str = "Hello, playground"
/*
class (Eq a, Show a) => Num a where
(+) :: a -> a -> a
@hiroshi-maybe
hiroshi-maybe / stream.swift
Created November 28, 2015 21:49
observable implementation in Swift
import Foundation
// Light-weight utility of observable stream
// https://gist.github.com/staltz/868e7e9bc2a7b8c1f754
class Observer<T> {
typealias EventHandler = T -> ()
let eventHandler: EventHandler
init(eventHandler: EventHandler) {
self.eventHandler = eventHandler
}
@hiroshi-maybe
hiroshi-maybe / crash.swift
Last active March 25, 2016 08:08
Swift compiler crash in Xcode 7.1
// 1. same name of functions with different signature in both protocol and protocol extension
// 2. someone is conforming to the protocol
// This should be compile error though, error does not happen. Instead Compiler crashes in AST -> SIL conversion phase.
protocol FooProtocol {
func sameName(differentSignature: Bool) -> UITableViewCell?
}
extension FooProtocol {
func sameName() -> UITableViewCell {
return UITableViewCell()
@hiroshi-maybe
hiroshi-maybe / tag-parser.swift
Created December 24, 2015 10:01
Simple flat tag parser
class Tag {
let tagName: NSString
let element: NSString
init(tagName: NSString, element: NSString) {
self.tagName = tagName
self.element = element
}
static let open: NSString = "<"
@hiroshi-maybe
hiroshi-maybe / typealias-compile-error.swift
Last active December 27, 2015 23:32
Swift compile error in Xcode 7.1
protocol MyProtocol: class {
typealias MyAlias
var something: MyAlias { get }
}
extension MyProtocol where MyAlias: YourProtocol {
func bar() {
// !!! Compile Error happens !!!
// expected an argument list of type '(Self.AliasTypeAlias)'
// self.something.foo { _ in print("X") }