Updated slides
This commit is contained in:
parent
7b35eb4da8
commit
17aefb8e54
1 changed files with 59 additions and 6 deletions
|
@ -491,7 +491,6 @@ main = do
|
||||||
|
|
||||||
```haskell
|
```haskell
|
||||||
$ ghci -v0
|
$ ghci -v0
|
||||||
Prelude> :set -XOverloadedStrings
|
|
||||||
Prelude> import Turtle
|
Prelude> import Turtle
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -919,16 +918,12 @@ format (s%" failed with exit code: "%d) :: Text -> Int -> Text
|
||||||
|
|
||||||
# Exercise
|
# Exercise
|
||||||
|
|
||||||
What do you think these print out?
|
What do you think this prints out?
|
||||||
|
|
||||||
```haskell
|
```haskell
|
||||||
Prelude Turtle> format ("A "%s%" string that takes "%d%" arguments") "format" 2
|
Prelude Turtle> format ("A "%s%" string that takes "%d%" arguments") "format" 2
|
||||||
```
|
```
|
||||||
|
|
||||||
```haskell
|
|
||||||
Prelude Turtle> format "I take 0 arguments"
|
|
||||||
```
|
|
||||||
|
|
||||||
# The `Format` type
|
# The `Format` type
|
||||||
|
|
||||||
A format string is not `Text`!
|
A format string is not `Text`!
|
||||||
|
@ -942,6 +937,7 @@ So what is going on here?
|
||||||
|
|
||||||
```haskell
|
```haskell
|
||||||
Prelude Turtle> format "I take 0 arguments"
|
Prelude Turtle> format "I take 0 arguments"
|
||||||
|
"I take 0 arguments"
|
||||||
```
|
```
|
||||||
|
|
||||||
# `Format` implements `IsString`
|
# `Format` implements `IsString`
|
||||||
|
@ -1727,6 +1723,63 @@ tuple = do
|
||||||
|
|
||||||
# Questions?
|
# Questions?
|
||||||
|
|
||||||
|
# Backup utility example
|
||||||
|
|
||||||
|
```haskell
|
||||||
|
{-# LANGUAGE OverloadedStrings #-}
|
||||||
|
|
||||||
|
import Turtle
|
||||||
|
import Prelude hiding (FilePath)
|
||||||
|
|
||||||
|
parser = (,) <$> argPath "src" "Source directory"
|
||||||
|
<*> argPath "dst" "Destination directory"
|
||||||
|
|
||||||
|
backup file = do
|
||||||
|
exists <- testfile file
|
||||||
|
when exists (do
|
||||||
|
let backupFile = file <.> "bak"
|
||||||
|
backup backupFile
|
||||||
|
mv file backupFile )
|
||||||
|
|
||||||
|
main = do
|
||||||
|
(src, dest) <- options "Backup a directory" parser
|
||||||
|
sh (do
|
||||||
|
inFile <- lstree src
|
||||||
|
Just suffix <- return (stripPrefix src inFile)
|
||||||
|
let outFile = dest </> suffix
|
||||||
|
backup outFile
|
||||||
|
echo (format ("Copying "%fp%" to "%fp) inFile outFile)
|
||||||
|
cp inFile outFile )
|
||||||
|
echo "Done!"
|
||||||
|
```
|
||||||
|
|
||||||
|
# Command line usage
|
||||||
|
|
||||||
|
```bash
|
||||||
|
$ ./backup --help
|
||||||
|
Backup a directory
|
||||||
|
|
||||||
|
Usage: backup SRC DST
|
||||||
|
|
||||||
|
Available options:
|
||||||
|
-h,--help Show this help text
|
||||||
|
SRC Source directory
|
||||||
|
DST Destination directory
|
||||||
|
```
|
||||||
|
|
||||||
|
```bash
|
||||||
|
$ ./backup a/ b/
|
||||||
|
Copying a/1 to b/1
|
||||||
|
Copying a/2 to b/2
|
||||||
|
$ ls b/
|
||||||
|
1 2
|
||||||
|
$ ./backup a/ b/
|
||||||
|
Copying a/1 to b/1
|
||||||
|
Copying a/2 to b/2
|
||||||
|
$ ls b/
|
||||||
|
1 1.bak 2 2.bak
|
||||||
|
```
|
||||||
|
|
||||||
# Conclusions
|
# Conclusions
|
||||||
|
|
||||||
You can use Haskell as a "better Bash", getting types for free without slow
|
You can use Haskell as a "better Bash", getting types for free without slow
|
||||||
|
|
Loading…
Reference in a new issue