This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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>>, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE LambdaCase, OverloadedStrings #-} | |
import Data.String | |
type Name = String | |
data Expr | |
= V Name | |
| Lam Name Expr | |
| Either Bool Expr -- "False = Left, True = Right" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE GADTs, DeriveFunctor, RankNTypes #-} | |
import Prelude hiding (Monad (..)) | |
main = print () | |
----------------------------------------------------------------------- | |
class Functor m => Monad m where | |
return :: a -> m a |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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...> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
template<typename T, T x> | |
struct Value | |
{ | |
static constexpr T value() { return x; } | |
}; | |
template<int n> | |
using Int = Value<int, n>; | |
template<bool b> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } |
NewerOlder