Skip to content

Instantly share code, notes, and snippets.

@friendlyanon
friendlyanon / clang-toolchain.cmake
Created Sep 22, 2021
Toolchain file to use LLVM Clang on Windows.
View clang-toolchain.cmake
# Path to your LLVM Clang executables, I have mine in the PATH so I just name
# them directly
set(CMAKE_C_COMPILER clang)
set(CMAKE_CXX_COMPILER clang++)
# Make sure CMake doesn't do something silly and just tell it that we are
# compiling for Windows on Windows
set(CMAKE_SYSTEM_NAME Windows)
set(CMAKE_CROSSCOMPILING 0)
View fmap.cpp
template<class T, template<class> class M>
[[nodiscard]] constexpr auto
fmap(const M<T>& x, auto&& f)
noexcept(noexcept(M<decltype(f(*x))>{f(*x)}))
-> M<decltype(f(*x))>
{
if (x) {
return {f(*x)};
} else {
return {};
View chunk.js
function* chunkIterable(iterable, chunkSize) {
const it = iterable[Symbol.iterator]();
for (let i = 0; i !== -1; ) {
const chunk = [];
for (const limit = i + chunkSize; i < limit; ++i) {
const { done, value } = it.next();
if (done) {
i = -1;
break;
}
View scan.js
export function* scan(iterable, reducer, initialValue) {
const it = iterable[Symbol.iterator]();
let accumulator = initialValue;
let index = 0;
if (arguments.length < 3) {
const step = it.next();
if (step.done) {
return;
}
View deep-search.js
function* search(visited, path, object, keys, values) {
for (const key of Reflect.ownKeys(object)) {
const value = object[key];
path.push(key);
if (keys.has(key) || values.has(value)) {
yield [path.slice(), value];
}
if (typeof value === "object" && value != null && !visited.has(value)) {
View ytlt.user.js
// ==UserScript==
// @name YouTube Link Title
// @description Adds video titles, shows previews and embeds on click. Also supported: Vimeo, LiveLeak, Dailymotion, vidme, WorldStarHipHop, Vine, Coub, Streamable
// @namespace http://w9p.co/userscripts/
// @version 2017.2.28
// @author kuehlschrank
// @homepage http://w9p.co/userscripts/ytlt
// @icon https://w9p.co/userscripts/ytlt/icon.png
// @include http*
// @exclude http*//*.google.*/*
View sql.ts
const arrayMarker = Symbol("sql.array");
export interface ArraySqlReplacement {
[arrayMarker]: true;
array: any[];
joiner: string;
}
const isArraySqlReplacement = (value: any): value is ArraySqlReplacement =>
typeof value === "object" && value !== null && value[arrayMarker];
View range.js
export function* range(start, endExclusive, step) {
switch (arguments.length) {
case 1:
endExclusive = start;
start = 0;
case 2:
step = start > endExclusive ? -1 : 1;
}
if (start === endExclusive) {
View trie.ts
// Ideally, Map would have this as a method, but the proposal is still only on
// stage 2: https://github.com/tc39/proposal-upsert
function emplace<K, V>(
map: Map<K, V>,
key: K,
supplier: (key: K) => V,
): V;
function emplace<K, V, This>(
map: Map<K, V>,
key: K,
View groupby.ts
export type AtLeastOne<T> = [T, ...T[]];
export function groupByUsing<V, K extends keyof V, U>(
iterable: Iterable<V>,
key: K | ((value: V, index: number) => V[K]),
callback: (value: V, index: number) => U,
): Map<V[K], AtLeastOne<U>> {
const mapper = typeof key !== "function" ? (v: V) => v[key] : key;
const map = new Map<V[K], AtLeastOne<U>>();