Skip to content

Instantly share code, notes, and snippets.

View DanielCardonaRojas's full-sized avatar

Daniel Cardona Rojas DanielCardonaRojas

View GitHub Profile
@DanielCardonaRojas
DanielCardonaRojas / StringExtensions.swift
Last active February 1, 2021 15:14
String extensions
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)
})
}
/* -------------- 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 {
@DanielCardonaRojas
DanielCardonaRojas / ArrayExtensions.swift
Last active October 31, 2022 16:50
FP Array Extensions
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] {
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)
@DanielCardonaRojas
DanielCardonaRojas / Monoids.swift
Last active October 30, 2017 20:50
Monoids in Swift
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 {