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 / getNearbyWords.swift
Created August 5, 2016 06:09
getNearbyWords.swift
//: Playground - noun: a place where people can play
import UIKit
// https://www.facebook.com/video.php?v=10152735777427200&set=vb.9445547199&type=2&theater
var str = "Hello, playground"
func getNearbyChars(ch: String) -> Set<String> {
switch ch {
case "g": return Set(["g","h","f"])
//
// LazyStreamTests.swift
// Playground
//
// Created by kori on 4/20/16.
// Copyright © 2016 hiroshi.kori. All rights reserved.
//
import Foundation
-- Beautiful fibonacci implementation
fibs = 0 : 1 : zipWith (+) fibs (tail fibs)
@hiroshi-maybe
hiroshi-maybe / TCOTests.swift
Created April 21, 2016 06:45
Tail Call Optimization test
//
// TCOTests.swift
// Playground
//
// Created by kori on 4/20/16.
// Copyright © 2016 hiroshi.kori. All rights reserved.
//
import XCTest
@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") }
@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 / 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 / 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 / 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 / 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.