Skip to content

Instantly share code, notes, and snippets.

View nikitamounier's full-sized avatar

Nikita Mounier nikitamounier

View GitHub Profile
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@nikitamounier
nikitamounier / csv.swift
Created January 7, 2022 16:59
CSV <-> Array<OrderedDictionary>
/*
In Computer Science class I was tasked with writing a function takes a raw CSV table and returns a list of OrderedDictionaries and vice versa – but in Python.
It felt messy, convoluted and slow, so I wanted to what it would look like in Swift, especially with Pointfree's swift-parsing library and their experimental parser-builder branch.
This is as high performance as possible while using the swift-parsing library. Consequently, this doesn't support special characters such as é or æ due to the use of UTF8 code units.
*/
import Algorithms
import OrderedCollections
import Parsing
@nikitamounier
nikitamounier / isEven.swift
Created December 21, 2021 23:38
More efficient way of detecting parity
extension BinaryInteger {
@inlinable
var isOdd: Bool {
return withUnsafePointer(to: self & 1) { pointer in
return UnsafeRawPointer(pointer)
.bindMemory(to: Bool.self, capacity: 1)
.pointee
}
}
@nikitamounier
nikitamounier / PredicateSet.swift
Created October 25, 2021 10:18
A simple PredicateSet type, with only one property of type (Element) -> Bool, which (nearly) conforms to SetAlgebra.
struct PredicateSet<Element: Equatable> {
var contains: (Element) -> Bool
init(_ contains: @escaping (Element) -> Bool) {
self.contains = contains
}
}
extension PredicateSet: SetAlgebra {
init() {
@nikitamounier
nikitamounier / FrenchTCA.md
Last active April 6, 2023 08:27
A French translation of The Composable Architecture's README.md

The Composable Architecture

CI

The Composable Architecture (TCA, pour faire court) est une bibliothèque permettant de construire des applications de manière cohérente et compréhensible, en tenant compte de la composition, des tests et de l'ergonomie. Elle peut être utilisée avec SwiftUI, UIKit, et encore, et sur toutes les plateformes Apple (iOS, ma

/// Property wrapper to introduce indirection into your property.
///
/// Indirection is very useful to introduce recursion into `structs`, which don't usually support it. This is very similar to using the `indirect` modifier on enum cases.
///
/// struct Person: Identifiable {
/// let id = UUID()
///
/// @Indirect var father: Person?
/// @Indirect var mother: Person?
/// }
@nikitamounier
nikitamounier / CopyOnWrite.swift
Last active April 3, 2021 16:03
Protocol to easily implement copy-on-write behaviour for your large value types.
protocol Copyable {
func copy() -> Self
}
/// A protocol for value types which gives them copy-on-write behaviour. This means that when multiple variables are pointing to the type, they point to the same underlying data to avoid excessive copying. When one of them modifies the type, it copies the type, therefore keeping value semantics.
@dynamicMemberLookup
protocol CopyOnWrite {
associatedtype Storage: AnyObject & Copyable
/// The underlying storage of the type, which is a reference type.
@nikitamounier
nikitamounier / BuilderPattern.swift
Last active March 4, 2021 17:19
Implementation of builder pattern using Swift's powerful keypaths
/// Simple protocol which types who want to implement builder pattern conform to for free – gives conforming types two simple functions for building
protocol Buildable {}
extension Buildable {
/// Returns a new instance whose property defined by the keypath is set to `value`.
/// - Parameters:
/// - keyPath: `WriteableKeyPath` to the property which shall be modified
/// - value: The value which will be set to the property
///
/// This function is used for types with value semantics.