Skip to content

Instantly share code, notes, and snippets.

View swiftyfinch's full-sized avatar
🏖️
On vacation

Vyacheslav Khorkov swiftyfinch

🏖️
On vacation
View GitHub Profile
@swiftyfinch
swiftyfinch / Set+Inserts.swift
Last active May 16, 2021 09:49
Inserts an collection to Set (It's not the same as union)
extension Set {
/// Inserts other collection to current. It works like the `insert` method, but not like the `union`.
func inserts<S: Sequence>(_ other: S) -> Self where S.Element == Element {
var new = self
other.forEach { new.insert($0) }
return new
}
}
@swiftyfinch
swiftyfinch / Optional+Unwrap.swift
Created January 30, 2021 11:09
Unwrap nested Optionals or throw error.
protocol OptionalType {
associatedtype Wrapped
var optional: Wrapped? { get }
}
extension Optional: OptionalType {
var optional: Self { self }
}
extension Optional {
final class Storage {
init() { print(“Storage init.”) }
}
final class Example {
@LazyOnce var storage = Storage()
}
let example = Example() // Output:
example.storage // Output: Storage init.
@swiftyfinch
swiftyfinch / LazyOnce.swift
Created September 23, 2020 07:32
LazyOnce implementation (for post: https://medium.com/@tinkoffhere/b22f51422345).
@propertyWrapper final class LazyOnce<T> {
var wrappedValue: T {
if let existStorage = storage { return existStorage }
let newStorage = lazyBlock()
self.storage = newStorage
return newStorage
}
private var storage: T?
private let lazyBlock: () -> T
init(wrappedValue: @escaping @autoclosure () -> T) {
@swiftyfinch
swiftyfinch / LazyOne.swift
Last active July 28, 2020 15:20
LazyOne it's an easy implementation of default lazy keyword based on @propertyWrapper feature.
@propertyWrapper
final class LazyOnce<T> {
private var storage: T?
private let lazyBlock: () -> T
var wrappedValue: T {
if let existStorage = storage { return existStorage }
let newStorage = lazyBlock()
self.storage = newStorage
return newStorage
@swiftyfinch
swiftyfinch / PodTree.rb
Last active November 22, 2023 14:32
🌳 Tiny utility for visualising CocoaPods dependencies tree. https://swiftyfinch.github.io/en/2020-06-20-pod-tree/
#!/usr/bin/ruby
require 'set'
# Constants
OUTPUT_BULLET = "•"
OUTPUT_BULLET_COLORS = [9, 3, 2, 75, 99]
# Help information
HELP_COMMANDS = ["-h", "--help", "help"]
#-------------------------------------------------------------------------------
# CocoaPods
#-------------------------------------------------------------------------------
function pods_install() {
red="\001$(tput setaf 1)\002"
yellow="\001$(tput setaf 3)\002"
green="\001$(tput setaf 2)\002"
reset="\001$(tput sgr0)\002"
if [ "$1" = "-f" ] ; then
struct WithoutEscapingSlashesEncoder {
private enum EncoderError: InternalError {
case dataToUTF8
case utf8ToData
}
func encode<T: Encodable>(_ object: T) throws -> Data {
let jsonEncoder = JSONEncoder()
if #available(iOS 13, *) {