Skip to content

Instantly share code, notes, and snippets.

Avatar

Gábor Lehel glaebhoerl

  • Hungary
View GitHub Profile
@glaebhoerl
glaebhoerl / scopemap.rs
Last active Apr 22, 2021
Rust hash table with efficient support for nested scopes (save/restore)
View scopemap.rs
// Based on idea: https://twitter.com/pkhuong/status/1287510400372748290
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 https://gist.github.com/glaebhoerl/466267f0c977cef74202f167d6493cc0 and tweets https://twitter.com/glaebhoerl/status/1129851506067427329
-- 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
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
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 http://stackoverflow.com/questions/12735274/breaking-data-set-integrity-without-generalizednewtypederiving/12744568#12744568
-- Discussion on haskell-cafe: http://thread.gmane.org/gmane.comp.lang.haskell.cafe/100870
-- http://www.haskell.org/pipermail/haskell-cafe/2012-October/103984.html
-- 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
Parts:
* 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 }