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 / treesoup.py
Last active Nov 19, 2020
walking the tree of soup
View treesoup.py
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
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
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
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
You can’t perform that action at this time.