Skip to content

Instantly share code, notes, and snippets.

View wuct's full-sized avatar

CT Wu wuct

View GitHub Profile
@wuct
wuct / deadlock.rs
Created June 29, 2020 11:49
A deadlock example in Rust with threads and `Mutex`
use std::sync::{Arc, Mutex};
use std::thread;
fn main() {
let a = Arc::new(Mutex::new(0));
let b = Arc::new(Mutex::new(0));
let mut handles = vec![];
{
let a = Arc::clone(&a);
@wuct
wuct / Vector.purs
Last active September 6, 2018 15:01
class Nat (a :: Nat)
where toInt :: NProxy a → Int
instance natZero :: Nat Zero
where toInt _ = 0
instance natSucc :: Nat n ⇒ Nat (Succ n)
where toInt _ = 1 + toInt (NProxy :: NProxy n)
newtype Vec (n :: Nat) a = Vec (Array a)
foreign import kind Nat
foreign import kind Boolean
foreign import data Zero :: Nat
foreign import data Succ :: Nat → Nat
foreign import data True :: Boolean
foreign import data False :: Boolean
data NProxy (a :: Nat) = NProxy
data BProxy (b :: Boolean) = BProxy
@wuct
wuct / TypeLeve.purs
Last active September 6, 2018 14:57
data Zero
data Succ n
type Two = Succ (Succ Zero)
type Three = Succ (Succ (Succ Zero))
data True
data False
class IsEven n b
data Nat = Succ Nat | Zero
isEven :: Nat → Boolean
isEven Zero = true
isEven (Succ n) = isOdd n
isOdd :: Nat → Boolean
isOdd Zero = false
isOdd (Succ n) = isEven n
@wuct
wuct / Printf.purs
Last active August 15, 2018 03:26
PureScript printf using instance chain
module Main where
import Prelude
import Effect (Effect)
import Effect.Class.Console (log)
class Printf r
where printf :: String -> r
instance printfString :: Printf String
@wuct
wuct / State.purs
Created December 2, 2017 10:23
A transformerless State Monad implemtation
module Main where
import Prelude
import Data.Tuple (Tuple(..), snd)
import Data.Monoid (class Monoid, mempty)
import Control.Monad.Eff.Console (logShow)
import TryPureScript (render, withConsole)
newtype State s a = State (s -> Tuple a s)
@wuct
wuct / Writer.purs
Created December 2, 2017 09:00
A Writer Monad implementation without introduction Monad Transformer.
module Main where
import Prelude
import Data.Tuple (Tuple(..), snd)
import Data.Monoid (class Monoid, mempty)
import Control.Monad.Eff.Console (logShow)
import TryPureScript (render, withConsole)
newtype Writer w a = Writer (Tuple a w)
@wuct
wuct / Reader.purs
Created October 24, 2017 16:53
This is a Reader Monad implementation without introduction Monad Transformer
module Main where
import Prelude
import Control.Monad.Reader.Class
import Control.Monad.Eff.Console (logShow)
import TryPureScript
newtype Reader r a = Reader (r -> a)
@wuct
wuct / PureScriptByExampleCh7.purs
Created October 3, 2017 11:01
Answers of PureScript by Example Ch7 exercises.
liftedAdd :: forall f a. Apply f => Semiring a => f a -> f a -> f a
liftedAdd = lift2 (+)
combineMaybe :: forall a f. Applicative f => Maybe (f a) -> f (Maybe a)
combineMaybe Nothing = pure Nothing
combineMaybe (Just f) = Just <$> f