Skip to content

Instantly share code, notes, and snippets.

@DarinM223
DarinM223 / oops.hs
Last active November 11, 2019 12:55
Error handling with variants
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE MonoLocalBinds #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeOperators #-}
module Main where
import Control.Monad.Except
import Control.Monad.Identity
import Data.Bifunctor
@DarinM223
DarinM223 / tooling.md
Last active January 26, 2021 03:33
Haskell/Purescript/Lisp tooling

Haskell

Package manager

The main package manager to use in Haskell is Cabal. Make sure that the Cabal version that you are using is at least 3.0. Then build, repl, install, etc will use the upgraded v2-style versions that work with minimal problems.

Documentation

@DarinM223
DarinM223 / implicit_params.md
Last active March 15, 2022 18:04
ImplicitParams Notes

ImplicitParams notes:

Give explicit type signatures to all functions that use implicits

All top level functions should already have explicit type signatures, and there are GHC warnings that force this rule.

However, all local functions that use implicits should

@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