Skip to content

Instantly share code, notes, and snippets.

View inamiy's full-sized avatar

Yasuhiro Inami inamiy

View GitHub Profile

impl Trait is Always Existential

Recently, Rust 1.26 was released, and with it came stable access to a heavily desired feature: impl Trait. For those unfamiliar, impl Trait lets you write something like this:

fn foo<'a, A, B>(x: &'a mut A) -> impl Bar<'a, B> { ... }

and have it be translated to something like this:

@rlxone
rlxone / CoreAudio.swift
Last active January 13, 2025 03:18
CoreAudio output device useful methods in Swift 4
/*
static func getOutputDevices() -> [AudioDeviceID: String]?
static func isOutputDevice(deviceID: AudioDeviceID) -> Bool
static func getAggregateDeviceSubDeviceList(deviceID: AudioDeviceID) -> [AudioDeviceID]
static func isAggregateDevice(deviceID: AudioDeviceID) -> Bool
static func setDeviceVolume(deviceID: AudioDeviceID, leftChannelLevel: Float, rightChannelLevel: Float)
static func setOutputDevice(newDeviceID: AudioDeviceID)
static func getDeviceVolume(deviceID: AudioDeviceID) -> [Float]
static func getDefaultOutputDevice() -> AudioDeviceID
*/
@chriseidhof
chriseidhof / finaltagless.swift
Last active March 20, 2019 23:16
Extensible Commands in TEA
import Foundation
// Let's use a Command protocol...
protocol Command {
associatedtype Action
static func modalAlert(title: String, accept: String) -> Self
static func request(_ request: URLRequest, available: @escaping (Data?) -> Action) -> Self
}
extension Optional: Sequence {
public func makeIterator() -> Iterator {
return Iterator(optional: self)
}
public struct Iterator: IteratorProtocol {
var optional: Wrapped?
public mutating func next() -> Wrapped? {
defer { optional = nil }
import Foundation
enum Either<A,B> {
case left(A)
case right(B)
}
// Works only using Swift 4.1
extension Either: Codable where A: Codable, B: Codable {
enum CodingKeys: CodingKey {
@bkase
bkase / monoidal-cancellation.swift
Created January 5, 2018 06:08
Monoidal Cancellations (ported from Swift Sandbox)
// Write some awesome Swift code, or import libraries like "Foundation",
// "Dispatch", or "Glibc"
struct Monoid<T> {
let empty: T
let append: (T, T) -> T
func fold(_ arr: [T]) -> T {
return arr.reduce(empty, append)
}
@bkase
bkase / algebraic-graphs.swift
Created January 5, 2018 05:58
Playing with Algebraic Graphs (port from Swift Sandbox)
// Paper located: https://github.com/snowleopard/alga-paper
infix operator <>: AdditionPrecedence
protocol Semigroup {
static func <>(lhs: Self, rhs: Self) -> Self
}
protocol Monoid: Semigroup {
static var empty: Self { get }
}
protocol CommutativeMonoid: Monoid { }
protocol BoundedSemilattice: CommutativeMonoid { }
//
// main.swift
// ExtensibleEffects
//
// Created by Chris Eidhof on 02.01.18.
// Copyright © 2018 objc.io. All rights reserved.
//
import Foundation
@kazuho
kazuho / git-blame-pr.pl
Last active June 28, 2022 07:15
git-blame by PR #
#! /usr/bin/perl
#
# Written in 2017 by Kazuho Oku
#
# To the extent possible under law, the author(s) have dedicated all copyright and related and neighboring rights to this software to the public domain worldwide. This software is distributed without any warranty.
# You should have received a copy of the CC0 Public Domain Dedication along with this software. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
#
use strict;
use warnings;
@sebmarkbage
sebmarkbage / Infrastructure.js
Last active February 26, 2025 13:57
SynchronousAsync.js
let cache = new Map();
let pending = new Map();
function fetchTextSync(url) {
if (cache.has(url)) {
return cache.get(url);
}
if (pending.has(url)) {
throw pending.get(url);
}