Skip to content

Instantly share code, notes, and snippets.

View kyouko-taiga's full-sized avatar

Dimi Racordon kyouko-taiga

View GitHub Profile
@kyouko-taiga
kyouko-taiga / main.md
Last active February 1, 2024 21:31
Generic programming with type classes

Generic programming with type classes

Generic programming is a discipline that encourages the construction of abstractions and the reuse of software component without loss of efficiency. Subtyping through inheritance can be adversary to this goal because it encourages type erasure, promising that efficiency is maintained thanks to dynamic dispatch. Sadly, this promise gets broken when generic data structures and algorithms depend on more than one generic receiver. In those instances, one must resort to generic parameters to preserve type information, introducing expensive annotation costs. This short article examine this problem and then discusses how type classes, an alternative approach to practice generic programming, can address many of the issues presented by subtyping through inheritance.

Disclaimer:

@kyouko-taiga
kyouko-taiga / Collection.val
Created December 6, 2022 20:17
Iterators in Val
/// A type representing a collection of values.
public trait Collection {
/// The type of the `Self`'s elements.
type Element
/// The type of a position in `Self`.
type Index: Equatable
/// Returns `self`'s first position.
@kyouko-taiga
kyouko-taiga / Slab.swift
Created September 21, 2021 22:30
Draft of slab allocator in Swift
import Darwin
/// A slab allocator, implemented as a collection of "arrays with tombstones".
///
/// It's a kind of array with stable indices.
public struct Slab<T> {
public struct Index {
fileprivate let bucket: Int
@kyouko-taiga
kyouko-taiga / dumb.swift
Last active July 13, 2018 06:29
How to do dumb things with Swift
infix operator ➡️: AdditionPrecedence
enum 🖇<🤖>: Collection, CustomStringConvertible {
indirect case 📎(🤖, 🖇)
case 🚫
func makeIterator() -> AnyIterator<🤖> {
var 📌 = self
return AnyIterator {
@kyouko-taiga
kyouko-taiga / wrapper.py
Last active August 9, 2023 06:05
A python class wrapper to instrument/override properties
# This snippet was written by Dimitri Racordon (kyouko.taiga@gmail.com)
#
# Copyright (c) 2015 Dimitri Racordon
# Licensed under the The MIT License (MIT).
class lazy(object):
# This class is heavily inspired by the werkzeug.utils.cached_property
# decorator. It transforms a class method to a lazy property, evaluated
# the first time the property is accessed.