This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
import XCPlayground | |
// 500-1000, 1-100 | |
func seq(length: Int, sampleRate: Int) -> [Int] { | |
var s = [Int]() | |
for i in 0..<length { | |
s.append((i * sampleRate).nonzeroBitCount) | |
} | |
return s |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func euclid(steps: Int, pulses: Int) -> [Int] { | |
let pulses = min(pulses, steps) | |
let ones = pulses | |
let zeros = steps - pulses | |
if zeros == 0 { | |
return [Int](repeating: 1, count: steps) | |
} | |
var s: [[Int]] = [Int].init(repeating: 1, count: ones).map({ [$0] }) + [Int](repeating: 0, count: zeros).map({ [$0] }) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
var data: [String: Any] = [ | |
"connection": [ | |
"wifi": [ | |
"sx": 1.1, | |
"tx": 11 | |
], | |
"cell": [ | |
"rx": 10, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
find . -name '.DS_Store' -type f -delete |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
protocol DictionaryStore { | |
static var storeFileName: String { get } | |
var store: [[String: Any]] { get set } | |
} | |
extension DictionaryStore { | |
func write() throws { | |
let data = try JSONSerialization.data(withJSONObject: store, options: []) | |
let document = try FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false) | |
let path = document.appendingPathComponent(Self.storeFileName) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// Merges right hand side dictionary into left hand side dictionary. Works on nested dictionaries as well. | |
/// | |
/// - Parameters: | |
/// - lhs: Dictionary you want to merge someting. | |
/// - rhs: Merging dictionary. | |
/// - Returns: Returns merged dictionary. | |
internal func +<Key, Value> (lhs: [Key: Value], rhs: [Key: Value]) -> [Key: Value] { | |
var result = lhs | |
rhs.forEach { | |
if let dict = $1 as? [Key: Value] { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
extension UIViewController { | |
var topMostChild: UIViewController? { | |
if let tab = self as? UITabBarController { | |
return tab.selectedViewController?.topMostChild | |
} else if let nav = self as? UINavigationController { | |
return nav.topViewController?.topMostChild | |
} else if let split = self as? UISplitViewController { | |
return split.viewControllers.last?.topMostChild |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
extension Array { | |
/// An array subscript extension that returns the element from the positive or negative circular index. | |
public subscript(circular index: Int) -> Element? { | |
guard count > 0 else { return nil } | |
let mod = index % count | |
let offset = index >= 0 ? 0 : count | |
let idx = mod == 0 ? 0 : mod + offset | |
return self[idx] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var timebaseInfo = mach_timebase_info_data_t() | |
init() { | |
mach_timebase_info(&timebaseInfo) | |
} | |
func machAbsoluteToSeconds(machAbsolute: UInt64 = mach_absolute_time()) -> Double { | |
let nanos = Double(machAbsolute * UInt64(timebaseInfo.numer)) / Double(timebaseInfo.denom) | |
return nanos / 1.0e9; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
func convert<T: FloatingPoint>(value: T, inRange: ClosedRange<T>, toRange: ClosedRange<T>) -> T { | |
let oldRange = inRange.upperBound - inRange.lowerBound | |
let newRange = toRange.upperBound - toRange.lowerBound | |
return (((value - inRange.lowerBound) * newRange) / oldRange) + toRange.lowerBound | |
} | |
func convert<T: SignedInteger>(value: T, inRange: ClosedRange<T>, toRange: ClosedRange<T>) -> T { | |
let oldRange = inRange.upperBound - inRange.lowerBound |