Skip to content

Instantly share code, notes, and snippets.

@hisui
hisui / undefined.swift
Created September 21, 2014 00:07
undefined
func undefined<A>() -> A { return (nil as A?)! }
@hisui
hisui / TouchToEndEditView.swift
Last active February 21, 2020 07:33
[iOS] TouchToEndEditView
import UIKit
class TouchToEndEditView: UIView {
private var keyboardRect: CGRect?
override init(frame: CGRect) {
super.init(frame: frame)
setUp()
}
@hisui
hisui / .scala
Created September 27, 2014 02:40
[Swift] currying
('A' to 'F').foldLeft(Seq(Seq[Char]())) { (a, c) => a :+ (a.last :+ c)}.tail.foreach { a =>
val `A,` = a.mkString(", ")
val `->` = a.mkString(" -> ")
println(s"""
func curried<${`A,`}, Z>(fn: (${`A,`}) -> Z) -> (${`->`} -> Z) {
return ${ a.foldRight(s"fn(${`A,`})") { (c, a) => s"{ $c in $a }" }.toLowerCase }
}
""")
}
@hisui
hisui / hotp.scala
Last active August 29, 2015 14:11
HOTP
def HMAC_SHA1(key: Array[Byte], text: Array[Byte]): Array[Byte] = {
val mac = Mac.getInstance("HmacSHA1")
mac.init(new SecretKeySpec(key, "RAW"))
mac.doFinal(text)
}
def hton64(n: Long): Array[Byte] =
(0 to 7)
.map(i => (n >>> 8 * (7 - i)) & 0xff)
.map(_.toByte)
var pc = new webkitRTCPeerConnection({"iceServers": [{"url": "stun:stun.l.google.com:19302"}]});
pc.createDataChannel("hello");
pc.createOffer(function (desc){
pc.setLocalDescription(desc, function() {
console.log(desc.sdp);
});
});
/*
v=0
@hisui
hisui / type.swift
Created February 1, 2015 04:00
Type Comparison
// This returns something like an ID of given type
func unsafeGetTypeId(type: Any.Type) -> uintptr_t {
return unsafeBitCast(type, uintptr_t.self)
}
func isOfType(value: Any, type: Any.Type) -> Bool {
return unsafeGetTypeId(reflect(value).valueType) == unsafeGetTypeId(type)
}
let a: Any = "Hello"
@hisui
hisui / UD.swift
Last active August 29, 2015 14:14
import Foundation
public class UDKey<T> {
let key: String
let map: BiMap<AnyObject, T>
public init(_ key: String, _ map: BiMap<AnyObject, T>) {
self.key = key
self.map = map
@hisui
hisui / red_black_tree.prolog
Created February 24, 2015 05:57
Red-Black Tree with Zipper in Prolog
:- op(200, xfy, <--).
:- op(200, xfy, /^\).
:- op(200, xfy, /^ ).
:- op(200, xfy, ^\).
tree_empty(zip(nil, [])).
tree_lhs(zip(L, [step(lhs, V, R)|XS]), zip(node(V, L, R), XS)).
tree_rhs(zip(R, [step(rhs, V, L)|XS]), zip(node(V, L, R), XS)).
// Playground - noun: a place where people can play
import UIKit
class MyControl {
private var closures: [(UIControlEvents, MyControl -> ())] = []
init() {}
@hisui
hisui / so28789095.swift
Created March 10, 2015 07:19
Array#sortedGroupBy
struct Record {
let time: NSDate
let name: String
}
extension Array {
func sortedGroupBy(isOrderedBefore: (T, T) -> Bool) -> [[T]] {
let tmp = sorted(isOrderedBefore)
var out = [[T]]()
for (i, e) in enumerate(tmp) {