Skip to content

Instantly share code, notes, and snippets.

View vdorr's full-sized avatar
🥰

vdorr

🥰
  • Czechia
View GitHub Profile
@vdorr
vdorr / RawSocket.hs
Last active March 8, 2018 18:50
Bind socket to device in Haskell on Linux
{-# LANGUAGE ForeignFunctionInterface #-}
module RawSocket where
import Foreign.C
import Network.Socket
foreign import ccall "setsockopt"
setsockopt_cstr :: CInt -> CInt -> CInt -> CString -> CInt -> IO CInt
@vdorr
vdorr / stmtimeout.hs
Last active April 2, 2025 11:49
Timeout in Haskell STM monad
-- important: compile with -threaded
import Control.Concurrent.STM
import Control.Applicative
-- |Wait for STM action specified amount of time and return value of action or default value if action times out
stmTimeout' :: Int -> a -> STM a -> IO a
stmTimeout' microseconds defVal f
= registerDelay microseconds
@vdorr
vdorr / maybe.hs
Created June 29, 2018 12:53
Church encoding of Maybe type (without algebraic data types and typeclasses) & monadic ops
{-# LANGUAGE RankNTypes #-}
-- |Maybe without algebraic data types and typeclasses, including monadic ops
newtype Maybe' a = Maybe' (forall b. b -> (a -> b) -> b)
just' :: a -> Maybe' a
nothing' :: Maybe' a
fromMaybe' :: a -> Maybe' a -> a
maybe' :: b -> (a -> b) -> Maybe' a -> b