Skip to content

Instantly share code, notes, and snippets.

@mzaks
mzaks / debounce.swift
Last active March 4, 2019 09:47
Swift debounce function based GCD
import Foundation
/**
Creates and returns a new debounced version of the passed block which will postpone its execution until after wait seconds have elapsed since the last time it was invoked.
It is like a bouncer at a discotheque. He will act only after you shut up for some time.
This technique is important if you have action wich should fire on update, however the updates are to frequent.
Inspired by debounce function from underscore.js ( http://underscorejs.org/#debounce )
*/
public func dispatch_debounce_block(wait : NSTimeInterval, queue : dispatch_queue_t = dispatch_get_main_queue(), block : dispatch_block_t) -> dispatch_block_t {
private protocol List{
var tail : List {get}
}
private struct EmptyList : List{
var tail : List {
return EmptyList()
}
}
enum EnumList<T> {
case Cons(T, EnumList<T>)
case Nil
}
let list = 1 => 2 => 3 => nil
println("\(list.value)") //1
for value in list {
println(value) // 1 2 3
}
let l1 = list[0]
let l2 = list[5]
let l3 = list[2]
func measureArray(){
let time = NSDate()
var a = [0]
for i in 1...1_000{
a.append(i)
}
let after = NSDate()
println("\(after.timeIntervalSince1970 - time.timeIntervalSince1970) Array 1K")
}
class Box<T>{
let value : T
init(_ value : T ){
self.value = value
}
}
enum EList<T> {
case Cons(Box<T>, Box<EList>) // Box the value T to work around a runtime deadlock bug in Swift
case Nil
func measureList(){
let time = NSDate()
var a = 0 => nil
for i in 1...1_000{
a = i => a
}
let after = NSDate()
println("\(after.timeIntervalSince1970 - time.timeIntervalSince1970) List 1K")
}
typealias TPerson = (name:String, age:Int, male:Bool)
struct SPerson{
let name : String
let age : Int
let male : Bool
}
enum EPerson {
case Male(name: String, age : Int)
func measureTuple(){
var name : String = ""
let time = NSDate()
for i in 1...10_000_000{
let p = TPerson("some", 25, true)
name = p.name
}
let after = NSDate()
println("\(after.timeIntervalSince1970 - time.timeIntervalSince1970) Tuple 10M")
println(name)
@mzaks
mzaks / gist:02b0c482509fed565ab8
Created June 1, 2015 14:10
Doodling a Entity system based prog language
context main
component Position {
x : Int,
y : Int
}
component Velocity {
x : Int,
y : Int