Skip to content

Instantly share code, notes, and snippets.

View dudarenko-io's full-sized avatar

Ilya Dudarenko dudarenko-io

View GitHub Profile
@dudarenko-io
dudarenko-io / ArrayFlatMap.swift
Last active June 29, 2017 14:50
Swift array flatMap example used to cast array`s content
class Person: CustomStringConvertible {
let name: String
var residence: String?
init(name: String,residence: String) {
self.name = name
self.residence = residence
}
var description: String {
@dudarenko-io
dudarenko-io / MapForFunctions.swift
Created June 30, 2017 15:02
Functions are functors too. Map function implemented to concatenate functions
func map<T,S,U>(_ f: @escaping (T)->S, _ g: @escaping (S)->U) -> (T) -> U {
return {x in g(f(x)) }
}
let countNumberOfDigits = map( { (n: Int) in "\(n)" }, { (str: String) in str.characters.count } )
countNumberOfDigits(12345)
extension Dictionary where Value: Collection {
/// A Boolean value indicating whether all collection-values in dictionary is empty.
var isContentsEmpty: Bool {
for collection in values where !collection.isEmpty {
return false
}
return true
}
}
extension String {
/// Finds and returns the range of the first occurrence of a given string within a given left and right occurencies Strings.
func rangeBetween(leftStringOccurence: String, rightStringOccurence: String) -> Range<String.Index>? {
guard let lowerBound = self.range(of: leftStringOccurence)?.upperBound,
let upperBound = self.range(of: rightStringOccurence)?.lowerBound,
lowerBound < upperBound else {
return nil
}
return lowerBound..<upperBound
}
enum Style {
case normal
case uppercased
}
extension Style {
func applyLetterCaseToText(_ text: String) -> String {
switch self {
case .normal:
return text
class A {}
class B: A {}
class C: A {}
extension A: CustomStringConvertible {
var description: String {
return "type of: \(type(of: self))"
}
}
// from ExistentialCollection.swift.gyb
@inline(never)
internal func _abstract(
file: StaticString = #file,
line: UInt = #line
) -> Never {
fatalError("Method must be overridden", file: file, line: line)
}
class MutableBox<T> {
var value: T
init(value: T) {
self.value = value
}
}
extension MutableBox: CustomDebugStringConvertible {
var debugDescription: String {
/// Class file structure
// MARK: - Internal Properties
// MARK: - Private Properties
// MARK: - Init / Deinit
// MARK: - Public Methods
#!/bin/sh
directories=(Example Source Tests)
for directory in "${directories[@]}"
do
echo "Cleaning whitespace in directory: $directory"
find $directory -iregex '.*\.swift' -exec sed -E -i '' -e 's/[[:blank:]]*$//' {} \;
done