Skip to content

Instantly share code, notes, and snippets.

View jship's full-sized avatar

Jason Shipman jship

View GitHub Profile
@jship
jship / share-the-load.hs
Last active February 8, 2021 23:31
Simple way of splitting up units of work across some max number of workers
#!/usr/bin/env stack
{- stack
--resolver lts-16.26
--install-ghc
exec ghci
--package hspec
-- -ignore-dot-ghci
-}
{-# LANGUAGE BlockArguments #-}
#!/usr/bin/env stack
{- stack
--resolver lts-16.26
--install-ghc
exec ghci
--package http-client
-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE NoImplicitPrelude #-}
@jship
jship / insecure-tls.hs
Created December 23, 2020 16:05
Haskell reference for building an insecure TLS HTTP manager
#!/usr/bin/env stack
{- stack
--resolver lts-16.8
--install-ghc
exec ghci
--package connection
--package http-client
--package http-client-tls
-}
@jship
jship / TreePaths.hs
Created November 18, 2020 17:44
Get the paths for a Tree
import Data.Tree (Tree(Node, rootLabel, subForest))
paths :: Tree a -> [[a]]
paths = \case
Node { rootLabel, subForest } ->
[rootLabel] : concatMap (map (rootLabel :) . paths) subForest
#!/usr/bin/env stack
{- stack
--resolver lts-16.8
--install-ghc
exec ghci
-}
{-# LANGUAGE ScopedTypeVariables #-}
import Prelude hiding (foldl, foldr)
-- const has type:
-- const :: a -> b -> a
weirdConst :: b -> (forall a. a -> a) -> b
weirdConst b _f = b
blah :: Int
blah = weirdConst 42 id
-- blahBlah produces this compiler error:
--
#!/usr/bin/env stack
-- stack --resolver lts-15.6 --install-ghc exec ghci --package wai
{-# LANGUAGE ScopedTypeVariables, TypeApplications #-}
import Network.Wai (Middleware)
-- | This function doesn't look like much, but is pretty neat-o...
h :: (Functor f) => (b -> c) -> (a -> f b) -> a -> f c
h g f = fmap g . f
@jship
jship / translubernete
Created August 7, 2020 16:04
This script helps ensure my writing is precise and free of today's popular shorthand
#!/usr/bin/env stack
-- stack --resolver lts-15.6 script --package text
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE OverloadedStrings #-}
import Data.Text (Text)
import Prelude
import qualified Data.Text as Text
import qualified Data.Text.IO as Text.IO
#!/usr/bin/env stack
-- stack --resolver lts-16.3 --install-ghc exec ghci
{-# OPTIONS_GHC -Wall -Werror #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE RankNTypes #-}
import Data.Set (Set)
import qualified Data.Set as Set
printTree :: Show a => Tree a -> IO ()
printTree = putStrLn . Tree.drawTree . fmap show
tree :: Tree Int
tree = Tree.unfoldTree buildNode 1 where
buildNode :: Int -> (Int, [Int])
buildNode n
| 2 * n + 1 <= 7 = (n, [2 * n, 2 * n + 1])
| otherwise = (n, [])