Skip to content

Instantly share code, notes, and snippets.

(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 /
Last active Nov 19, 2020
walking the tree of soup
import requests
import sys
from bs4 import BeautifulSoup # type: ignore
from bs4.element import Tag, NavigableString # type: ignore
from typing import Callable, Dict, List
def retrieve(url: str) -> BeautifulSoup:
rq = requests.get(url)
rq.encoding = 'utf-8'
derrickturk / const_or_not.cpp
Created Nov 16, 2020
abstracting over const, four ways
View const_or_not.cpp
// this is annoying in every language with a const-like
#include <cstddef>
#include <iostream>
#include <stdexcept>
#include <type_traits>
// just duplicate...
class A {
int xs_[10] = { };
derrickturk / mi_dynamic.cpp
Last active Nov 10, 2020
sidecasting with dynamic_cast in C++
View mi_dynamic.cpp
#include <iostream>
#include <memory>
#include <vector>
struct base {
virtual ~base() = default;
struct capability1 {
virtual void do_capability1() = 0;
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 / 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 /
Created Oct 8, 2020
Arcane vtable hacks in Rust: working with generic traits, dynamically
// tested with rustc 1.49.0-nightly (91a79fb29 2020-10-07)
use std::{
any::{Any, TypeId},
derrickturk /
Last active Oct 8, 2020
We /lightweight dependent types/ now, again, maybe
#![feature(const_generics, const_evaluatable_checked)]
// 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];
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 /
Created Oct 4, 2020
Well-typed (ish) HLists and named tuples in Rust, with const generics
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 / Cargo.toml
Created Sep 22, 2020
A minimal Python module in Rust (move to src/; rename target/{profile}/rustpy.dll to rustpy.pyd.
View Cargo.toml
name = "rustpy"
version = "0.1.0"
authors = ["Derrick W. Turk <>"]
edition = "2018"
crate-type = ["cdylib"]
# See more keys and their definitions at
You can’t perform that action at this time.