Skip to content

Instantly share code, notes, and snippets.

@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
@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 / 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 / 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 / Main.hs
Last active August 30, 2018 06:33
State composition with ReaderT that can be fully mocked
{-# LANGUAGE TypeInType #-}
module Main where
import Control.Concurrent.STM
import Control.Exception.Base
import Control.Monad.Except
import Control.Monad.Identity
import Control.Monad.Reader
import Control.Monad.State
@DarinM223
DarinM223 / squareText.hs
Last active May 28, 2018 04:40
Meme text in Haskell
module Main where
import Control.Monad
import Data.List
newtype Indent = Indent { unIndent :: Int } deriving (Show, Eq)
squareText :: String -> Maybe String
squareText [] = Nothing
squareText [_] = Nothing
@DarinM223
DarinM223 / Main.hs
Last active March 26, 2018 08:16
Conduit TCP Server that handles messages one at a time with timeout handling and backpressure
module Main where
import Conduit
import Control.Concurrent (forkIO)
import Control.Concurrent.STM
import Data.Conduit.Network
import Data.ByteString hiding (putStrLn)
import Data.String
import qualified Data.STM.RollingQueue as RQ
@DarinM223
DarinM223 / defaults.yaml
Last active May 26, 2018 19:35
My Haskell defaults (paste into hpack generated stack project)
default-extensions:
- BangPatterns
- ConstraintKinds
- DataKinds
- DefaultSignatures
- DeriveFoldable
- DeriveFunctor
- DeriveGeneric
- DeriveLift
- DeriveTraversable