I hereby claim:
- I am dennislysenko on github.
- I am lysenko (https://keybase.io/lysenko) on keybase.
- I have a public key ASDsv4zgEBA5cq7_3BJnk-AqdpyZGlXfyyd-HBsoEpdGxQo
To claim this, I am signing this object:
import UIKit | |
public extension UIView { | |
public var width: CGFloat { | |
set { | |
bounds.size.width = newValue | |
} | |
get { | |
return bounds.size.width | |
} |
// call like: asyncify(runRequest)(arg) { success in ... } | |
func asyncify<P, R>(_ function: @escaping (P) throws -> (R)) -> ((P, _ completion: @escaping ((Result<R>) -> Void)) -> Void) { | |
return { (arg: P, completion: @escaping (Result<R>) -> Void) in | |
DispatchQueue.global(qos: .background).async { | |
do { | |
let result = try function(arg) | |
DispatchQueue.main.async { | |
completion(.success(result)) | |
} | |
} catch let error { |
function fixBoxes() { | |
// fixes punctuation in a string: mixed LTR/RTL text causes punctuation at the end to appear at the beginning of the string | |
function fix(str) { | |
var match = str.match(/^[\!\?"]+/); // find !, ?, and " at the beginning of a string | |
if (match) { | |
return str.substring(match[0].length) + match[0]; // and move it to the end | |
} | |
return str; | |
} | |
// | |
// SimpleMediaSaver.swift | |
// | |
// Created by Dennis Lysenko on 27-09-16. | |
// Copyright © 2016 Dennis Lysenko. All rights reserved. | |
// https://gist.github.com/dennislysenko/5388cacb83b754e8983e99bff7fef2d2 | |
// | |
// This gist is licensed under the terms of the MIT license. | |
// |
// Profile your code when you're too lazy to use Instruments | |
// Method one: get the time to execute a block | |
func getTimeToExecute(block: () throws -> Void) rethrows -> NSTimeInterval { | |
let start = NSDate().timeIntervalSince1970 | |
try block() | |
return NSDate().timeIntervalSince1970 - start | |
} | |
// example: |
I hereby claim:
To claim this, I am signing this object:
// Setup: We want to arm a NuclearWarhead and get a LaunchCode from it but there's a 1/10 chance of a PrematureDetonationError | |
// (nb: if Swift had any kind of useful random generator that I could pick up in 5 seconds I would use that instead of this pseudorandom-at-best algorithm) | |
struct NuclearWarhead {} | |
struct LaunchCode {} | |
struct PrematureDetonationError: ErrorType {} | |
func armNuclearWarhead(warhead: NuclearWarhead) throws -> LaunchCode { | |
let currentTimestamp = NSDate().timeIntervalSince1970 | |
let lastDigit = currentTimestamp % 10 | |
if lastDigit == 8 { |
I hereby claim:
To claim this, I am signing this object:
// Fuzzy string-matching algorithm | |
// Implementation of algorithm described at: http://blog.notdot.net/2007/4/Damn-Cool-Algorithms-Part-1-BK-Trees | |
// Essentially builds an index of strings by levenshtein distance (N-ary tree) on which you can run range queries. | |
// The root node can be chosen arbitrarily. Each node holds a string and a collection of edges, representing distance to other strings, which then have their own children and so on, building a complete index. | |
// See https://github.com/vy/bk-tree for (impressive) performance statistics despite this tending to create an unbalanced N-ary tree | |
class BKTreeNode { | |
var value: String | |
var edges: Dictionary<Int, BKTreeNode> |