Let's say you have a function getVal
like this:
getVal :: IO Int
getVal = do
valRef <- newIORef 2
modifyIORef valRef (+ 1)
val <- readIORef valRef
cabal install ghcid
Plugin 'ndmitchell/ghcid', { 'rtp': 'plugins/nvim' }
:PluginInstall
to install the plugin.: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
.stack build
.
Ghcid will automatically restart and watch the new modules/dependencies.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>>>, | |
} |
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 |
Here are some tips I found useful for writing Haskell code that abstracts over effects, along with the articles that describe them.
ReaderT Config IO
,
where Config is a record with all the mutable references to everything
needed for the application.{-# 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 |
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);
{-# 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 |
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) |