In this article I’ll tell you about my pure functional library for Software Transactional Memory (STM)
that I’ve built in C++. I adopted some advanced functional programming concepts that make it composable and convenient to use. Its implementation is rather small and robust, which differentiates the library from competitors. Let’s discuss what STM is and how to use it.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Main where | |
import Control.Monad.State | |
import Control.Monad | |
import System.Random | |
type Name = String | |
data Context = Context { ctxNextId :: State Context Int | |
, ctxNextName :: State Context Name } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- file PathFind/Dijkstra.hs | |
module PathFind.Dijkstra (findPath) where | |
findPath = undefined | |
-- file PathFind/AStar.hs | |
module PathFind.AStar (findPath) where | |
findPath = undefined | |
-- file PathFind.hs | |
module PathFind (findPath, dijkstra, aStar) where |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE TemplateHaskell, Rank2Types #-} | |
module Main where | |
import Control.Lens | |
import Control.Monad.State | |
import Data.Monoid | |
import Data.List as L (insert, isInfixOf) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
takeFork :: TVar Fork -> STM () | |
takeFork tvFork = do | |
fork <- readTVar tvFork | |
case fork of | |
InUse -> retry | |
Free -> writeTVar tvFork InUse | |
makeDiningPhilosopher :: TVar Fork -> TVar Fork -> STM (TVar Philosopher) | |
makeDiningPhilosopher leftFork rightFork = do | |
takeFork leftFork |
Article topics:
Bio TODO
#include <iostream>
constexpr int sum (int a, int b)
{
return a + b;
}
constexpr int ediv (int a, int b)
{
OlderNewer