Skip to content

Instantly share code, notes, and snippets.

View pteasima's full-sized avatar

Petr Sima pteasima

  • simapps s.r.o.
  • Prague, Czech Republic
  • X @pteasima
View GitHub Profile
@CodaFi
CodaFi / Free.swift
Created September 20, 2014 19:20
Free Monads in Swift
//
// Free.swift
// Swift_Extras
//
// Created by Robert Widmann on 9/19/14.
// Copyright (c) 2014 Robert Widmann. All rights reserved.
//
import Foundation
@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 }
}
}
@aryaxt
aryaxt / RawRepresentableExtension
Last active April 19, 2018 08:31
Swift enum -> Get an array of all cases
extension RawRepresentable where Self: Hashable {
private static func iterateEnum<T: Hashable>(_: T.Type) -> AnyIterator<T> {
var index = 0
let closure: () -> T? = {
let next = withUnsafePointer(to: &index) {
$0.withMemoryRebound(to: T.self, capacity: 1) { $0.pointee }
}
guard next.hashValue == index else { return nil }
@sjoerdvisscher
sjoerdvisscher / expression-problem.swift
Last active August 21, 2019 16:57
Finally tagless in Swift 3
protocol Expr {
static func lit(_ x: Int) -> Self
static func add(_ lhs: Self, _ rhs: Self) -> Self
}
extension Int : Expr {
static func lit(_ x : Int) -> Int {
return x;
}
static func add(_ lhs: Int, _ rhs: Int) -> Int {
@inamiy
inamiy / SwiftElmFrameworkList.md
Last active March 11, 2024 10:20
React & Elm inspired frameworks in Swift
@anandabits
anandabits / HKT.swift
Last active December 25, 2023 00:57
Emulating HKT in Swift
// This example shows how higher-kinded types can be emulated in Swift today.
// It acheives correct typing at the cost of some boilerplate, manual lifting and an existential representation.
// The technique below was directly inspired by the paper Lightweight Higher-Kinded Polymorphism
// by Jeremy Yallop and Leo White found at http://ocamllabs.io/higher/lightweight-higher-kinded-polymorphism.pdf
/// `ConstructorTag` represents a type constructor.
/// `Argument` represents an argument to the type constructor.
struct Apply<ConstructorTag, Argument> {
/// An existential containing a value of `Constructor<Argument>`
/// Where `Constructor` is the type constructor represented by `ConstructorTag`
@chriseidhof
chriseidhof / forms-tagless.swift
Last active January 4, 2018 12:51
forms.swift
//
// ViewController.swift
// Forms
// By Brandon Kase and Chris Eidhof
import UIKit
protocol Element {
associatedtype Input
//: [Previous](@previous)
import Foundation
struct Counter {
var name: String = ""
var age: Int = 33
enum Action {
case increment
//: [Previous](@previous)
import Foundation
protocol Changable {
associatedtype Change
mutating func apply(_ diff: Change)
}
extension Array: Changable {
enum Command {
case buttonTapped
case textChanged(String)
}
enum SideEffect<C> {
case anEvent(C)
case readFile((String) -> C)
// ...
}