Skip to content

Instantly share code, notes, and snippets.

View cleoold's full-sized avatar
🤔
thonking

midori cleoold

🤔
thonking
View GitHub Profile
@cleoold
cleoold / main.cpp
Created October 28, 2023 02:32
c++ coroutine intro
#include <coroutine>
#include <format>
#include <deque>
#include <optional>
using namespace std;
deque<coroutine_handle<>> queue;
template<class T>
@cleoold
cleoold / convert.py
Created March 24, 2021 02:47
Convert type annotations to new style
# requires Python 3.9+ for unparse()
import ast
from typing import cast
class MyTransformer(ast.NodeTransformer):
def visit_Subscript(self, node: ast.Subscript) -> ast.AST:
result = node
name = cast(ast.Name, node.value)
@cleoold
cleoold / enumerable_select.py
Last active December 31, 2020 07:34
example overloading callable type based on its argument type (tested on py 3.7+)
from __future__ import annotations
from inspect import getattr_static, isclass
from typing import Callable, Generic, Iterable, List, TypeVar, overload
TSource_co = TypeVar('TSource_co', covariant=True)
TResult = TypeVar('TResult')
class Enumerable(Generic[TSource_co]):
def __init__(self, iter_: Iterable[TSource_co]) -> None:
super().__init__()
@cleoold
cleoold / maybetest.cpp
Created December 2, 2020 06:52
maybe type with funny operator overloading
#include <iostream>
#include <string>
#include <variant>
#include <tuple>
// https://gist.github.com/cleoold/df31c3789f2b2b8fe2cfa5022957ee06
#include "lambda_traits.hpp"
template<class T>
struct decay_maybe { using type = T; };
@cleoold
cleoold / bitstring.ts
Created November 9, 2020 04:34
bitstring manip in typescript type system
/**
* Binary string and bitwise arithmetic entirely in TypeScript's type system
* (with template string types).
*
* Requires version >= 4.1.0
*
* Hover on the types to see the results.
*/
type A = '010101'
@cleoold
cleoold / tupleop.h
Created October 17, 2020 19:32
std::tuple map and foldl
// c++17
#pragma once
#include <tuple>
#include <utility>
namespace detail {
template<class T, class F, size_t ...N>
auto constexpr tuple_map_impl(const T &t, F &&f, std::index_sequence<N...>) {
return std::make_tuple(std::forward<F>(f)(std::get<N>(t))...);
@cleoold
cleoold / mynonpromise.ts
Last active September 3, 2020 06:16
basic javascript promise implementation
// implements ultra simple mypromise
type Nullable<T> = T | null;
function defaultReject(err: any) {
throw Error(`uncaught promise rejection: ${err}`);
}
class MyPromise<T> {
@cleoold
cleoold / test.cpp
Created September 3, 2020 00:08
demo: open 3 Lua states and let them interact. Two of them run infinite loops and the other notifies them via c function to stop looping
// g++ test.cpp -lpthread -llua5.3 -Wall
#include <array>
#include <atomic>
#include <chrono>
#include <future>
#include <iostream>
#include "lua.hpp"
namespace {
@cleoold
cleoold / test.cpp
Created August 30, 2020 07:31
switch case style` std::conditional`-like thing for pattern matching
// match case for c++11
// avoids nested brackets as in std::conditional
template<bool Cond, class Tp>
struct Case {};
template<class ...Cases>
struct Match;
template<class Case_1, class ...Cases>
@cleoold
cleoold / thread_raii.hpp
Last active April 23, 2021 02:14
raii safe c++ thread
#pragma once
// source: Scott Meyers - Effective Modern C++-O'Reilly item 37
// compile flag: -lpthread -Wall -std=c++20
#include <thread>
#include <type_traits>
// RAII managed thread class
class thread_raii {
public: