Skip to content

Instantly share code, notes, and snippets.

View hartbit's full-sized avatar
👨‍💻
Swifting, one commit at a time

David Hart hartbit

👨‍💻
Swifting, one commit at a time
  • Atipik Sàrl & Witty Wings SA
  • Geneva, Switzerland
View GitHub Profile
@hartbit
hartbit / reactive_perf.swift
Created February 9, 2016 14:24
Testing a performance scenario of many observers in different Reactive frameworks in Swift
//
// main.swift
// ReactivePerf
//
// Created by David Hart on 09/02/16.
// Copyright © 2016 David Hart. All rights reserved.
//
import Foundation
import enum Result.NoError
@hartbit
hartbit / gist:666421f6539416425558
Created October 13, 2015 08:30
inout vs UnsageMutablePointer
let closure1: (inout Int) -> () -> Void = { a in
return {
a = 1
return
}
}
let closure2: (UnsafeMutablePointer<Int>) -> () -> Void = { a in
return {
a.memory = 2
@hartbit
hartbit / self-closure-init.swift
Last active August 29, 2015 14:27
Using self in closure to super.init
class A<T> {
init(closure: (T -> Void) -> Void) {}
}
class B<T> : A<T> {
private var lastValue: T?
override init(closure: (T -> Void) -> Void) {
super.init(closure: { sink in
closure { value in
require 'rake'
task :works do
puts "works!"
end
class MyClass
include Rake::DSL
def self.define_task
@hartbit
hartbit / Publisher-Subscriber in C#
Last active August 29, 2015 14:24
Publisher/Subscriber Pattern from C# to Swift
using System.Collections.Generic;
interface IPublisher<T> {
void Subscribe(ISubscriber<T> subscriber);
}
interface ISubscriber<T> {
void Publish(T value);
}
//: Playground - noun: a place where people can play
import Foundation
protocol ObserverType {
typealias Element
func onNext(value: Self.Element)
func onError()
func onCompleted()
protocol ObserverType {
typealias Element
func onNext(value: Self.Element)
func onError()
func onCompleted()
}
protocol ObservableType {
func subscribe<T : ObserverType>(observer: T)
protocol ObserverType {
typealias Element
func onNext(value: Self.Element)
func onError()
func onCompleted()
}
protocol ObservableType {
typealias Observer : ObserverType
protocol Value: Equatable {
}
protocol Smashable {
typealias T: Value
func valueBySmashing​OtherValue​(value: T) -> T
}
struct Foo : Value, Smashable {
typealias T = Bar
@hartbit
hartbit / gist:22269b9fe700ff2de8ab
Created June 19, 2015 12:34
Cannot invoke 'assert' with an argument list of type '(Bool, String)'
extension UIView {
var firstSubview: UIView? {
if let firstSubview = self.subviews.first as? UIView {
assert(firstSubview != nil, "ASSERT COMPILATION ERROR")
return firstSubview
} else {
return nil
}
}
}