Skip to content

Instantly share code, notes, and snippets.

@DarinM223
DarinM223 / child.ex
Created June 3, 2017 12:09
OTP supervisor Registry example
defmodule Otp.Child do
@moduledoc """
A simple child process that models a stack.
"""
use GenServer
def start_link(state, opts \\ []) do
GenServer.start_link(__MODULE__, state, opts)
end
@DarinM223
DarinM223 / Main.hs
Last active February 13, 2018 12:58
Go-Fish Haskell
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE FlexibleContexts #-}
module Main where
@DarinM223
DarinM223 / Installing.md
Created January 27, 2018 13:29
Installing GHC Mod

Installing GHC Mod

  1. Go to ~/.stack/global-project/stack.yaml and change the resolver: section to lts-10.3.

  2. Add:

- https://hackage.haskell.org/package/ghc-mod-5.9.0.0/candidate/ghc-mod-5.9.0.0.tar.gz
- cabal-helper-0.8.0.0
@DarinM223
DarinM223 / typeclassses.md
Last active March 31, 2023 03:35
Typeclasses in Haskell, Scala, and Rust

Type classes

Type classes have some advantages over Java style interfaces. One advantage is the ability to implement functions that do not need to take in an instance of the interface.

For example, (FromString v) takes in a String and returns v, and it doesn't have to take v as a parameter.

@DarinM223
DarinM223 / haskell.md
Last active February 16, 2018 04:36
Haskell stuff

How to use stack?

  1. Run stack new project_name to create a new stack project.
  2. Edit package.yaml instead of the cabal file. This is confusing because many older answers recommend editing the cabal file, but recent versions of stack use HPack which autogenerates the cabal file from the package.yaml file. Editing the package.yaml file is better than manually editing the cabal file because it automatically discovers your modules and adds them to the cabal (otherwise you have to add every file to the modules section). Examples on HPack syntax are in https://github.com/sol/hpack
  3. To build with stack, run stack build.
  4. To run an executable with stack, run stack exec [project_name]-exe. To run with arguments add -- before your arguments.
@DarinM223
DarinM223 / rust_haskell_examples.md
Last active June 22, 2018 06:43
Rust and Haskell examples

Some examples in Rust and Haskell

I've been learning Haskell recently and the hardest thing so far is figuring out how to map stuff that I already know how to do in other languages to Haskell, like updating nested structures, composing state, and handling errors with state.

For me I feel that the best way to do this is to show examples of equivalent code in Haskell and another language. Because I am familiar with Rust and Rust has types that are similar to Haskell's types while also being an imperative language, the comparison code is in Rust.

@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
@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 / 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 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