Skip to content

Instantly share code, notes, and snippets.

View dennislysenko's full-sized avatar

Dennis Lysenko dennislysenko

View GitHub Profile
@dennislysenko
dennislysenko / FixMemrise.js
Last active September 30, 2017 18:34
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.
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 / Asyncify.swift
Created March 27, 2018 19:03
Convenience methods for sync-to-async conversion in Swift with a Result<T> monad that represents a failable result.
// 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 / UIKit+Convenience.swift
Created March 27, 2018 19:04
Tiny set of helpers to make autoresizing-mask-based programmatic UI prototyping more palatable.
import UIKit
public extension UIView {
public var width: CGFloat {
set {
bounds.size.width = newValue
}
get {
return bounds.size.width
}