Skip to content

Instantly share code, notes, and snippets.

View loromits's full-sized avatar

Anton Kovtun loromits

  • Mykolayiv, Ukraine
View GitHub Profile
@loromits
loromits / Stack.swift
Last active September 11, 2017 10:06
Simple Stack data structure implementation in Swift
struct Stack<Element> {
fileprivate class Node {
let value: Element
var previousNode: Node?
init(_ value: Element, previousNode: Node?) {
self.value = value
self.previousNode = previousNode
}
}
@loromits
loromits / DoublyLinkedList.swift
Last active July 21, 2023 03:27
Doubly Linked List implementation in Swift
struct DoublyLinkedList<Element> {
class Node {
var value: Element
weak var next: Node?
var previous: Node?
init(_ value: Element, next: Node?, previous: Node?) {
(self.value, self.next, self.previous) = (value, next, previous)
next?.previous = self
previous?.next = self
@loromits
loromits / RandomAccessCollection+StableSorted.swift
Created October 27, 2017 07:17
Add stableSorted to Swift collections
extension RandomAccessCollection {
func stableSorted(by areInIncreasingOrder: (Self.Element, Self.Element) -> Bool) -> [Self.Element] {
return enumerated()
.sorted { first, second in
return areInIncreasingOrder(first.element, second.element) || first.offset < second.offset
}
.map { $0.element }
}
}
@loromits
loromits / ConstraintSwitcher.swift
Created November 9, 2017 11:40
Extend SnapKit with struct that is able to switch two sets of constraints
import SnapKit
struct ConstraintSwitcher {
private var initialActive = [Constraint]()
private var initialInactive = [Constraint]()
private(set) var isInitialActive = true
mutating func append(_ constraint: Constraint, active: Bool) {
if active == isInitialActive {
initialActive.append(constraint)
extension Dictionary {
func flatMapValues<T>(_ transform: (Value) throws -> T?) rethrows -> [Key: T] {
return try reduce(into: [:]) { $0[$1.key] = try transform($1.value) }
}
}
extension Sequence {
func toDictionary<T: Hashable>(_ keyTransformer: (Element) throws -> T) rethrows -> Dictionary<T, Element> {
return try reduce(into: [:]) { try $0[keyTransformer($1)] = $1 }
}
}
@loromits
loromits / EventDispatcher.swift
Last active April 27, 2018 12:25
Simple stupid internally type-erasing reactive NotificationCenter addition to handle observables through misconnected parts of application
import RxSwift
protocol Dispatchable {}
protocol DispatchableElement {}
extension PublishSubject: Dispatchable where E: DispatchableElement {}
final class EventDispatcher {
import Foundation
final class XMLElementExtractor: NSObject, XMLParserDelegate {
private var isTarget = false
private var value: String?
private let elementName: String
private init(elementName: String) {
self.elementName = elementName
}
@loromits
loromits / JSON.swift
Last active November 5, 2019 12:56
Modification of https://gist.github.com/itaiferber/d26cf93355f3415f4f684510f21ca202 to be able to explore contents of JSON.
import Foundation
public enum JSON : Codable {
case null
case boolean(Bool)
case number(Double)
case string(String)
case array([JSON])
case dictionary([String : JSON])
struct SubtitleEntry {
let index: Int
let startTime, endTime: TimeInterval
let text: String
}
extension String {
subscript(nsrange: NSRange) -> String? {
return Range(nsrange, in: self)
.flatMap { range in String(self[range]) }