Skip to content

Instantly share code, notes, and snippets.

View jfischoff's full-sized avatar
™️
Jonathaning

Jonathan Fischoff jfischoff

™️
Jonathaning
View GitHub Profile
delayOne :: (MonadHold t m, Reflex t) => Event t a -> m (Event t (a, a))
delayOne e = do
b <- hold Nothing $ Just <$> e
let eOld = W.catMaybes $ b <@ e
pure $ liftF2 (,) eOld e
@jfischoff
jfischoff / Fold.hs
Created July 15, 2019 03:11
Fold like thing with termination
-- The idea is to compose fold like things that can terminate. This is primarily so I can
-- make a Alternative instance that returns the first finished fold.
-- I copied much of this from foldl and folds but unlike those libraries you cannot call the `extractor` until the
-- fold is finished.
data StepState = Running | Finished
deriving (Eq, Show, Ord, Read, Generic)
anyFinished :: StepState -> StepState -> StepState
anyFinished x y = case (x, y) of
{-# LANGUAGE RecordWildCards #-}
module TH where
import Language.Haskell.TH
import System.FilePath
import Control.Monad ((<=<))
import System.Directory (getCurrentDirectory, canonicalizePath)
fileRelativeToAbsolute :: String -> Q Exp
fileRelativeToAbsolute = stringE <=< fileRelativeToAbsoluteStr
@jfischoff
jfischoff / WaitFor.hs
Created September 15, 2017 16:40
Common wait for a socket function
waitForServer :: Int -> IO ()
waitForServer port = handle (\(_ :: IOException) -> waitForServer port) $ do
let hints = S.defaultHints { S.addrFlags = [ S.AI_NUMERICHOST
, S.AI_NUMERICSERV
]
, S.addrSocketType = S.Stream
}
addr:_ <- S.getAddrInfo (Just hints) (Just "127.0.0.1") (Just $ show port)
bracket (S.socket (S.addrFamily addr) (S.addrSocketType addr) (S.addrProtocol addr))
S.close
{-# LANGUAGE QuasiQuotes #-}
module SimpleDBSpec (spec, main) where
import Database.PostgreSQL.Simple.SqlQQ
import qualified Database.PostgreSQL.Simple as Simple
import Database.PostgreSQL.Transact
import Test.Hspec (Spec, hspec)
import Test.Hspec.Expectations.Lifted
import Test.Hspec.DB
import Control.Monad
waitForPort :: Int -> IO ()
waitForPort port = handle (\(_ :: IOException) -> threadDelay 10000 >> waitForDB port) $ do
let hints = defaultHints
{ addrFlags =
[ AI_NUMERICHOST
, AI_NUMERICSERV
]
, addrSocketType = Stream
}
addr:_ <- getAddrInfo (Just hints) (Just "127.0.0.1") (Just $ show port)
{-# LANGUAGE OverloadedLists #-}
{-# LANGUAGE LambdaCase #-}
module Network.TCP where
import Data.Set (Set)
data PacketFlag = NS | CWR | ECE | URG | ACK | PSH | RST | SYN | FIN
deriving (Eq, Show, Ord)
type Packet = Set PacketFlag
data Edit a
= Insert a
| Delete a
| Same a
deriving (Show, Eq)
isSame :: Edit a -> Bool
isSame x = case x of
Insert {} -> False
Delete {} -> False
@jfischoff
jfischoff / gist:999abf4ac5e5dc516d3a
Created December 31, 2014 22:40
A mix of Vinyl style and SOP style
foldRRec :: forall f xs c a b p.
RecAll f xs c
=> p c
-> (forall x. c x => x -> a)
-> (a -> b -> b)
-> b
-> NP f xs
-> b
foldRRec p convert f z xs
= foldRRec' convert f z
@jfischoff
jfischoff / gist:b2f8c60883f2cfd0b187
Created December 13, 2014 23:51
Use case for reflection non law abiding Eqs
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE StandaloneDeriving #-}
module Approx where
import Data.Reflection
import Data.Proxy
import Foreign.Storable