Skip to content

Instantly share code, notes, and snippets.

@erikbasargin
erikbasargin / AsyncZip.swift
Last active July 24, 2023 08:38
Implementation of a zip function which accept asynchronous functions as variadic parameters (parameter packs).
import Foundation
func zip<FirstElement, SecondElement, each Element>(
_ firstOperation: @Sendable @escaping () async throws -> FirstElement,
_ secondOperation: @Sendable @escaping () async throws -> SecondElement,
_ operation: repeat @Sendable @escaping () async throws -> (each Element)) async throws -> (FirstElement, SecondElement, repeat each Element)
{
let group = OperationGroup()
group.addOperation(firstOperation)
@erikbasargin
erikbasargin / USD.build.command
Last active May 16, 2022 20:57
Build Pixar’s USD Library for Apple’s USDZ Convert 0.65 (Python 3.7)
#!/bin/sh
# Uncomment the next line to use Python 3.7.9 localy
# pyenv local 3.7.9
[ -d "./USD" ] && mv USD USD-$(uuidgen)
[ ! -d "./USDSource" ] && git clone https://github.com/PixarAnimationStudios/USD USDSource
python3.7 USDSource/build_scripts/build_usd.py --build-args TBB,extra_inc=big_iron.inc --python --no-imaging --no-usdview --build-monolithic USD
@erikbasargin
erikbasargin / StoreWrapper.swift
Last active February 16, 2021 17:52
Store resolver for MVI architecture pattern in SwiftUI
import struct SwiftUI.ObservedObject
import protocol SwiftUI.ObservableObject
import protocol SwiftUI.DynamicProperty
@available(iOS 14.0, macOS 11.0, tvOS 14.0, watchOS 7.0, *)
@propertyWrapper public struct Store<ObjectType: ObservableObject>: DynamicProperty {
// MARK: - Initialization
public init(name: String? = nil) {
@erikbasargin
erikbasargin / Package.swift
Created December 16, 2020 00:28 — forked from Sorix/Package.swift
Example of Package.swift with environment variables support
// swift-tools-version:4.0
import PackageDescription
#if os(Linux)
import Glibc
#else
import Darwin.C
#endif
enum Enviroment: String {
// generate<A, B>(value:as:)
sil hidden @$s5test28generate5value2asxq__xmtAA12TestProtocolRzSeR_SER_r0_lF : $@convention(thin) <T, Value where T : TestProtocol, Value : Decodable, Value : Encodable> (@in_guaranteed Value, @thick T.Type) -> @out T {
// %0 // user: %9
// %1 // users: %7, %3
// %2 // users: %9, %4
bb0(%0 : $*T, %1 : $*Value, %2 : $@thick T.Type):
debug_value_addr %1 : $*Value, let, name "value", argno 1 // id: %3
debug_value %2 : $@thick T.Type, let, name "type", argno 2 // id: %4
%5 = alloc_stack $Decodable & Encodable // users: %10, %9, %6
%6 = init_existential_addr %5 : $*Decodable & Encodable, $Value // user: %7
open func decode<T : Decodable>(_ type: T.Type, from data: Data) throws -> T {
let topLevel: Any
do {
topLevel = try JSONSerialization.jsonObject(with: data)
} catch {
throw DecodingError.dataCorrupted(DecodingError.Context(codingPath: [], debugDescription: "The given data was not valid JSON.", underlyingError: error))
}
let decoder = __JSONDecoder(referencing: topLevel, options: self.options)
guard let value = try decoder.unbox(topLevel, as: type) else {
protocol TestProtocol {
var info: String { get }
init(from value: Codable)
}
struct Test<T: Codable>: TestProtocol {
let value: T
let info: String
}
struct Test<T>: Codable where T: Codable {
enum CodingKeys: String, CodingKey {
case value
}
let value: T
let info: String
}
extension Test {
@erikbasargin
erikbasargin / palette.stencil
Last active September 30, 2020 13:06
SwiftUI Palette | This is the SwiftGen template just to make Palette.swift file in project.
// swiftlint:disable all
// Generated using SwiftGen — https://github.com/SwiftGen/SwiftGen
//
// Created by Erik Basargin.
//
{% if catalogs and catalogs.count > 0 and resourceCount.color > 0 %}
{% set colorCoreAlias %}{{param.colorCoreAliasName|default:"PaletteCore"}}{% endset %}
{% set colorAlias %}{{param.colorAliasName|default:"Palette"}}{% endset %}
{% set accessModifier %}{% if param.publicAccess %}public{% else %}internal{% endif %}{% endset %}
@erikbasargin
erikbasargin / AnimatableVector.swift
Created June 17, 2020 11:26 — forked from mecid/AnimatableVector.swift
High-performance Animatable Vector for SwiftUI
import SwiftUI
import enum Accelerate.vDSP
struct AnimatableVector: VectorArithmetic {
static var zero = AnimatableVector(values: [0.0])
static func + (lhs: AnimatableVector, rhs: AnimatableVector) -> AnimatableVector {
let count = min(lhs.values.count, rhs.values.count)
return AnimatableVector(values: vDSP.add(lhs.values[0..<count], rhs.values[0..<count]))
}