Skip to content

Instantly share code, notes, and snippets.

View chriseidhof's full-sized avatar

Chris Eidhof chriseidhof

View GitHub Profile
@chriseidhof
chriseidhof / AsyncZipped.swift
Last active October 23, 2023 20:41
Async Zipped
/*
Make sure to compile this with the following flags:
-Xfrontend -warn-concurrency -Xfrontend -enable-actor-data-race-checks
*/
extension AsyncIteratorProtocol {
func newAndNext() async throws -> (Self, Element)? {
@chriseidhof
chriseidhof / gist.swift
Last active December 6, 2021 05:29
AsyncZipped
struct AsyncZippedSequence<L, R>: AsyncSequence where L: AsyncSequence, R: AsyncSequence {
typealias Element = (L.Element, R.Element)
var l: L
var r: R
func makeAsyncIterator() -> AsyncZippedIterator<L.Element, R.Element> {
AsyncZippedIterator(l, r)
}
}
import SwiftUI
struct ContentView: View {
@State var cond = false
var body: some View {
ZStack {
Color.blue
Test(cond: cond).onTapGesture {
withAnimation(.easeInOut(duration: 2)) {
cond.toggle()
import SwiftUI
extension View {
func pipe<R>(@ViewBuilder _ f: (Self) -> R) -> R {
f(self)
}
}
struct ContentView: View {
@State var flag: Bool = false
@chriseidhof
chriseidhof / Package.swift
Created June 29, 2021 09:42
Toml To Yaml
// swift-tools-version:5.3
// The swift-tools-version declares the minimum version of Swift required to build this package.
import PackageDescription
let package = Package(
name: "TomlToYaml",
products: [
// Products define the executables and libraries a package produces, and make them visible to other packages.
.executable(
import SwiftUI
import Yams
extension String {
var yamlToJSON: String {
do {
guard let parsed = try Yams.load(yaml: self) else { return "" }
let data = try JSONSerialization.data(withJSONObject: parsed, options: [.sortedKeys, .prettyPrinted])
return String(decoding: data, as: UTF8.self)
} catch {
// Simplified version of https://gist.github.com/anandabits/d9494d14fef221983ff4f1cafa318d47#file-areequatablyequal-swift
func isEqual(x: Any, y: Any) -> Bool {
func f<LHS>(_ lhs: LHS) -> Bool {
let p = Wrapper<LHS>.self as? AnyEquatable.Type
return p?.isEqual(x, y) ?? false
}
return _openExistential(x, do: f)
}
/// Takes an absolute path and fits it inside the proposed rectangle.
struct FittingShape: Shape {
var absolutePath: Path
func path(in rect: CGRect) -> Path {
let p = absolutePath
let boundingRect = p.boundingRect
let scale = min(rect.width/boundingRect.width, rect.height/boundingRect.height)
let scaled = p.applying(.init(scaleX: scale, y: scale))
let scaledBoundingRect = scaled.boundingRect
@chriseidhof
chriseidhof / ContentView.swift
Created February 20, 2021 17:19 — forked from steipete/ContentView.swift
FB9013209: SwiftUI: Preview and actual layout differ in this example. (SwiftUI Bug)
//
// ContentView.swift
// Shared
//
// Created by Peter Steinberger on 20.02.21.
//
import SwiftUI
struct SettingsGroup<Content: View>: View {
//: A Cocoa based Playground to present user interface
import SwiftUI
struct ContentView: View {
var body: some View {
HStack {
Text("Highlight :")
.background(Color.green)
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topTrailing)