Skip to content

Instantly share code, notes, and snippets.

View eonist's full-sized avatar
🎯
Focusing

André J eonist

🎯
Focusing
View GitHub Profile
@eonist
eonist / ProtocolAssociatedType.swift
Created March 27, 2017 17:47 — forked from troystribling/ProtocolAssociatedType.swift
A swift protocol with associated type used as type parameter in generic function
protocol Thing {
typealias argType
func doit(val:argType) -> argType
}
class IntThing : Thing {
func doit(val: Int) -> Int {
return val + 1
}
}
import Cocoa
// Only needed until we have class variables
var __SwizzleSayHello = { (who:String) -> String in
return "Hello, \(who)"
}
class Swizzle {
//Only needed until we have class variables
class var _sayHello : (String)->String { get{ return __SwizzleSayHello } set (swizzle) {__SwizzleSayHello = swizzle} }
@eonist
eonist / TypeErasure.swift
Created March 11, 2017 10:48 — forked from russbishop/TypeErasure.swift
Type erasure with multiple adopting types
// Paste me into a playground!
import Cocoa
//: # Basic Setup
protocol FancyProtocol {
associatedtype Thing
func holdPinkyUp(x: Thing)
}
@eonist
eonist / diff.mdown
Last active March 5, 2017 09:49 — forked from ndarville/diff.mdown
Paul Heckel's Diff Algorithm

oliveratkinson [12:50 AM] Paul Heckel's Diff Algorithm

[12:50]
http://dl.acm.org/citation.cfm?id=359467

eonist [12:53 AM] Instagram iOS dev interview question: How would you explain the “Paul Heckel's Diff Algorithm” to a child? go...

oliveratkinson [12:54 AM]

@eonist
eonist / String+HTML.swift
Created March 2, 2017 22:10 — forked from ollieatkinson/String+HTML.swift
Unescape HTML entities in Swift 3 == £ -> £
extension String {
func unescapeHTMLEntities() throws -> String {
guard contains("&#") else {
return self
}
guard let data = data(using: .utf8) else {
return self
function flatten(arr) {
return arr.reduce((acc, val) => acc.concat(Array.isArray(val) ? flatten(val) : val), []);
}
@eonist
eonist / URLRequest+cURLCommand.swift
Created March 2, 2017 21:19 — forked from ollieatkinson/URLRequest+cURLCommand.swift
Generate cURL command's from URLRequest's
extension URLRequest {
/// The cURL representation of the URLRequest, useful for debugging and executing requests outside of the app.
var cURLCommand: String {
var command = "curl"
if let httpMethod = httpMethod {
command.append(commandLineArgument: "-X \(httpMethod)")
}
@eonist
eonist / gist:a21a491496409ed3e203d3f47fdbe35c
Last active February 25, 2017 11:25 — forked from mickmaccallum/gist:c2b322e059c9b3379245
Swift recursive flatmap (Alternative version)
protocol AnyArray{}/*<--Neat trick to assert if a value is an Array, use-full in reflection and when the value is Any but really an array*/
extension Array:AnyArray{}//Maybe rename to ArrayType
func recFlatMap<T>(_ arr:[AnyObject]) -> [T]{
var result:[T] = []
Swift.print("arr.count: " + "\(arr.count)")
arr.forEach{
if($0 is AnyArray){
let a:[AnyObject] = $0 as! [AnyObject]
result += recFlatMap(a)
}else{
@eonist
eonist / playground.swift
Last active January 18, 2017 13:09 — forked from h2ero/playground.swift
InteractivePlayground with button that prints hello (swift 3.0.1)
import AppKit
import XCPlayground
import Cocoa
import PlaygroundSupport
extension NSLayoutConstraint {
convenience init(view item: NSView, to toItem: NSView, attribute: NSLayoutAttribute, constant c: CGFloat = 0) {
self.init(item: item, attribute: attribute,
relatedBy: .equal,
toItem: toItem, attribute: attribute,
@eonist
eonist / touch.swift
Last active September 5, 2017 22:25 — forked from erica/touch.swift
Swift 3 update (playground oriented programming)
import Cocoa
import XCPlayground
import PlaygroundSupport
class TouchView: NSView {
var (path, currentPath) = (NSBezierPath(), NSBezierPath())
override func draw(_ dirtyRect: NSRect) {
guard let contextPtr = NSGraphicsContext.current()?.graphicsPort else {return}
let context = unsafeBitCast(contextPtr, to: CGContext.self)