Skip to content

Instantly share code, notes, and snippets.

View hooman's full-sized avatar

Hooman Mehr hooman

  • Kirkland, WA, United States
  • X @h_mehr
View GitHub Profile
@dabrahams
dabrahams / EfficientAny.swift
Last active June 14, 2021 22:39
An efficient storage foundation for type erasure.
// Copyright 2020 Penguin Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
@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`
@lattner
lattner / TaskConcurrencyManifesto.md
Last active July 21, 2024 05:08
Swift Concurrency Manifesto
//: Playground - noun: a place where people can play
//: http://webyrd.net/scheme-2013/papers/HemannMuKanren2013.pdf
typealias Var = Int
typealias Subst = [(Var, Term)]
typealias State = (Subst, Int)
typealias Goal = State -> Stream
indirect enum Stream {
case Nil, Cons(State, Stream), Lazy(() -> Stream)
@erica
erica / pggrep.swift
Last active November 30, 2018 08:52
Find files in playgrounds, which are otherwise not indexed by spotlight
//
// main.swift
// pggrep - updated 12/4/15
// Created by Erica Sadun on 6/17/15.
// Copyright © 2015 Erica Sadun. All rights reserved.
//
import Foundation
extension String {
@chriseidhof
chriseidhof / gcd.swift
Created August 28, 2014 01:08
GCD Wrappers
import Foundation
// Executes an array of blocks in parallel, but only returns after they're all done.
func parallel(blocks: [() -> ()]) {
let group = dispatch_group_create()
let queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
for block in blocks {
dispatch_group_async(group, queue, block)
}
@rnapier
rnapier / gist:8eda179689d9d61c2bfb
Last active August 29, 2015 14:04
And autoclosure saves(?) the day for generic recursive enums
// Creating a generic recursive data structure with autoclosure. (READ ALL NOTES; THIS MAY NOT DO WHAT YOU WANT.)
// Closures are reference types, so the size is known (? I think ?)
// Note that this is just because of unimplemented features in the compiler in Beta5
// There's no reason to think this is a long-term requirement.
// IMPORTANT: the closure will run every time you access this value, so if that has
// side effects, this won't work. It's only possible on pure value types.
// But the fact that this works as expected is actually kind of incredible.
// Think about what is required for it to work out the type for NestedList.Elem("first").
@CodaFi
CodaFi / Aviary.swift
Last active October 23, 2023 20:32
Fly my pretties
// Playground - noun: a place where people can play
// I wouldn't want a pair of birds that were... too demonstrative.
func idiot<A>(a : A) -> A {
return a
}
func kestrel<A, B>(a : A) -> B -> A {
return { _ in a }
@chriseidhof
chriseidhof / parsing.swift
Last active April 16, 2023 02:38
JSON Parsing in Swift
// This code accompanies a blog post: http://chris.eidhof.nl/posts/json-parsing-in-swift.html
//
// As of Beta5, the >>= operator is already defined, so I changed it to >>>=
import Foundation
let parsedJSON : [String:AnyObject] = [
"stat": "ok",
"blogs": [
@turowicz
turowicz / swift-json-class.md
Last active February 21, 2017 07:00
Apple Swift strong type object serialization to JSON