Skip to content

Instantly share code, notes, and snippets.

Avatar

Dennis Lysenko dennislysenko

View GitHub Profile
@dennislysenko
dennislysenko / UIKit+Convenience.swift
Created Mar 27, 2018
Tiny set of helpers to make autoresizing-mask-based programmatic UI prototyping more palatable.
View UIKit+Convenience.swift
import UIKit
public extension UIView {
public var width: CGFloat {
set {
bounds.size.width = newValue
}
get {
return bounds.size.width
}
@dennislysenko
dennislysenko / Asyncify.swift
Created Mar 27, 2018
Convenience methods for sync-to-async conversion in Swift with a Result<T> monad that represents a failable result.
View Asyncify.swift
// 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 {
@dennislysenko
dennislysenko / FixMemrise.js
Last active Sep 30, 2017
Fix memrise for right-to-left languages (developed during an intense Hebrew study session in a café playing Sigur Rós). Userscript - load into Panda Styler or another such extension.
View FixMemrise.js
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;
}
@dennislysenko
dennislysenko / SimpleMediaSaver.swift
Last active Dec 10, 2019
Simplifies saving media on iOS to an app-specific collection in the Photo Library
View SimpleMediaSaver.swift
//
// 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.
//
@dennislysenko
dennislysenko / QuickProfiling.swift
Created Feb 24, 2016
Profile your Swift code when you're too lazy to use Instruments
View QuickProfiling.swift
// 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:
View keybase.md

Keybase proof

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:

View PromisesPlus.swift
// 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 {
@dennislysenko
dennislysenko / SocialAccount.swift
Created Oct 20, 2015
Genome+MappableCoreDataObject Example
View SocialAccount.swift
//
// SocialAccount.swift
// Riff
//
// Created by Dennis Lysenko on 10/19/15.
// Copyright © 2015 Riff Digital. All rights reserved.
//
import Foundation
import CoreData
View keybase.md

Keybase proof

I hereby claim:

  • I am dslysenko on github.
  • I am lysenko (https://keybase.io/lysenko) on keybase.
  • I have a public key whose fingerprint is 4087 C50D 1032 765B 843B 1FDF F6EA 64B9 5E00 ED5C

To claim this, I am signing this object:

@dennislysenko
dennislysenko / BKTree.swift
Created Jun 11, 2015
Fuzzy String Matching (BK Tree)
View BKTree.swift
// 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>
You can’t perform that action at this time.