Skip to content

Instantly share code, notes, and snippets.

Soroush Khanlou khanlou

Block or report user

Report or block khanlou

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@khanlou
khanlou / Fonts.swift
Created Oct 6, 2016
Print all fonts in Swift 3
View Fonts.swift
UIFont.familyNames.forEach({ familyName in
let fontNames = UIFont.fontNames(forFamilyName: familyName)
print(familyName, fontNames)
})
View ProbingDictionary.swift
import Foundation
struct ProbingDictionary<Key: Hashable, Value> {
enum Item: CustomStringConvertible {
case element(Key, Value)
case empty
case tombstone
var isEmpty: Bool {
if case .empty = self {
View NilError.swift
public struct NilError: Error, CustomStringConvertible {
let file: String
let line: Int
public init(file: String = #file, line: Int = #line) {
self.file = file
self.line = line
}
View SafeSyncQueue.swift
import Foundation
final class SafeSyncQueue {
struct QueueIdentity {
let label: String
}
let queue: DispatchQueue
@khanlou
khanlou / Swift2.swift
Last active Jun 8, 2019
`any`, `all`, `none`, `first`, and `count` on SequenceType in Swift
View Swift2.swift
import Foundation
extension SequenceType {
@warn_unused_result
func any(@noescape predicate: (Self.Generator.Element) throws -> Bool) rethrows -> Bool {
for element in self {
let result = try predicate(element)
if result {
return true
}
View SafeDecodableArray.swift
import Foundation
/**
Decode an array of objects while simply omitting any nested objects that themselves fail to be decoded.
Inspired by https://stackoverflow.com/a/46369152/503916
*/
struct SafeDecodableArray<T: Decodable>: Decodable {
/*
An intermediate type that always succeeds at being decoded. Necessary because when iterating the
View ObjectStorage.swift
//
// ObjectStorage.swift
// Soroush Khanlou
//
// Created by Soroush Khanlou on 3/8/19.
// Copyright © 2019 Soroush Khanlou. All rights reserved.
//
import Foundation
@khanlou
khanlou / MD5StringTest.swift
Last active Apr 12, 2019
MD5 and SHA1 on String in Swift 3
View MD5StringTest.swift
import XCTest
@testable import <#project#>
class StringMD5Test: XCTestCase {
func testMD5() {
let string = "soroush khanlou"
XCTAssertEqual(string.md5, "954d741d14b14002d1ba88f600eee635")
@khanlou
khanlou / Sequence+Arithmetic.swift
Last active Mar 20, 2019
Sum on Sequence and Average on Array in Swift 4
View Sequence+Arithmetic.swift
extension Sequence where Self.Iterator.Element: Numeric {
var sum: Self.Iterator.Element {
return self.reduce(0, +)
}
}
extension Collection where Element: BinaryFloatingPoint {
var average: Element {
return self.reduce(0, +) / Element((0 as IndexDistance).distance(to: self.count))
}
View ZipWithDefaults.swift
let zipped = zip([1,2,3], default: 0, ["a"], default: "-")
print(Array(zipped))
let zippedSameType = zip([1,2,3], [1], default: 0)
print(Array(zippedSameType))
You can’t perform that action at this time.