Skip to content

Instantly share code, notes, and snippets.

@DarinM223
DarinM223 / hkt.rs
Last active September 27, 2019 09:23
Example of higher kinded types in Rust
pub trait Unplug {
type Gen;
type A;
}
pub trait Plug<A> {
type Out: Unplug<A = A>;
}
pub trait Functor: Unplug + Plug<<Self as Unplug>::A> {
@DarinM223
DarinM223 / records.md
Last active September 13, 2019 11:08
Various methods of reaching into records in Haskell

There are a few ways of reaching into records that each have their own advantages and drawbacks. I keep reevaluating these methods every time I want to reach into a record so I'm writing down the cons for each method so I can remember them later. Most of these methods will be in the context of storing multiple effects, because that is the most common reason for me to want to reach into a record without caring about the record type.

Has Typeclasses

The simplest way of reaching into a record is to use a Has*** typeclass. For example if I have an effectful record of functions UserRepo m, then a typeclass to reach into an arbitrary record and pull out a UserRepo m would

@DarinM223
DarinM223 / ModifyEffects.hs
Created August 25, 2019 10:18
Modifying effects in a loop dynamically
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE TemplateHaskell #-}
module Main where
import Control.Lens
import Control.Monad.State
import Data.IORef
@DarinM223
DarinM223 / CMakeLists.txt
Created March 16, 2019 00:01
Sample usages of C++17
cmake_minimum_required(VERSION 3.12)
project(Test)
add_executable(TestExe main.cpp)
target_compile_options(TestExe PRIVATE -Wall -Werror -Wno-missing-braces -g)
set_target_properties(TestExe PROPERTIES CXX_STANDARD 17)
@DarinM223
DarinM223 / ReverseInt.hs
Last active March 4, 2019 08:59
Reverse an integer in Haskell
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE GADTs #-}
module ReverseInt where
import Data.Foldable
data MyNum a where
MyNum :: (Num a, Integral a) => a -> MyNum a
instance Foldable MyNum where
@DarinM223
DarinM223 / typeclass_problem.md
Last active January 26, 2019 06:20
A limitation of typeclasses

Its common to refer to Haskell's typeclasses as a more powerful version of traits in other languages. However, because Haskell is pure and keeps track of effects in the type system, certain traits in other languages can't be modeled as easily with typeclasses. For example, in Rust you can write a trait describing a store that performs side effects:

pub trait Store<K, V> {
    fn get(&self, key: K) -> Option<V>;
    fn set(&mut self, key: K, value: V);
@DarinM223
DarinM223 / Main.hs
Last active November 30, 2018 09:27
Some haskell code inspired by Rich Hickey's Maybe Not
{-# LANGUAGE UndecidableInstances #-}
module Main where
-- Warning: do not do this in real Haskell code!
-- It's way simpler to just refactor your code instead of
-- going through hoops to try to avoid "breaking" changes.
import Control.Lens
import Data.Generics.Sum
@DarinM223
DarinM223 / setup.md
Last active December 5, 2023 22:06
Setting up a simple Reflex GHCJS project

Setting up a Reflex GHCJS Project

This is a simple tutorial that doesn't go deep into nix.

Step 1. Create new project

Use mkdir to create a new folder and cabal init to initialize a new cabal project.

@DarinM223
DarinM223 / effectful_haskell.md
Last active January 8, 2019 12:10
Effectful Haskell tips

Tips I found useful for writing effectful Haskell

Here are some tips I found useful for writing Haskell code that abstracts over effects, along with the articles that describe them.

  1. Main transformer should be a newtype around ReaderT Config IO, where Config is a record with all the mutable references to everything needed for the application.
@DarinM223
DarinM223 / MonadErrorComposed.hs
Last active August 29, 2018 03:28
Simple example of "composing" MonadErrors
data SmallError = Error1 | Error2 | Error3 deriving (Show, Eq)
data MediumError = Error4 | Error5 | Error6 deriving (Show, Eq)
class HasSmallError e where
fromSmallError :: SmallError -> e
toSmallError :: e -> Maybe SmallError
class HasMediumError e where
fromMediumError :: MediumError -> e
toMediumError :: e -> Maybe MediumError