Skip to content

Instantly share code, notes, and snippets.

View derrickturk's full-sized 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 / badlist.py
Created December 15, 2020 21:17
a bad representation for lists
def cons(x, lis):
return lambda t: x if t else lis
def nil():
return None
def foldr(fn, lis, init):
if lis is None:
return init
head = lis(True)
@derrickturk
derrickturk / windows.hs
Created December 9, 2020 16:40
Sliding-windows in Haskell
takeExactly :: Int -> [a] -> Maybe [a]
takeExactly 0 _ = Just []
takeExactly n [] = Nothing
takeExactly n (x:xs) = (x:) <$> takeExactly (n - 1) xs
windows :: Int -> [a] -> [[a]]
windows n xs = case takeExactly n xs of
Nothing -> []
Just window -> window:windows n (tail xs)
@derrickturk
derrickturk / typecase.idr
Created December 5, 2020 02:07
Idris 2 - looks like typecase is back on the menu, boys!
perverse : {a : Type} -> a -> a
perverse {a = Integer} x = x + 1
perverse {a = String} x = "hello " <+> x
perverse {a = _} x = x
main : IO ()
main = do
printLn $ perverse 5
printLn $ perverse "world"
printLn $ perverse True
@derrickturk
derrickturk / treesoup.py
Last active November 19, 2020 22:03
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
derrickturk / const_or_not.cpp
Created November 16, 2020 16:50
abstracting over const, four ways
// 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 February 1, 2021 18:37
sidecasting with dynamic_cast in C++
#include <iostream>
#include <memory>
#include <vector>
struct base {
virtual ~base() = default;
};
struct capability1 {
virtual void do_capability1() = 0;
@derrickturk
derrickturk / symbol.cpp
Last active October 21, 2020 14:50
Named... things, in C++
#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 October 9, 2020 02:49
Concepts & constraints do what SFINAE does
#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 October 8, 2020 15:34
Arcane vtable hacks in Rust: working with generic traits, dynamically
#![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 October 8, 2020 14:37
We /lightweight dependent types/ now, again, maybe
#![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[..]);