This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
// MARK: - Getting substrings | |
// String API lacks take(while:) and suffix(while:) but has drop(while:) and prefix(while:) | |
extension String { | |
func prefix(while predicate: (Unicode.Scalar) -> Bool) -> Substring { | |
return self.prefix(while: { (c: Character) -> Bool in | |
return c.unicodeScalars.contains(where: predicate) | |
}) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* -------------- Higher order function combinators ---------------- */ | |
// Composition operators | |
infix operator >>> | |
func >>> <A,B,C> (aToB: @escaping (A) -> B, bToC: @escaping (B) -> C) -> (A) -> C { | |
return { a in aToB(a) |> bToC } | |
} | |
infix operator <<< | |
func <<< <A,B,C> (bToC: @escaping (B) -> C, aToB: @escaping (A) -> B) -> (A) -> C { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var str = "hhhhh Hello playground" | |
extension String { | |
static func groupBy(_ string: String, _ f: (Character, Character) -> Bool) -> [String] { | |
let array = Array(string) as! [Character] | |
let groups = Array.groupBy(array, f) | |
return groups.map { g in String(g) } | |
} | |
static func group(_ string: String) -> [String] { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class BezierView: UIView { | |
private var bezier: UIBezierPath | |
init(bezier: UIBezierPath) { | |
self.bezier = bezier | |
let transform = CGAffineTransform() | |
transform.scaledBy(x: 2, y: 2) | |
let rectBounds = bezier.bounds | |
rectBounds.applying(transform) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
protocol Semigroup { | |
/* A semigroup is a set and a binary operator defined for elements within that | |
set, to yeild a another element of the same type. | |
*/ | |
static func combine (_ lhs: Self, _ rhs: Self) -> Self | |
} | |
extension Semigroup { | |
// Extending a protocol enables to get free functions based on the implementation of the protocol | |
func mappend(_ with: Self) -> Self { |
NewerOlder