This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import functools | |
| def compose(*functions): | |
| "Compose multiple functions together. See https://mathieularose.com/function-composition-in-python/" | |
| return functools.reduce(lambda f, g: lambda x: f(g(x)), functions) | |
| # More limited case of just two functions: | |
| #def compose2(f, g): | |
| # return lambda x: f(g(x)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| -- Let's say that we want to print out each of a list of items: | |
| λ>map print [1,2,3] | |
| <interactive>:13:1: | |
| No instance for (Show (IO ())) arising from a use of ‘print’ | |
| In a stmt of an interactive GHCi command: print it | |
| -- That doesn't work!... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| λ> let fib = 0: 1: zipWith (+) fib (tail fib) | |
| λ> take 10 fib | |
| [0,1,1,2,3,5,8,13,21,34] | |
| λ> fib !! 100 | |
| 354224848179261915075 | |
| λ> fib !! 1000 | |
| 434665576869374564356885276750406258025646605173717804024817290895365554179490518904038798400792551692959225930803226347 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| λ> import Data.Functor.Compose | |
| -- List of lists | |
| λ> getCompose $ fmap (+1) $ Compose [[],[1],[2,3],[4,5,6]] | |
| [[],[2],[3,4],[5,6,7]] | |
| -- List of Maybe | |
| λ> getCompose $ fmap (+1) $ Compose [Just 1, Just 2, Nothing, Just 3] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module ReaderMonad where | |
| import Control.Monad.Reader | |
| stuff :: Reader Int String | |
| stuff = do | |
| s <- ask | |
| return (show s ++ " green bottles") | |
| main :: IO () |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module WriterMonad where | |
| -- From http://learnyouahaskell.com/for-a-few-monads-more | |
| -- This example no longer works without tweaking - see | |
| -- http://stackoverflow.com/questions/11684321/how-to-play-with-control-monad-writer-in-haskell | |
| -- just replace the data constructor "Writer" with the function "writer" in the line marked "here" | |
| -- That changed with mtl going from major version 1.* to 2.*, shortly after LYAH and RWH were written | |
| import Control.Monad.Writer | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module TrivialMonad where | |
| -- See also http://hackage.haskell.org/package/transformers-0.4.1.0/docs/src/Data-Functor-Identity.html#Identity | |
| import Control.Applicative (Applicative(..)) -- import type and all exported constructors using '(..)' | |
| -- How is that different from "import Control.Applicative" | |
| import Control.Monad (liftM, ap) | |
| data Trivial a = Trivial a deriving (Show) -- wrapper type |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module FindFiles where | |
| import System.FilePath.Find | |
| -- Can't install FileManip on Windows because it depends on "unix", which (surprise!) can't be installed | |
| -- on Windows without Cygwin etc. Instead, install filemanip not FileManip (obviously ?!) | |
| -- which depends on unix-compat, which is fine on Windows | |
| main :: IO () | |
| main = do |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module ZipExample where | |
| -- https://hackage.haskell.org/package/zip-archive | |
| -- https://hackage.haskell.org/package/zip-archive-0.2.3.5/docs/Codec-Archive-Zip.html | |
| -- Example usage: https://github.com/jgm/zip-archive/blob/master/Zip.hs | |
| import Codec.Archive.Zip | |
| import qualified Data.ByteString.Lazy as B | |
| import Control.Applicative ( (<$>) ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module ZipConduit1 where | |
| -- requires zip-conduit package | |
| -- slightly adapted from the docs at: | |
| -- https://hackage.haskell.org/package/zip-conduit-0.2.2.1/docs/Codec-Archive-Zip.html | |
| import Codec.Archive.Zip | |
| main :: IO () | |
| main = do |