Skip to content

Instantly share code, notes, and snippets.

Avatar

Steven Leiva StevenXL

View GitHub Profile
@StevenXL
StevenXL / FizzBuzz.hs
Created Dec 19, 2018
On over-engineered example of FizzBuzz in Haskell
View FizzBuzz.hs
-- 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 Nov 5, 2018
IORef (Map UserId (TChan Text))
View Connect.hs
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 Nov 5, 2018 — forked from bitemyapp/redis-yesod.md
Connecting to Redis from Yesod
View redis-yesod.md

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 #-}
View twilio-grab.hs
-- 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 Jul 20, 2018
AppVeyor Issue on Yesod
View appveyor.txt
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."
View infinite_type.hs
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 Jun 13, 2018
Monads and Pure Functions
View monad_pure.hs
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
View KleisliComposition.hs
module KleisliComposition where
import Data.Map (Map)
import qualified Data.Map as M
import Control.Monad ((<=<))
type UserName = String
type GamerID = Int
type PlayerCredits = Int
View monad_transformers.md

Monads allow us to build computations that have effects. However, in "real code", we often need to be able to use several effects at once. This is what monad transformers allow us to do.

A monad transformer modifies the behavior of the underlying monad. It modifies the behavior in that it adds teh effect of the base monad to the inner monad.

Some examples:

  1. The StateT monad transformer adds mutable state to the underlying monad.
View a.hs
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE OverloadedStrings #-}
module Handler.ExpenseFile where
import Import
import Handler.Expense (expenseFileForm)
import Data.Csv (decodeByName)
import qualified Data.ByteString as B
postExpenseFileR :: Handler Html
You can’t perform that action at this time.