Skip to content

Instantly share code, notes, and snippets.

@harrytwright
harrytwright / Predicatable.swift
Last active April 30, 2022 13:01
Swift4 KeyPath Predicate
import Foundation
/// Predicatable is the protocol that all predicting objects conform.
public protocol Predicatable: CustomStringConvertible {
/// Returns a Boolean value indicating whether the specified object matches the conditions specified by the predicate.
///
/// - Parameter object: The object against which to evaluate the predicate.
/// - Returns: `true` if object matches the conditions specified by the predicate, otherwise `false`.
func evaluate(with object: Any?) -> Bool
@BjornRuud
BjornRuud / UILabel+Debug.swift
Created November 7, 2017 12:09
Debug mode for UILabel that can optionally show bounds, ascender, descender, x height, cap height, baseline and leading.
import UIKit
struct UILabelDebugOptions: OptionSet {
let rawValue: Int
static let bounds = UILabelDebugOptions(rawValue: 1 << 0)
static let ascender = UILabelDebugOptions(rawValue: 1 << 1)
static let descender = UILabelDebugOptions(rawValue: 1 << 2)
static let xHeight = UILabelDebugOptions(rawValue: 1 << 3)
static let capHeight = UILabelDebugOptions(rawValue: 1 << 4)
@yossan
yossan / io_monad_sample.swift
Last active September 19, 2019 08:36
IO Monad with Swift
enum Pair<T, WORLD> {
case cons (T, WORLD) // (value, outside)
}
// Outside the computer
typealias WORLD = Any
// IO Monad Instance (a.k.a IO Action)
typealias IO<T> = (WORLD) -> Pair<T, WORLD>
@oleander
oleander / Makefile
Created April 6, 2017 12:26
How to use the Swift Package Manager and CocoaPods within the same XCode application
APP="MyApp"
CONSTRUCT=xcodebuild -workspace $(APP).xcworkspace -scheme $(APP) clean
install_deps:
pod install
create_config:
swift package fetch
swift package generate-xcodeproj
wipe:
rm -rf .build $(APP).xcodeproj $(APP).xcworkspace Package.pins Pods Podfile.lock
@mbrowne
mbrowne / TransferMoney.swift
Last active November 4, 2024 05:00
DCI in Swift - simple money transfer example
//TransferMoney context
class TransferMoney: Context {
private let SourceAccount: SourceAccountRole
private let DestinationAccount: DestinationAccountRole
init(source:AccountData, destination:AccountData) {
SourceAccount = source as! SourceAccountRole
DestinationAccount = destination as! DestinationAccountRole
@timdonnelly
timdonnelly / Playground.swift
Last active April 23, 2023 05:26
Maintaining visible scroll position while inserting items in a UICollectionView (Swift playground)
import Foundation
import UIKit
import XCPlayground
XCPlaygroundPage.currentPage.needsIndefiniteExecution = true
class Layout: UICollectionViewLayout {
private var attributes: [[UICollectionViewLayoutAttributes]] = []
@JadenGeller
JadenGeller / AnyEquatable.swift
Last active January 6, 2024 18:26
AnyEquatable
public struct AnyEquatable: Equatable {
private let value: Any
private let equals: Any -> Bool
public init<E: Equatable>(_ value: E) {
self.value = value
self.equals = { ($0 as? E == value) ?? false }
}
}
@geoffrasb
geoffrasb / frp_note.txt
Last active September 4, 2015 10:07
FRP learning note
reference: Functional Reactive Animation - by Conal Elliott et al
* 2 things:
- Behavior a
- Event a
* semantic functions
- at :: Behavior a -> Time -> a
- occ :: Event a -> (Time, a)
@xareelee
xareelee / NSObject+PropertyName.h
Last active May 2, 2019 13:43
Objective-C property name to a string with autocompletion and compile-time check
// Release under MIT
// Copyright (C) 2015 Xaree Lee
#import <Foundation/Foundation.h>
/* Get property name for the class (C string or NSSting). */
#define keypathForClass(Klass, PropertyName) \
(((void)(NO && ((void)[Klass _nullObjectForCheckingPropertyName].PropertyName, NO)), # PropertyName))
#define keypathStringForClass(Klass, PropertyName) \
@keypathForClass(Klass, PropertyName)
@bradfrost
bradfrost / gist:59096a855281c433adc1
Last active September 4, 2023 15:01
Why I'm Not A JavaScript Developer

Answering the Front-end developer JavaScript interview questions to the best of my ability.

  • Explain event delegation

Sometimes you need to delegate events to things.

  • Explain how this works in JavaScript

This references the object or "thing" defined elsewhere. It's like "hey, thing I defined elsewhere, I'm talkin' to you."

  • Explain how prototypal inheritance works.