Skip to content

Instantly share code, notes, and snippets.

Avatar
💭
(Come in under the shadow of this red rock)

Derrick Turk derrickturk

💭
(Come in under the shadow of this red rock)
View GitHub Profile
@derrickturk
derrickturk / symbol.cpp
Last active Oct 21, 2020
Named... things, in C++
View symbol.cpp
#include <cstddef>
#include <iostream>
template<std::size_t N>
struct symbol {
char name[N];
constexpr symbol(const char (&str)[N]) noexcept
{
for (std::size_t i = 0; i < N; ++i)
@derrickturk
derrickturk / concept_sfinae.cpp
Created Oct 9, 2020
Concepts & constraints do what SFINAE does
View concept_sfinae.cpp
#include <iostream>
/* are concept constraint violations like SFINAE "errors", or like, uh...
* ... the other kind?
* they're like SFINAE errors! constraint violations take function templates
* out of consideration for overloading, but are not errors.
*/
template<class T>
concept fugastic = requires {
@derrickturk
derrickturk / arcane_vtable_hack.rs
Created Oct 8, 2020
Arcane vtable hacks in Rust: working with generic traits, dynamically
View arcane_vtable_hack.rs
#![feature(raw)]
// tested with rustc 1.49.0-nightly (91a79fb29 2020-10-07)
use std::{
any::{Any, TypeId},
collections::HashMap,
mem,
raw::TraitObject,
};
@derrickturk
derrickturk / const_generic_arithmetic.rs
Last active Oct 8, 2020
We /lightweight dependent types/ now, again, maybe
View const_generic_arithmetic.rs
#![feature(const_generics, const_evaluatable_checked)]
#![allow(incomplete_features)]
// tested with rustc 1.49.0-nightly (91a79fb29 2020-10-07)
fn append<T: Copy + Default, const N: usize, const M: usize>(
xs: &[T; N], ys: &[T; M]) -> [T; N + M] {
let mut result = [T::default(); N + M];
result[..N].copy_from_slice(&xs[..]);
result[N..].copy_from_slice(&ys[..]);
@derrickturk
derrickturk / cpp_adders.cpp
Last active Oct 4, 2020
messing around with concepts and type erasure in C++20
View cpp_adders.cpp
#include <exception>
#include <iostream>
#include <utility>
#include <memory>
#include <vector>
#include <concepts>
template<class T> concept IntFn = requires (T t) {
{t(0)} -> std::same_as<int>;
};
@derrickturk
derrickturk / hlist.rs
Created Oct 4, 2020
Well-typed (ish) HLists and named tuples in Rust, with const generics
View hlist.rs
use std::marker::PhantomData;
pub trait Ix { }
pub trait HList { }
pub trait Ixed<T, N> {
fn get(&self) -> &T;
fn get_mut(&mut self) -> &mut T;
}
@derrickturk
derrickturk / Cargo.toml
Created Sep 22, 2020
A minimal Python module in Rust (move lib.rs to src/lib.rs); rename target/{profile}/rustpy.dll to rustpy.pyd.
View Cargo.toml
[package]
name = "rustpy"
version = "0.1.0"
authors = ["Derrick W. Turk <dwt@terminusdatascience.com>"]
edition = "2018"
[lib]
crate-type = ["cdylib"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
@derrickturk
derrickturk / Makefile
Last active Sep 22, 2020
A minimal(?) Python module in C++, with MinGW makefile
View Makefile
CXX=g++
CXXOPTS=-Wall -pedantic -std=c++2a -O2 -static
PYROOT=C:/Users/Derrick/local
PYVER=36
PYINCLUDEDIR=$(PYROOT)/Python$(PYVER)/include
PYLIBDIR=$(PYROOT)/Python$(PYVER)/libs
ALL: cpppytest.pyd cpppyobj.pyd
%.pyd: %.cpp
@derrickturk
derrickturk / Makefile
Last active Sep 22, 2020
A minimal Python module in C, with MinGW makefile
View Makefile
CC=gcc
COPTS=-Wall -pedantic -std=c99 -O2 -static
PYROOT=C:/Users/Derrick/local
PYVER=36
PYINCLUDEDIR=$(PYROOT)/Python$(PYVER)/include
PYLIBDIR=$(PYROOT)/Python$(PYVER)/libs
cpytest.pyd: cpytest.c
$(CC) $(COPTS) -shared -I$(PYINCLUDEDIR) -L$(PYLIBDIR) -o $@ $^ -lpython$(PYVER)
@derrickturk
derrickturk / model_context.py
Created Sep 12, 2020
A simpler version of PyMC3's questionable idea
View model_context.py
import sys
from typing import ClassVar, Dict, List, Type
class Model:
_ctx: 'ClassVar[List[Model]]' = list()
def __init__(self):
self.nodes: 'Dict[Node]' = dict()
def __enter__(self):
You can’t perform that action at this time.