Skip to content

Instantly share code, notes, and snippets.

View lukevanin's full-sized avatar

Luke Van In lukevanin

View GitHub Profile
@lattner
lattner / async_swift_proposal.md
Last active December 28, 2024 11:11 — forked from oleganza/async_swift_proposal.md
Concrete proposal for async semantics in Swift

Async/Await for Swift

Introduction

Modern Cocoa development involves a lot of asynchronous programming using closures and completion handlers, but these APIs are hard to use. This gets particularly problematic when many asynchronous operations are used, error handling is required, or control flow between asynchronous calls gets complicated. This proposal describes a language extension to make this a lot more natural and less error prone.

This paper introduces a first class Coroutine model to Swift. Functions can opt into to being async, allowing the programmer to compose complex logic involving asynchronous operations, leaving the compiler in charge of producing the necessary closures and state machines to implement that logic.

@JohnSundell
JohnSundell / Autoclosure.swift
Created January 19, 2017 15:00
Simple Dictionary extension to avoid the if let-dance when retrieving values
extension Dictionary {
mutating func value(for key: Key, orAdd closure: @autoclosure () -> Value) -> Value {
if let value = self[key] {
return value
}
let value = closure()
self[key] = value
return value
}
@junpluse
junpluse / CVError.swift
Created December 15, 2016 11:21
CVReturn as Swift.Error
//
// CVError.swift
//
// Created by Jun Tanaka on 2016/12/13.
// Copyright © 2016 Jun Tanaka. All rights reserved.
//
import CoreVideo
public enum CVError: Int32, Error {
//
// SnippetVideoCompositionInstruction.swift
// OneSecondEveryday
//
// Created by Sami Samhuri on 2016-09-05.
// Copyright © 2016 1 Second Everyday. All rights reserved.
//
import Foundation
import AVFoundation
import Foundation
protocol Monoid {
static var zero: Self { get }
func appending(_: Self) -> Self
}
struct AnyMonoid<Type> {
var zero: Type
let append: (Type, Type) -> Type
@shahdhiren
shahdhiren / P12toPEM.txt
Created September 9, 2016 14:57
Convert P12 file for Push Notification to PEM format
Development Phase:
Step 1: Create Certificate .pem from Certificate .p12
Command: openssl pkcs12 -clcerts -nokeys -out apns-dev-cert.pem -in apns-dev-cert.p12
Step 2: Create Key .pem from Key .p12
Command : openssl pkcs12 -nocerts -out apns-dev-key.pem -in apns-dev-key.p12
Step 3: Optional (If you want to remove pass phrase asked in second step)
Command : openssl rsa -in apns-dev-key.pem -out apns-dev-key-noenc.pem
@andymatuschak
andymatuschak / States-v3.md
Last active April 14, 2025 22:47
A composable pattern for pure state machines with effects (draft v3)

A composable pattern for pure state machines with effects

State machines are everywhere in interactive systems, but they're rarely defined clearly and explicitly. Given some big blob of code including implicit state machines, which transitions are possible and under what conditions? What effects take place on what transitions?

There are existing design patterns for state machines, but all the patterns I've seen complect side effects with the structure of the state machine itself. Instances of these patterns are difficult to test without mocking, and they end up with more dependencies. Worse, the classic patterns compose poorly: hierarchical state machines are typically not straightforward extensions. The functional programming world has solutions, but they don't transpose neatly enough to be broadly usable in mainstream languages.

Here I present a composable pattern for pure state machiness with effects,

import Foundation
protocol Currency { static var sign: String { get } }
enum GBP: Currency { static let sign = "£" }
enum EUR: Currency { static let sign = "€" }
enum USD: Currency { static let sign = "$" }
protocol _Money {
associatedtype C: Currency
var amount: NSDecimalNumber { get }
@beccadax
beccadax / Example.swift
Last active March 6, 2020 16:10
Elegant handling of localizable strings in Swift. Note: This code is in Swift 2 and would need updates to be used in modern Swift.
let color = "blue"
let num = 42
localized("Colorless green ideas sleep furiously.")
localized("Colorless \(color) ideas sleep furiously.")
localized("\(num.formatted("%05d")) colorless green ideas sleep furiously.")
@JohnEstropia
JohnEstropia / frchack.swift
Last active June 8, 2018 08:00
A workaround for an NSFetchedResultsController bug introduced in XCode 7. Credits to stackoverflow user Andy: http://stackoverflow.com/a/32466951/809614
// Workaround a nasty bug introduced in XCode 7 targeted at iOS 8 devices
// https://forums.developer.apple.com/message/9998#9998
// https://forums.developer.apple.com/message/31849#31849
// This is not my original idea. Please give your stars to stackoverflow user Andy for sharing his solution: http://stackoverflow.com/a/32466951/809614
class MyFetchedResultsControllerDelegate: NSObject, NSFetchedResultsControllerDelegate {
// ... your code
// MARK: Private