Skip to content

Instantly share code, notes, and snippets.

View osa1's full-sized avatar

Ömer Sinan Ağacan osa1

View GitHub Profile
@osa1
osa1 / Main.hs
Last active August 22, 2018 05:59
{-# LANGUAGE ForeignFunctionInterface #-}
module Main where
import System.Clock -- clock
import Foreign.C.Types -- base
foreign import ccall unsafe "my_sleep" sleep_unsafe :: CInt -> IO CInt
foreign import ccall safe "my_sleep" sleep_safe :: CInt -> IO CInt
@osa1
osa1 / gist:aa28bb5c94a74e96e870ddde846b2e9c
Last active October 26, 2017 17:55
.ghci for fuzzy file loading
import System.IO.Temp (withSystemTempFile)
import System.Process (runProcess, waitForProcess)
:{
let loadFuzzy _ = do
withSystemTempFile "fzf_out" $ \f h -> do
p <- runProcess "fzf" [] Nothing Nothing Nothing (Just h) Nothing
_ <- waitForProcess p
out <- readFile f
return (":load " ++ init out)
@osa1
osa1 / Main.hs
Last active November 6, 2017 12:56
parallel scheduler
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeFamilies #-}
module Main where
@osa1
osa1 / errors.hs
Last active May 31, 2017 13:04
More freer and exceptions
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeOperators #-}
import Control.Monad.Eff
module ExpandOrPats where
{-
When desugaring we may want to be smarter than this, but for checking purposes simple expansion
of or patterns into non-or-patterns is fine.
In GHC a match has this type:
data Match id body
= Match {
@osa1
osa1 / checked.hs
Created February 24, 2017 20:02
lightweight checked exceptions with freer
{-# LANGUAGE CPP #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE ExistentialQuantification #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE IncoherentInstances #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE RoleAnnotations #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeOperators #-}
@osa1
osa1 / perms.hs
Created February 19, 2017 14:04
mutable variables with permissions
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}
module Lib where
@osa1
osa1 / lensref.hs
Created February 19, 2017 11:12
Mutable references with attached lenses for fmap-like mapping
{-# LANGUAGE ExistentialQuantification #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE TemplateHaskell #-}
module Lib where
import Control.Lens
import Data.IORef
atomicModifyIORef_ :: IORef a -> (a -> a) -> IO ()
@osa1
osa1 / Queue.hs
Created February 17, 2017 07:08
data Queue a = Queue { _front :: [a], _back :: [a] }
module Data.Queue
( Queue
, newQueue
, enqueue
, dequeue
, filterQueue
) where
-- | A functional queue.
data Queue a = Queue { _front :: [a], _back :: [a] }
@osa1
osa1 / stateful.hs
Last active February 13, 2017 20:20
Running state effect using IORef
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE TypeOperators #-}
module Lib where
import Control.Monad.Freer
import Control.Monad.Freer.Internal