Skip to content

Instantly share code, notes, and snippets.

@DarinM223
DarinM223 / referential_transparency_examples.md
Last active July 24, 2018 02:03
Examples to show the benefits of the IO monad

Simple Example 1:

Let's say you have a function getVal like this:

getVal :: IO Int
getVal = do
    valRef <- newIORef 2
    modifyIORef valRef (+ 1)
    val <- readIORef valRef
@DarinM223
DarinM223 / ghcid.md
Last active August 1, 2018 03:13
Autoreload workflow

How I use Ghcid in a stack project

  1. Install ghcid with cabal install ghcid
  2. Add the ghcid neovim plugin to ~/.config/nvim/init.vim: Plugin 'ndmitchell/ghcid', { 'rtp': 'plugins/nvim' }
  3. Close neovim and reopen it and run :PluginInstall to install the plugin.
  4. Open a stack project in neovim and open a Haskell file (a file with a .hs extension)
  5. Run Ghcid with :Ghcid --restart=[project].cabal -c "stack repl --ghci-options=-fno-code" where [project] is the name of the project. If you want to load tests (but not load the app/Main module), run :Ghcid.
  6. Now whenever you add new files or update the dependencies, open a new terminal in neovim and run stack build. Ghcid will automatically restart and watch the new modules/dependencies.
@DarinM223
DarinM223 / non_null.rs
Last active September 15, 2018 02:05
Example of NonNull in Rust with a doubly linked list
use std::fmt::Debug;
use std::ptr::NonNull;
#[derive(Debug)]
pub struct DListNode<T: Debug> {
data: T,
next: Option<Box<DListNode<T>>>,
prev: Option<NonNull<DListNode<T>>>,
}
@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
@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 / 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 / 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 / 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 / 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 / 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)