This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Noncopyable struct: passed by reference | |
// and only one variable can hold the reference. | |
struct User: ~Copyable { | |
var name: String | |
// Read-only | |
func read() { | |
print(self.name) | |
// self.name = "BBBBB" // Error: 'self' is immutable | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {createWriteStream} from "fs"; | |
import {pipeline} from "stream/promises"; | |
try { | |
const response = await fetch("https://github.githubassets.com/images/modules/logos_page/Octocat.png"); | |
if (!response.ok) { | |
throw new Error(response.statusText); | |
} else { | |
await pipeline(response.body, createWriteStream("Octocat.png")); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# /etc/nginx/conf.d/default_server.conf | |
server { | |
server_name _; | |
listen 80 default_server; | |
listen [::]:80 default_server; | |
log_not_found off; | |
return 410; | |
} | |
server { | |
server_name _; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
extension UIView { | |
/// Updates `anchorPoint`, `frame`, and `layer.transform` to fit arbitrary corners. | |
/// Based on https://stackoverflow.com/questions/9470493/transforming-a-rectangle-image-into-a-quadrilateral-using-a-catransform3d/39981054#39981054 | |
func distort(topLeft tl: CGPoint, topRight tr: CGPoint, bottomLeft bl: CGPoint, bottomRight br: CGPoint) { | |
// Frame ----------------------------------------------------------- / | |
let xmin = min(tr.x, tl.x, bl.x, br.x) | |
let ymin = min(tr.y, tl.y, bl.y, br.y) | |
let xmax = max(tr.x, tl.x, bl.x, br.x) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import simd | |
// Interpolate a Quaternion without specifying duration, in the style of Herbert Stocker's X3D OrientationDamper: | |
// https://www.web3d.org/specifications/java/javadoc/org/web3d/x3d/sai/Followers/OrientationDamper.html | |
class OrientationDamper { | |
private(set) var isActive: Bool = false | |
private(set) var value: simd_quatf | |
private(set) var destination: simd_quatf | |
private(set) var tau: Float | |
private(set) var tolerance: Float |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
FROM ubuntu:22.04 | |
RUN apt update \ | |
&& apt install -y wget \ | |
&& wget https://github.com/digitalocean/doctl/releases/download/v1.88.0/doctl-1.88.0-linux-amd64.tar.gz -O doctl.tar.gz \ | |
&& tar xf doctl.tar.gz \ | |
&& rm doctl.tar.gz \ | |
&& mv doctl /usr/local/bin |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// CSS gradient generated by https://www.joshwcomeau.com/gradient-generator/ | |
// hsl(199deg 95% 55%) 0% | |
// hsl(140deg 63% 65%) 50% | |
// hsl(99deg 100% 50%) 100% | |
let gradient = CAGradientLayer() | |
// ... | |
gradient.type = .axial | |
gradient.colors = [ | |
UIColor.hsl(degrees: 199, saturation: 0.95, lightness: 0.55).cgColor, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
class JSON { | |
static func encodeData<T>(_ from: T, format: JSONEncoder.OutputFormatting = .sortedKeys) -> Data? where T: Encodable { | |
let encoder = JSONEncoder() | |
encoder.outputFormatting = format | |
do { | |
return try encoder.encode(from) | |
} catch { | |
print(error) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
extension FileManager { | |
public static var documentDirectory: URL { | |
return FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first! | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
import RealityKit | |
extension TextureResource { | |
static func generate(from: UIImage, withName: String?) -> TextureResource? { | |
guard let cgImage = from.cgImage else { return nil } | |
let options = TextureResource.CreateOptions( | |
semantic: .color, | |
mipmapsMode: .allocateAndGenerateAll | |
) |