Skip to content

Instantly share code, notes, and snippets.

View DJBen's full-sized avatar

Sihao Lu DJBen

  • Snap Inc
  • Palo Alto
  • 19:33 (UTC -12:00)
View GitHub Profile
@DJBen
DJBen / UIImage+Lanczos5.swift
Created July 14, 2023 22:51
A snippet that resamples the image using the Lanczos5 resampling method with the specified scale factor.
import UIKit
import Accelerate
import ImageIO
import MobileCoreServices
extension UIImage {
/// Resamples the image using the Lanczos5 resampling method with the specified scale factor.
///
/// - Parameter scale: The scale factor to be applied to the image during resampling.
/// - Returns: A new UIImage instance that is the result of the Lanczos5 resampling, or `nil` if the operation fails.
@DJBen
DJBen / pipeline_with_multicontrolnet_fails_to_save.py
Created June 6, 2023 04:18
This snippet demonstrates the failure to save multi controlnet using Modal.com, a serverless function service
import modal
stub = modal.Stub("demo_multicontrolnet_failure")
modelsVolume = modal.SharedVolume()
# https://github.com/huggingface/diffusers/commit/f7b4f51cc2a423c96cb2a4c2282e55feba0be506
GIT_SHA = "f7b4f51cc2a423c96cb2a4c2282e55feba0be506"
sd_image = (
modal.Image.debian_slim(python_version="3.10")
@DJBen
DJBen / Initializer.stencil
Last active December 14, 2020 23:36
Sourcery: Initializer stencil with default values
{# A template to generate initializer inline #}
{# Example: $sourcery --sources <path/to/source.swift> --templates <path/to/this/stencil> --output Output/ #}
{% for type in types.structs %}
{% set spacing %}{% if type.parentName %} {% endif %}{% endset %}
{% map type.storedVariables into parameters using var %}{{ var.name }}: {{ var.typeName }}{% if var.defaultValue %} = {{var.defaultValue}}{% elif var.typeName.isOptional %} = nil{% endif %}{% endmap %}
// sourcery:inline:auto:{{ type.name }}.AutoInit
{{spacing}} {{ type.accessLevel }} init({{ parameters|join:", " }}) {
{{spacing}} {% for variable in type.storedVariables %}
{{spacing}} self.{{ variable.name }} = {{ variable.name }}
{{spacing}} {% endfor %}
@DJBen
DJBen / KeyOrderMap.swift
Created May 6, 2017 00:46
Swift 3 map function ordered by key
extension Dictionary where Key: Comparable {
func keyOrderMap<T>(_ transform: @escaping ((key: Key, value: Value)) throws -> T) rethrows -> [T] {
return try keys.sorted().map { try transform(($0, self[$0]!)) }
}
}
@DJBen
DJBen / PPAP.swift
Last active October 1, 2016 22:46
Apple-Pen-Pineapple-Pen lyric in swift! https://www.youtube.com/watch?v=d9TpRfDdyU0
// https://www.youtube.com/watch?v=d9TpRfDdyU0
let 🖋 = "🖋"
let 🍎 = "🍎"
let 🍍 = "🍍"
func verse(_ s1: String, _ s2: String) -> String {
return [s1,s2].map { "I have \(singular($0))" }.joined(separator: ", ") + "\nUh \(s2) \(s1)"
}
@DJBen
DJBen / UIImage+OrientationFix.swift
Created April 22, 2015 08:52
Fix UIImage Orientation in Swift
//
// UIImage+OrientationFix.swift
//
// Created by Sihao Lu on 4/22/15.
// Copyright (c) 2015 DJ.Ben. All rights reserved.
//
import UIKit
extension UIImage {
@DJBen
DJBen / Emoji.swift
Created January 3, 2015 18:17
Emoji-Swift
enum Emoji: String {
case Copyright = "\u{00A9}"
case Registered = "\u{00AE}"
case Bangbang = "\u{203C}"
case Interrobang = "\u{2049}"
case Tm = "\u{2122}"
case InformationSource = "\u{2139}"
case LeftRightArrow = "\u{2194}"
case ArrowUpDown = "\u{2195}"
case ArrowUpperLeft = "\u{2196}"
@DJBen
DJBen / swift-currying.swift
Created October 7, 2014 20:32
Swift Currying
func curry<T, U, V>(value: T, body: (T, U) -> V) -> U -> V {
let curried: U -> V = {
b in
return body(value, b)
}
return curried
}
// Examples
func add(a: Int, b: Int) -> Int {