Skip to content

Instantly share code, notes, and snippets.

View blixt's full-sized avatar
🚧
???

Blixt blixt

🚧
???
View GitHub Profile
extension OrderedDictionary: DictionaryLiteralConvertible {
init(dictionaryLiteral elements: (Key, Value)...) {
for (key, value) in elements {
self[key] = value
}
}
}
@blixt
blixt / NSDataExtension.swift
Created November 14, 2015 23:38
NSData hex extension
extension NSData {
var hex: String {
let pointer = UnsafePointer<UInt8>(self.bytes)
var hex = ""
for i in 0..<self.length {
hex += String(format: "%02x", pointer[i])
}
return hex
}
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
guard #available(iOS 9.0, *) else {
return
}
// MAX FORCE!
guard let maxForce = touches.maxElement({ $0.force < $1.force })?.force where maxForce > 3 else {
return
}
// Forceful touching is going on.
}
@blixt
blixt / HexToUIColor.swift
Last active July 28, 2021 05:19
A Swift String extension that converts a hexadecimal color to a UIColor.
import UIKit
extension String {
var hexColor: UIColor? {
let hex = self.stringByTrimmingCharactersInSet(NSCharacterSet.alphanumericCharacterSet().invertedSet)
var int = UInt32()
guard NSScanner(string: hex).scanHexInt(&int) else {
return nil
}
let a, r, g, b: UInt32
@blixt
blixt / formatting.js
Created September 27, 2015 15:36
Formatting templating strings in ES6
function format(strings, ...values) {
const pieces = [];
for (let [index, string] of strings.entries()) {
let value = values[index];
const formatIndex = string.lastIndexOf('$');
if (formatIndex != -1) {
const format = string.substr(formatIndex + 1);
string = string.substr(0, formatIndex);
// TODO: Use an actual format engine here.
@blixt
blixt / Event.swift
Last active January 19, 2023 19:42
A simple Event class for handling events in Swift without strong retain cycles.
import Foundation
private class Invoker<EventData> {
weak var listener: AnyObject?
let closure: (EventData) -> Bool
init<Listener : AnyObject>(listener: Listener, method: (Listener) -> (EventData) -> Void) {
self.listener = listener
self.closure = {
[weak listener] (data: EventData) in
// Update: See a solution here:
// https://gist.github.com/blixt/08434b74f0c043f83fcb
// (NSMapTable holds onto the weak keys longer than I expected)
import Foundation
private class ClosureWrapper<EventData> {
typealias EventHandler = (EventData) -> Void
let closure: EventHandler
init(_ closure: EventHandler) {
@blixt
blixt / OrderedDictionary.swift
Created August 19, 2015 23:45
Somewhat complete ordered dictionary implementation
struct OrderedDictionary<Key : Hashable, Value> {
typealias Element = (key: Key, value: Value)
private var dictionary = [Key: Value]()
private var orderedKeys = [Key]()
var count: Int {
return self.orderedKeys.count
}
class ListDiff<T : Hashable> {
typealias IndexList = [Int]
typealias FromIndexToIndexList = [(from: Int, to: Int)]
// A list of indexes of where items were added.
let added: IndexList
// A list of indexes where items that have disappeared used to be.
let deleted: IndexList
// A list of tuples of indexes representing the old and new indexes of items that moved.
let moved: FromIndexToIndexList
@blixt
blixt / arbit.txt
Created June 29, 2015 18:32
Dieharder output
#=============================================================================#
# dieharder version 3.31.1 Copyright 2003 Robert G. Brown #
#=============================================================================#
rng_name | filename |rands/second|
file_input| dieharder.txt| 4.31e+06 |
#=============================================================================#
test_name |ntup| tsamples |psamples| p-value |Assessment
#=============================================================================#
diehard_birthdays| 0| 100| 100|0.15936729| PASSED
diehard_operm5| 0| 1000000| 100|0.95725635| PASSED