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 / KleisliComposition.hs
Created June 12, 2018 13:23
Kleisli Composition
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
@StevenXL
StevenXL / monad_transformers.md
Created May 20, 2018 16:30
Monad Transformers

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.
@StevenXL
StevenXL / a.hs
Created February 8, 2018 02:34
a
{-# 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
@StevenXL
StevenXL / bitestring
Created February 8, 2018 02:29
ByteString
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE OverloadedStrings #-}
module Handler.ExpenseFile where
import Import
import Handler.Expense (expenseFileForm)
import Data.Csv (decodeByName)
postExpenseFileR :: Handler Html
postExpenseFileR = do
calculateReport :: Budget -> [Transaction] -> Report
calculateReport budget transactions =
Report
{ budgetProfit = budgetProfit'
, netProfit = netProfit'
, difference = netProfit' - budgetProfit'
}
where
budgetProfit' = budgetIncome budget - budgetExpenditure budget
netProfit' = getSum (foldMap asProfit transactions)
@StevenXL
StevenXL / separate.hs
Created January 18, 2018 14:52
Separate Query
-- WORKING CODE
getCategoryR :: Handler Html
getCategoryR = do
allCategories <- runDB $ selectList [] [] :: Handler [Entity Category]
let badges = []
categoriesWithExpenseCount <- categoriesWithExpenseCount
(formWidget, formEnctype) <- generateFormPost categoryForm
defaultLayout $(widgetFile "category")
categoriesWithExpenseCount :: Handler [(Entity Category, E.Value Int)]
@StevenXL
StevenXL / foldr.hs
Created March 19, 2017 14:04
foldr
-- foldr is meant to encapsulate the following pattern:
-- f [] = v
-- f (x:xs) = x # f xs
-- where # is an operator applied to the head of the list and recursively processing the tail
-- Using sum:
-- sum [] = 0
-- sum (x:xs) = x + sum xs
-- Therefore, sum = foldr (+) 0
@StevenXL
StevenXL / fromIntegral.md
Created November 6, 2016 14:22
fromIntegral

Hello folks, I have the following code, and I am having trouble understanding how fromIntegral is functioning:

avgDb :: [DatabaseItem] -> Double
avgDb dataBase = dividend / divisor
                 where dividend = fromIntegral $ sumDb dataBase
                       divisor = fromIntegral $ length $ filterDbNumber dataBase

Note that sumDb :: [DatabaseItem] -> Integer and filterDbNumber :: [DatabaseItem] -> [Integer].

@StevenXL
StevenXL / unverified_error.hs
Created October 12, 2016 01:45
Haskell Book - Unverified Error
-- Page 12 on the 0.12.0-screen version of the book
-- You start step 2 with the following lambda:
(\x.\y.\z.xy(yz))(\m.\n.\m)(\p.p)
-- You then reduce it, in step 3 to the following:
(\y.\z(\m.\n.\m)z(yz))(\p.p)
-- I believe that this reduce is missing a period after the first z, and instead should read like so:
(\y.\z.(\m.\n.\m)z(yz))(\p.p)
@StevenXL
StevenXL / keybase.md
Created June 12, 2016 20:14
Keybase.io GitHub Verification

Keybase proof

I hereby claim:

  • I am stevenxl on github.
  • I am stevenxl (https://keybase.io/stevenxl) on keybase.
  • I have a public key whose fingerprint is 9520 A429 3F63 17DD 47A8 7765 92CD EE68 A369 3DFA

To claim this, I am signing this object: