Skip to content

Instantly share code, notes, and snippets.

View groue's full-sized avatar

Gwendal Roué groue

View GitHub Profile
@groue
groue / Playground.swift
Last active September 28, 2022 15:54
Semaphore.swift
import PlaygroundSupport
PlaygroundPage.current.needsIndefiniteExecution = true
Task {
do {
print("Start, 1 concurrent task")
let semaphore = Semaphore(value: 1)
await withThrowingTaskGroup(of: Void.self) { group in
for i in 0..<10 {
group.addTask {
import SwiftUI
struct MyCollection: RandomAccessCollection {
let startIndex = 0
let endIndex = 1000
subscript(index: Int) -> Int {
print("Access \(index)")
return index
}
}

Explicit Protocol Fulfillment

Introduction

This pitch introduces of a new keyword in front of declarations: conformance.

The conformance keyword means: "this declaration is intended by the programmer to fulfill a protocol requirement". Its presence is never required. However, when present, the program is ill-formed unless the declaration does match a protocol requirement.

For example:

@groue
groue / Database+withDeferredUniqueIndexes.swift
Last active August 23, 2022 14:43
Deferred unique indexes with GRDB
import GRDB
extension Database {
/// Executes the provided closure with deferred unique indexes
func withDeferredUniqueIndexes(_ execute: () throws -> Void) throws {
try inSavepoint {
// Fetch all unique indexes, but primary keys.
let uniqueConstraints: [(table: String, index: IndexInfo)] = try String
.fetchAll(self, sql: "SELECT name FROM sqlite_master WHERE type = 'table'")
.filter { !Database.isGRDBInternalTable($0) }
@groue
groue / ContainerView.swift
Last active April 18, 2022 17:29
Exploring @‍Query initializers
import SwiftUI
/// A Container view which uses various techniques
/// for controlling RequestView.
struct ContainerView: View {
@State var parameter = 0
var body: some View {
List {
Section {
@groue
groue / ScopedFunctions.md
Last active March 13, 2021 15:16
Pitch: Scoped Functions

Pitch: Scoped Functions

Introduction

Scoped Functions are functions that enhance the leading dot syntax inside their body, so that it gives short-hand access to the members of a specific value. Such functions exist so that API designers and their consumers can collaborate towards a way to write focused code where an agreed implicit and obvious context does not have to be spelled out.

Motivation

This pitch considers that developers often want to use short-hand identifier resolution in particular contexts, and api designers often want to make it possible.

@groue
groue / SinglePublisher.swift
Last active October 8, 2021 13:34
SinglePublisher
import Combine
import Foundation
// MARK: - SinglePublisher
/// The protocol for "single publishers", which publish exactly one element, or
/// an error.
///
/// `Just`, `Future` and `URLSession.DataTaskPublisher` are examples of
/// publishers that conform to `SinglePublisher`.
extension Collection {
/// Returns an array of subsequences of maximum size `chunkSize`.
///
/// For example:
///
/// // ["ABCD", "EFGH", "IJ"]
/// "ABCDEFGHIJ".split(chunkSize: 4)
///
/// - parameter chunkSize: the maximum size of the returned subsequences.
/// - precondition: chunkSize > 0
@groue
groue / Combine+Weak.swift
Created June 15, 2020 08:09
Combine assignWeakly
import Combine
extension Publisher where Failure == Never {
/// Same as assign(to:on:), but root object is not retained.
func assignWeakly<Root: AnyObject>(
to keyPath: ReferenceWritableKeyPath<Root, Self.Output>,
on object: Root)
-> AnyCancellable
{
var cancellable: AnyCancellable?
@groue
groue / GRDB+DeleteAllOther.swift
Last active June 15, 2020 15:14
GRDB request.deleteAllOther(db)
import GRDB
extension QueryInterfaceRequest where RowDecoder: MutablePersistableRecord {
/// Deletes all records in the database, but the ones selected by the request.
///
/// For example:
///
/// // Delete all players whose score is below 1000
/// let bestPlayers = Player.filter(Column("score") > 1000)
/// try dbQueue.write(bestPlayers.deleteAllOther)