When starting out with Haskell, I found it difficult to read a lot of the symbols. I made this document explaining the names of some symbols and how I read them.
x :: Int
// A JavaScript promises cheat-sheet | |
// an immediately resolved promise | |
const p1 = Promise.resolve('foo'); | |
// can get it after the fact, unlike events | |
p1.then((res) => console.log('p1 got:', res)); | |
// => p1 got: foo |
import Control.Monad ( void ) | |
import Data.Aeson ( FromJSON, ToJSON ) | |
import qualified Data.Text as T | |
import GHC.Generics ( Generic ) | |
import Language.Plutus.Contract | |
import Language.PlutusTx as PlutusTx | |
import Language.PlutusTx.Prelude | |
import Ledger | |
import Ledger.Ada as Ada | |
import Ledger.Constraints as Constraints |
I hereby claim:
To claim this, I am signing this object:
#! /usr/bin/env stack | |
-- stack --resolver lts-13.15 runghc --package mtl | |
{-# LANGUAGE FlexibleContexts #-} | |
import Control.Monad.Except | |
main :: IO () | |
main = do |
#! /usr/bin/env stack | |
-- stack --resolver lts-18.8 script | |
{-# LANGUAGE OverloadedStrings #-} | |
{- | |
This is a handy illustration of converting between five of the commonly-used | |
string types in Haskell (String, ByteString, lazy ByteString, Text and lazy | |
Text). |
#!/usr/bin/env stack | |
-- stack --resolver lts-12.24 script | |
-- If stack isn't installed on your system and you can't/won't get it, install | |
-- a reasonably-modern GHC and use this instead of the above two lines: | |
-- #! /usr/bin/env runhaskell | |
import Control.Monad.State | |
In Haskell, it's sometimes useful to turn off buffering when writing code that uses the standard input/output file handles. That can be done to the three commonly-used handles in one line like this:
import System.IO
mapM_ (flip hSetBuffering NoBuffering) [ stdout, stderr, stdin ]
It's kind of terse though, let's go through what's happening in it.
Going through the example code for System.Console.GetOpt got me thinking about the type of foldl (flip id)
Specifically, the second example Interpreting flags as transformations of an options record. The code is using this to combine all of the command-line args into a record of the args as a whole.
I was confused about how foldl (flip id)
has the type it does
#! /usr/bin/env runhaskell | |
import Text.HTML.TagSoup | |
main :: IO () | |
main = do | |
tags <- fmap parseTags $ readFile "foo.xml"; | |
let names = sections (~== "<name>") $ tags | |
mapM_ (\t -> putStrLn $ "saw: " ++ t ) $ map fromTagText | |
$ map (flip (!!) 1) names |