Skip to content

Instantly share code, notes, and snippets.

View illescasDaniel's full-sized avatar
💻
Learning something new

Daniel Illescas Romero illescasDaniel

💻
Learning something new
View GitHub Profile
@illescasDaniel
illescasDaniel / SemanticVersion.swift
Created March 9, 2020 15:31
SemanticVersion struct Swift
import Foundation
struct SemanticVersion: ExpressibleByStringLiteral {
let major: UInt8
let minor: UInt8
let patch: UInt8
init(stringLiteral value: String) {
self.init(value)
@illescasDaniel
illescasDaniel / Streams_v0.2.swift
Created February 25, 2020 20:21
Streams v0.2
/*
The MIT License (MIT)
Copyright (c) 2020 Daniel Illescas Romero
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
@illescasDaniel
illescasDaniel / Stream-v0.1.swift
Created February 25, 2020 08:04
Stream example swift
class Stream<T> {
var handler: ((T) -> Void)?
func emitValue(_ value: T) {
handler?(value)
}
func listen(_ handler: @escaping (T) -> Void) {
self.handler = handler
}
}
@illescasDaniel
illescasDaniel / GKStateMachine-example1.swift
Created February 24, 2020 20:27
GKStateMachine test example
import Foundation
import GameplayKit
protocol TestStatesDelegate: class {
func onFetch()
func onSuccess()
func onError()
}
enum DataFetchState {
@illescasDaniel
illescasDaniel / Links awakening color dungeon room 18 puzzle solver.swift
Created January 19, 2020 18:23
Link's awakening, Color Dungeon, Room 18 puzzle solver (brute force)
@illescasDaniel
illescasDaniel / String+fmt.kt
Created January 15, 2020 07:07
Kotlin fmt - simple curly braces format
fun String.fmt(args: Map<String, Any>) = Regex("""\{(\w+)\}""").replace(this) {
"${args.getOrDefault(it.groupValues[1], it.groupValues[0])}"
}
@illescasDaniel
illescasDaniel / swift string replace matches.swift
Created January 14, 2020 18:08
swift regular expression replace matches using transform
import Foundation
extension String {
func replacingOcurrences(ofPattern pattern: String, _ matchTransform: (NSTextCheckingResult) -> String) -> String {
guard let regex = try? NSRegularExpression(pattern: pattern, options: []) else {
return self
}
var transformedCharacters: [Character] = []
import Foundation
@propertyWrapper
public struct Validate<Value> {
fileprivate let _isValid: (Value) -> Bool
public let asserts: Bool
public let useLastValid: Bool
public let message: (Value) -> String
@illescasDaniel
illescasDaniel / UserDefault-propertywrapper.swift
Created June 5, 2019 14:50
UserDefault property wrapper
import Foundation
@propertyDelegate
public struct UserDefault<ValueType> {
public let key: String
public let defaults: UserDefaults
public let defaultValue: () -> ValueType
init(key: String, defaultValue: @autoclosure @escaping () -> ValueType, defaults: UserDefaults = .standard) {
@illescasDaniel
illescasDaniel / RangeSum.swift
Last active November 7, 2018 13:58
Sum Range Swift
extension ClosedRange where Bound: BinaryInteger {
var sum: Bound {
let uB = self.upperBound
let lB = self.lowerBound
return ((uB - (lB-1)) * (uB + lB)) / 2
}
}
extension Range where Bound: BinaryInteger {
var sum: Bound {