Skip to content

Instantly share code, notes, and snippets.

View pwightman's full-sized avatar

Parker Wightman pwightman

View GitHub Profile
@pwightman
pwightman / RemoteImage.swift
Last active July 21, 2020 17:51
Basic image fetcher, with in-memory cache and request deduplication.
import Combine
import SwiftUI
import UIKit
struct RemoteImage: View {
let url: URL
var placeholder: AnyView?
@StateObject var fetcher = RemoteImageFetcher()
@pwightman
pwightman / foo.ex
Last active February 18, 2020 17:04
# Allows me to call Enum.map as a "pre-curried" func, kind of...
[1, 2, 3]
|> Enum.map(& &1 * 2)
# vs...
def my_map(list) do
fn transform ->
Enum.map(list, transform)
end
@pwightman
pwightman / JavaScriptEncode.swift
Last active March 10, 2021 13:29
How to escape a string of JSON/JavaScript in iOS.
extension String {
func javaScriptEscapedString() -> String {
// Because JSON is not a subset of JavaScript, the LINE_SEPARATOR and PARAGRAPH_SEPARATOR unicode
// characters embedded in (valid) JSON will cause the webview's JavaScript parser to error. So we
// must encode them first. See here: http://timelessrepo.com/json-isnt-a-javascript-subset
// Also here: http://media.giphy.com/media/wloGlwOXKijy8/giphy.gif
let str = self
.stringByReplacingOccurrencesOfString("\u{2028}", withString: "\\u2028")
.stringByReplacingOccurrencesOfString("\u{2029}", withString: "\\u2029")
// Because escaping JavaScript is a non-trivial task (https://github.com/johnezang/JSONKit/blob/master/JSONKit.m#L1423)
class Locks {
private var queuedDocuments = [String:Document]()
private var inFlightDocuments = [String:Document]()
private let queuedQueue = dispatch_queue_create("com.lucidchart.DocumentStateStoreLocks.queued", DISPATCH_QUEUE_SERIAL)
private let inFlightQueue = dispatch_queue_create("com.lucidchart.DocumentStateStoreLocks.inFlight", DISPATCH_QUEUE_SERIAL)
func queued(block: [String:Document] -> [String:Document]) {
dispatch_sync(queuedQueue) {
self.queuedDocuments = block(self.queuedDocuments)
@pwightman
pwightman / hypermedia.swift
Last active August 29, 2015 14:10
Basic Hypermedia API implementation in Swift
import UIKit
let userJSON = [
"url": "https://example.com/users/123",
"name": "Jim",
"profile_url": "https://example.com/users/123/profile",
"posts_url": "https://example.com/users/123/posts"
]
// API resources, such as User, Profile, Post, etc.
<a href="javascript:toggleChatWindow()">Chat With Us!</a>
<script>
function toggleChatWindow() {
if (FHChat.chatState == "minimized") {
FHChat.transitionTo("maximized");
document.getElementById("fchat-message").focus();
} else {
FHChat.transitionTo("minimized");
}
<a href="javascript:popOpenChatWindow()">Chat With Us!</a>
<script>
function popOpenChatWindow() {
FHChat.transitionTo("maximized");
document.getElementById("fchat-message").focus();
};
</script>
@pwightman
pwightman / ZSPattern.m
Last active August 29, 2015 13:57
Pattern matching, taken from functional programming languages like Clojure/Racket, applied to Objective-C.
// Example Goal: find URLs embedded in arbitrary JSON
NSDictionary *JSON = @{
@"some": @[
@"arbitrary": @[
@"json",
@"http://google.com",
@{ @"url": @"http://yahoo.com" }
]
]
@pwightman
pwightman / example.rb
Last active August 29, 2015 13:57
example.rb
# post.rb
class Post
has_many :tags, :through => :post_tags
has_many :post_tags
end
# tag.rb
class Tag
has_many :posts, :through => :post_tags
has_many :post_tags
@pwightman
pwightman / letterpress.clj
Last active August 29, 2015 13:56
Better Letterpress Solver
(ns letterpress-solver.core
(:require [clojure.string :as string])
(:require [multiset.core :as ms]))
(def board-letters "srrbtsdofaghvypksbkkbegnv")
(def letterpress-wordlist (string/split-lines (slurp "resources/wordlist.txt")))
(defn -main [& args]
(println "Began...")