Skip to content

Instantly share code, notes, and snippets.

View StevenXL's full-sized avatar
🙌
WFH

Steven Leiva StevenXL

🙌
WFH
View GitHub Profile
@StevenXL
StevenXL / Test.hs
Created April 3, 2019 13:40
Compiler Error with Symbol
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE InstanceSigs #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE FlexibleInstances #-}
@StevenXL
StevenXL / cardinality.txt
Created March 8, 2019 17:52
Cardinality
| Either Bool (Bool, Maybe Bool -> Bool)| =
|Bool| + |(Bool, Maybe Bool -> Bool)| =
2 + |(Bool, Maybe Bool -> Bool)| =
2 + (|Bool| * |Maybe Bool -> Bool|) =
2 + (2 * |Maybe Bool -> Bool|) =
2 + (2 * |Bool|^(Maybe Bool)) =
2 + (2 * 2^(Maybe Bool)) =
2 + (2 * 2^(|Bool| + 1)) =
2 + (2 * 2^(2 + 1)) =
2 + (2 * 2^(3)) =
@StevenXL
StevenXL / Cipher.hs
Created December 21, 2018 21:05
Example usage of Scope Type Variables
{-# LANGUAGE ScopedTypeVariables #-}
module Cipher where
data FourLetterAlphabet = L1 | L2 | L3 | L4 deriving (Show, Enum, Bounded)
-- Because of ScopedTypeVariables, the type variable "a" in the where clause
-- refers to the same type variable "a" as in the function's signature.
--
-- When we apply rotN to a value, all instances of "a" will be instantiated to
@StevenXL
StevenXL / FizzBuzz.hs
Created December 19, 2018 22:44
On over-engineered example of FizzBuzz in Haskell
-- This is an over-engineered example of using the `Either a` monad to implement
-- a FizzBuzz. This exercise was inspired by "The Power of Composition" by Scott
-- Wlaschin (https://youtu.be/WhEkBCWpDas?t=2489).
module FizzBuzz where
main :: IO ()
main = mapM_ printAsFizzBuzz [1..100]
where printAsFizzBuzz :: Int -> IO ()
printAsFizzBuzz = putStrLn . fizzBuzz
@StevenXL
StevenXL / Connect.hs
Last active November 5, 2018 02:47
IORef (Map UserId (TChan Text))
getConnectR :: Handler ()
getConnectR = do
mUserId <- maybeAuthId
maybe notAuthenticated connectToWebSocket mUserId
where connectToWebSocket :: UserId -> Handler ()
connectToWebSocket userId = do
webSockets (serverEventSocket userId)
return () -- return 426 (https://httpstatuses.com/426) (https://stackoverflow.com/questions/42324473/http-1-1-426-upgrade-required)
serverEventSocket :: UserId -> WebSocketsT Handler ()
serverEventSocket userId = do
@StevenXL
StevenXL / redis-yesod.md
Created November 5, 2018 02:35 — forked from bitemyapp/redis-yesod.md
Connecting to Redis from Yesod

Connecting to Redis from Yesod

This is a quick run-through of how I connected to Redis from a Yesod site (which used the default scaffolding). There isn't much specific to Redis here, so this information should apply to connecting to any database or service from Yesod.

Background: Basics of Hedis

First, a brief intro of the basics of Hedis:

{-# LANGUAGE OverloadedStrings #-}
@StevenXL
StevenXL / twilio-grab.hs
Last active September 17, 2018 00:33
Twilio Grab
-- stack --resolver lts-12.5 script --package mtl --package aeson --package http-conduit --package bytestring --package cassava --package MissingH
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE DeriveGeneric #-}
import Control.Monad.IO.Class (liftIO)
import Control.Monad.Reader (ReaderT, ask, runReaderT)
import Control.Monad.State (StateT, evalStateT, get, put)
import Data.Aeson (FromJSON)
import qualified Data.ByteString as B
@StevenXL
StevenXL / appveyor.txt
Created July 20, 2018 02:59
AppVeyor Issue on Yesod
Configuring yesod-bin-1.6.0.3...
Preprocessing executable 'yesod' for yesod-bin-1.6.0.3..
Building executable 'yesod' for yesod-bin-1.6.0.3..
[1 of 7] Compiling AddHandler ( AddHandler.hs, .stack-work\dist\010ee936\build\yesod\yesod-tmp\AddHandler.o )
[2 of 7] Compiling Devel ( Devel.hs, .stack-work\dist\010ee936\build\yesod\yesod-tmp\Devel.o )
[3 of 7] Compiling HsFile ( HsFile.hs, .stack-work\dist\010ee936\build\yesod\yesod-tmp\HsFile.o )
[4 of 7] Compiling Keter ( Keter.hs, .stack-work\dist\010ee936\build\yesod\yesod-tmp\Keter.o )
C:\projects\yesod\yesod-bin\Keter.hs:36:15: warning: [-Wdeprecations]
In the use of `decodeFile' (imported from Data.Yaml):
Deprecated: "Please use decodeFileEither, which does not confused type-directed and runtime exceptions."
@StevenXL
StevenXL / infinite_type.hs
Created July 1, 2018 04:28
Infinite Type
module RecursiveContents (getRecursiveContents, simpleFind, filterM, betterFind) where
import Data.Maybe (fromMaybe)
import Control.Exception (SomeException, bracket, handle)
import Control.Monad (filterM, forM)
import Data.Time (UTCTime)
import System.Directory (Permissions, doesFileExist,
getModificationTime, getPermissions,
listDirectory)
import System.FilePath ((</>))
@StevenXL
StevenXL / monad_pure.hs
Created June 13, 2018 13:01
Monads and Pure Functions
module Monad where
maxPairM :: (Monad m, Ord a) => m (a, a) -> m a
maxPairM m = m >>= \p -> return (maxPair p)
maxPairM' :: (Monad m, Ord a) => m (a, a) -> m a
maxPairM' m = (return . maxPair) =<< m -- I like it because it looks like function composition
maxPair :: Ord a => (a, a) -> a
maxPair (f, s) = max f s