Skip to content

Instantly share code, notes, and snippets.


Gábor Lehel glaebhoerl

  • Hungary
View GitHub Profile
glaebhoerl /
Last active Apr 22, 2021
Rust hash table with efficient support for nested scopes (save/restore)
// Based on idea:
use hashbrown::raw::RawTable;
pub struct ScopeMap<K, V> {
last_scope_id: ScopeId,
scopes: Vec<ScopeId>, // values are zeroed instead of popped to save a check in get() / is_fresh()
current_scope: ScopeDepth, // index of innermost valid scope
values: RawTable<Entry<K, V>>,
shadowed: Vec<Shadowed<K, V>>,
View UTLC++.hs
-- see also previous gist and tweets
-- here we accomplish the same thing but in a totally different way, by relying on the host language for basically everything
{-# LANGUAGE LambdaCase, GADTs, TypeOperators, DataKinds, PolyKinds, Strict, DeriveFunctor, RankNTypes, TypeFamilies, ConstraintKinds #-}
import Prelude hiding (product, sum)
import Data.Type.Equality
import GHC.Exts (Constraint)
View UTLC++.hs
{-# LANGUAGE LambdaCase, OverloadedStrings #-}
import Data.String
type Name = String
data Expr
= V Name
| Lam Name Expr
| Either Bool Expr -- "False = Left, True = Right"
View FreerCofreer.hs
{-# LANGUAGE GADTs, DeriveFunctor, RankNTypes #-}
import Prelude hiding (Monad (..))
main = print ()
class Functor m => Monad m where
return :: a -> m a
View Person.hs
module Person (Person, null, newPerson, isNull, getAge, getName) where
-- constructor is not exported! only this module has access to the internal Maybe
newtype Person = Person (Maybe (Int, String))
null :: Person
null = Person Nothing
newPerson :: Int -> String -> Person
newPerson age name = Person (Just (age, name))
glaebhoerl / gist:056051916220a099eb4d
Created Nov 29, 2014
Fn, FnMut, FnOnce hierarchy in C++
View gist:056051916220a099eb4d
template<typename Return, typename... Arguments>
struct FnOnce
using This = FnOnce<Return, Arguments...>;
virtual Return operator ()(Arguments&&...) && = 0;
template<typename Return, typename... Arguments>
struct FnMut: FnOnce<Return, Arguments...>
glaebhoerl / lambda-template-unary.cpp
Created Nov 29, 2014
Lazily evaluated type-level lambda calculus in C++
View lambda-template-unary.cpp
template<typename T, T x>
struct Value
static constexpr T value() { return x; }
template<int n>
using Int = Value<int, n>;
template<bool b>
View A.hs
-- Code taken from
-- Discussion on haskell-cafe:
-- Modified to remove orphan instances by rwbarton
-- Simplified by glaebhoerl
module A (A(..), Set, empty, insert, on) where
import Data.Set
import Data.Function
View gist:8795f00d90bec80bc400
## Exceptions
* union types / restricted Any
* match-on-type
* `throws Type`, `throw val`
* `try!`
Union types:
Either<A, B, C, D>
Can be any of A, B, C, or D
View gist:57d50d57be6579c00849
trait ToInt {
fn to_int(&self) -> int { 666 }
struct A<'a> {
a: &'a Box<int>
impl<'a> ToInt for A<'a> {
fn to_int(&self) -> int { **self.a }