Skip to content

Instantly share code, notes, and snippets.

View mwrites's full-sized avatar
🔧

Supermercat mwrites

🔧
View GitHub Profile
@mwrites
mwrites / ModifyingObject.swift
Created March 6, 2018 07:42
ModifyingObject
//https://github.com/apple/swift-evolution/blob/master/proposals/0176-enforce-exclusive-access-to-memory.md
func modifying<T>(_ value: inout T, _ function: (inout T) -> ()) {
function(&value)
}
modifying(&object.pair) { pair in swap(&pair.x, &pair.y) }
@mwrites
mwrites / BaseToDecimal.swift
Last active March 5, 2018 09:07
Convert Binary, Base X, to Decimal
func baseToDecimal(_ base: Int, notation : [Int]) -> Int {
return notation.reversed().enumerated().filter { $0.element == 1 }.reduce(into: 0) {
let powR = Int(pow(Float(base), Float($1.offset)))
$0 += powR
}
}
func decimal(_ d : Int, toBase base: Int) -> [Int] {
var res = [Int]()
@mwrites
mwrites / FloatingPointIsInt.swift
Created March 4, 2018 14:13
Check if Float is Int
extension FloatingPoint {
var isInt: Bool {
#if swift(>=4.0)
return self.truncatingRemainder(dividingBy: 1) == 0
#else
return self % 1 == 0
#endif
}
}
@mwrites
mwrites / ParallelEnumeration.swift
Created February 28, 2018 08:55
Enumerating Two Collections In Parallel
for (a, b) in zip(A.enumerated(), B.enumerated()) {
print("a[\(a.offset)]: \(a.element)")
print("b[\(b.offset)]: \(b.element)")
}
@mwrites
mwrites / CustomCollectionDescription.Swift
Created February 28, 2018 08:52
CustomCollection Description
public struct Queue<T> : CustomStringConvertible where T : CustomStringConvertible {
fileprivate var array = [T]()
public var description: String {
var desc: String = ""
desc = array.reduce(into: desc) {
$0 += $1.description + "\n"
}
return desc
}
@mwrites
mwrites / StringCompat.swift
Last active February 27, 2018 10:21
StringCompat
extension String {
extension String {
func toArray() -> [Character] {
return Array(characters)
}
#if swift(>=4)
#else
var count: Int {
@mwrites
mwrites / In_your_networking_class.swift
Created February 1, 2018 07:36 — forked from giulio92/In_your_networking_class.swift
NSURLSession Proxy configuration
let sessionConfiguration: URLSessionConfiguration = .default
let proxyConfiguration: [AnyHashable : Any] = [
kCFNetworkProxiesHTTPEnable as AnyHashable: true,
kCFNetworkProxiesHTTPPort as AnyHashable: [Place-proxy's-port-number-here],
kCFNetworkProxiesHTTPProxy as AnyHashable: "Place-your-proxy-address-here"
]
sessionConfiguration.connectionProxyDictionary = proxyConfiguration
@mwrites
mwrites / MainQueueVsMainThread.swift
Last active October 13, 2017 10:13
MainQueue vs MainThread
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
let k = DispatchSpecificKey<String>()
DispatchQueue.main.setSpecific(key: k, value: "MainQ")
@mwrites
mwrites / cherry-pick-single-file.md
Created October 11, 2017 05:38
cherry-pick single file

up vote 387 down vote accepted I'd do it with cherry-pick -n (--no-commit) which lets you inspect (and modify) the result before committing:

git cherry-pick -n

unstage modifications you don't want to keep, and remove the

modifications from the work tree as well.

@mwrites
mwrites / TextSize.swift
Created October 8, 2017 09:29 — forked from gnou/TextSize.swift
Calculate height of some text when width is fixed
public struct TextSize {
fileprivate struct CacheEntry: Hashable {
let text: String
let font: UIFont
let width: CGFloat
let insets: UIEdgeInsets
fileprivate var hashValue: Int {
return text.hashValue ^ Int(width) ^ Int(insets.top) ^ Int(insets.left) ^ Int(insets.bottom) ^ Int(insets.right)