Skip to content

Instantly share code, notes, and snippets.

View wildthink's full-sized avatar

Jason Jobe wildthink

  • 00:03 (UTC -04:00)
View GitHub Profile
@resultBuilder
struct StringBuilder {
static func buildBlock(_ components: String...) -> String {
let filtered = components.filter { $0 != "" }
return filtered.joined(separator: "⭐️")
}
static func buildOptional(_ component: String?) -> String {
return component ?? ""
@wildthink
wildthink / line.swift
Created April 17, 2021 13:10 — forked from GrantMeStrength/line.swift
Drawing a line between two points in SceneKit / iOS / Swift
func line(from p1: SCNVector3, to p2: SCNVector3) -> SCNNode? {
// Draw a line between two points and return it as a node
var indices = [Int32(0), Int32(1)]
let positions = [p1, p2]
let vertexSource = SCNGeometrySource(vertices: positions)
let indexData = Data(bytes: &indices, count:MemoryLayout<Int32>.size * indices.count)
let element = SCNGeometryElement(data: indexData, primitiveType: .line, primitiveCount: 1, bytesPerIndex: MemoryLayout<Int32>.size)
let line = SCNGeometry(sources: [vertexSource], elements: [element])
let lineNode = SCNNode(geometry: line)
@wildthink
wildthink / property-key-paths.swift
Created May 29, 2021 15:37 — forked from rxwei/property-key-paths.swift
StoredPropertyIterable + CustomKeyPathIterable
//============================================================================//
// Part 1. StoredPropertyIterable
// This models the purely static layout of a struct.
//============================================================================//
// This is an implementation detail that is required before PAT existentials are
// possible.
protocol _StoredPropertyIterableBase {
static var _allStoredPropertiesTypeErased: [AnyKeyPath] { get }
static var _recursivelyAllStoredPropertiesTypeErased: [AnyKeyPath] { get }
@wildthink
wildthink / ScrollableView.swift
Created August 31, 2021 21:12 — forked from jfuellert/ScrollableView.swift
A scrollable SwiftUI view, UIScrollView wrapper. ScrollableView lets you read and write content offsets for scrollview in SwiftUI, with and without animations.
import SwiftUI
struct ScrollableView<Content: View>: UIViewControllerRepresentable, Equatable {
// MARK: - Coordinator
final class Coordinator: NSObject, UIScrollViewDelegate {
// MARK: - Properties
private let scrollView: UIScrollView
var offset: Binding<CGPoint>
@wildthink
wildthink / TwoWaySnapList.swift
Created September 1, 2021 03:18 — forked from sameersyd/TwoDirectionalSnapList.swift
SwiftUI - Two Directional SnapList
// Checkout the explanation article here - https://sameer-syd.medium.com/swiftui-two-directional-snaplist-95cb852957be
import SwiftUI
import Combine
struct HomeView: View {
@StateObject var viewModel: HomeViewModel
@wildthink
wildthink / SnapCarousel.swift
Created September 8, 2021 09:54 — forked from xtabbas/SnapCarousel.swift
A carousel that snap items in place build on top of SwiftUI
//
// SnapCarousel.swift
// prototype5
//
// Created by xtabbas on 5/7/20.
// Copyright © 2020 xtadevs. All rights reserved.
//
import SwiftUI
@wildthink
wildthink / transducer-type.swift
Created September 10, 2021 09:43 — forked from hsavit1/transducer-type.swift
How to get a transducer type without higher kinds
import Foundation
// A bunch of convenience things
func const <A, B> (b: B) -> A -> B {
return { _ in b }
}
func repeat <A> (n: Int) -> A -> [A] {
return { a in
return map(Array(1...n), const(a))
}
@wildthink
wildthink / DraggableView.swift
Last active October 2, 2021 10:02 — forked from ohayon/DraggableView.swift
Example of making a reusable `draggable()` modifier for SwiftUI Views
//
// DraggableView.swift
// Created by Jason Jobe on 9/15/21.
//
import SwiftUI
public struct DraggableView: ViewModifier {
public enum Axis { case x, y, xy }
@wildthink
wildthink / latency.txt
Last active September 21, 2021 13:57 — forked from jboner/latency.txt
Latency Numbers Every Programmer Should Know
Latency Comparison Numbers (~2012)
----------------------------------
L1 cache reference 0.5 ns
Branch mispredict 5 ns
L2 cache reference 7 ns 14x L1 cache
Mutex lock/unlock 25 ns
Main memory reference 100 ns 20x L2 cache, 200x L1 cache
Compress 1K bytes with Zippy 3,000 ns 3 us
Send 1K bytes over 1 Gbps network 10,000 ns 10 us
Read 4K randomly from SSD* 150,000 ns 150 us ~1GB/sec SSD
@wildthink
wildthink / Always.swift
Created December 12, 2021 09:51 — forked from sharplet/Always.swift
A Combine publisher that repeatedly emits the same value as long as there is demand
// Copyright (c) 2019–20 Adam Sharp and thoughtbot, inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in