Skip to content

Instantly share code, notes, and snippets.

View alexito4's full-sized avatar

Alejandro Martínez alexito4

View GitHub Profile
@lattner
lattner / TaskConcurrencyManifesto.md
Last active May 1, 2024 15:48
Swift Concurrency Manifesto
@tclementdev
tclementdev / libdispatch-efficiency-tips.md
Last active April 26, 2024 10:15
Making efficient use of the libdispatch (GCD)

libdispatch efficiency tips

The libdispatch is one of the most misused API due to the way it was presented to us when it was introduced and for many years after that, and due to the confusing documentation and API. This page is a compilation of important things to know if you're going to use this library. Many references are available at the end of this document pointing to comments from Apple's very own libdispatch maintainer (Pierre Habouzit).

My take-aways are:

  • You should create very few, long-lived, well-defined queues. These queues should be seen as execution contexts in your program (gui, background work, ...) that benefit from executing in parallel. An important thing to note is that if these queues are all active at once, you will get as many threads running. In most apps, you probably do not need to create more than 3 or 4 queues.

  • Go serial first, and as you find performance bottle necks, measure why, and if concurrency helps, apply with care, always validating under system pressure. Reuse

@AliSoftware
AliSoftware / Bindings.swift
Last active April 24, 2024 01:10
Re-implementation of @binding and @State (from SwiftUI) myself to better understand it
/*:
This is a concept re-implementation of the @Binding and @State property wrappers from SwiftUI
The only purpose of this code is to implement those wrappers myself
just to understand how they work internally and why they are needed,
⚠️ This is not supposed to be a reference implementation nor cover all
subtleties of the real Binding and State types.
The only purpose of this playground is to show how re-implementing
them myself has helped me understand the whole thing better
@lattner
lattner / async_swift_proposal.md
Last active April 21, 2024 09:43 — forked from oleganza/async_swift_proposal.md
Concrete proposal for async semantics in Swift

Async/Await for Swift

Introduction

Modern Cocoa development involves a lot of asynchronous programming using closures and completion handlers, but these APIs are hard to use. This gets particularly problematic when many asynchronous operations are used, error handling is required, or control flow between asynchronous calls gets complicated. This proposal describes a language extension to make this a lot more natural and less error prone.

This paper introduces a first class Coroutine model to Swift. Functions can opt into to being async, allowing the programmer to compose complex logic involving asynchronous operations, leaving the compiler in charge of producing the necessary closures and state machines to implement that logic.

@christianselig
christianselig / Locale+SFSymbol.swift
Created September 3, 2021 21:54
Returns an SF Symbol currency image that match's the device's current locale, for instance dollar in North America, Indian rupee in India, etc.
extension Locale {
/// Returns an SF Symbol currency image that match's the device's current locale, for instance dollar in North America, Indian rupee in India, etc.
func currencySFSymbol(filled: Bool, withConfiguration configuration: UIImage.Configuration? = nil) -> UIImage {
// Default currency symbol will be the Animal Crossing Leaf coin 􁂬 to remain impartial to any specific country
let defaultSymbol = UIImage(systemName: "leaf.circle\(filled ? ".fill" : "")")!
guard let currencySymbolName = currencySymbolNameForSFSymbols() else { return defaultSymbol }
let systemName = "\(currencySymbolName).circle\(filled ? ".fill" : "")"
return UIImage(systemName: systemName, withConfiguration: configuration) ?? defaultSymbol
@DougGregor
DougGregor / parallel_map.swift
Created December 24, 2020 01:10
Swift async/await implementation of a parallel map
extension Collection {
func parallelMap<T>(
parallelism requestedParallelism: Int? = nil,
_ transform: @escaping (Element) async throws -> T
) async throws -> [T] {
let defaultParallelism = 2
let parallelism = requestedParallelism ?? defaultParallelism
let n = self.count
if n == 0 {

2015-01-29 Unofficial Relay FAQ

Compilation of questions and answers about Relay from React.js Conf.

Disclaimer: I work on Relay at Facebook. Relay is a complex system on which we're iterating aggressively. I'll do my best here to provide accurate, useful answers, but the details are subject to change. I may also be wrong. Feedback and additional questions are welcome.

What is Relay?

Relay is a new framework from Facebook that provides data-fetching functionality for React applications. It was announced at React.js Conf (January 2015).

@nicklockwood
nicklockwood / OSKit.swift
Created January 28, 2023 11:32
A lightweight approach to writing cross-platform code in SwiftUI without a lot of conditional compilation blocks
import SwiftUI
enum OSDocumentError: Error {
case unknownFileFormat
}
#if canImport(UIKit)
import UIKit
// SwiftUI Custom Styles (TripleToggleStyle)
// https://swiftui-lab.com
// https://swiftui-lab.com/custom-styling
import SwiftUI
// MARK: - TripleToggle View
public struct TripleToggle: View {
@Environment(\.tripleToggleStyle) var style: AnyTripleToggleStyle
@chriseidhof
chriseidhof / ContentView.swift
Last active March 27, 2024 19:14
Variadic Views
import SwiftUI
struct MyValue: _ViewTraitKey {
static var defaultValue: Int = 0
}
extension View {
func myValue(_ value: Int) -> some View {
_trait(MyValue.self, value)
}