Skip to content

Instantly share code, notes, and snippets.

@EkremDincel
EkremDincel / writeup.md
Created December 8, 2020 19:25 — forked from edmundsmith/writeup.md
Method for Emulating Higher-Kinded Types in Rust

Method for Emulating Higher-Kinded Types in Rust

Intro

I've been fiddling about with an idea lately, looking at how higher-kinded types can be represented in such a way that we can reason with them in Rust here and now, without having to wait a couple years for what would be a significant change to the language and compiler.

There have been multiple discussions on introducing higher-ranked polymorphism into Rust, using Haskell-style Higher-Kinded Types (HKTs) or Scala-looking Generalised Associated Types (GATs). The benefit of higher-ranked polymorphism is to allow higher-level, richer abstractions and pattern expression than just the rank-1 polymorphism we have today.

As an example, currently we can express this type:

@EkremDincel
EkremDincel / new_array.rs
Created May 4, 2021 23:10
A safe way of creating and initializing huge arrays on the heap in Rust.
#![feature(allocator_api)]
use core::alloc::Layout;
use std::alloc::Allocator;
use std::alloc::Global;
fn new_array<T, const N: usize>(mut initializer: impl FnMut() -> T) -> Box<[T; N]> {
// allocate the array with default allocator by using low level API
let ptr: *mut T = Global
.allocate(Layout::new::<[T; N]>())
// panic on Out of Memory
def windows(sequence, step):
i = 0
while i < len(sequence):
yield sequence[i: i + step]
i += step
def filled_windows(sequence, step):
i = 0
while i + step <= len(sequence):
yield sequence[i: i + step]
@EkremDincel
EkremDincel / profile.py
Created July 11, 2021 10:20
Basic Python profiler
from contextlib import contextmanager
from timeit import default_timer as timer
try:
import matplotlib
except ModuleNotFoundError:
PLOT = False
else:
PLOT = True
class Profiler: