Skip to content

Commit 9d9b8c1

Browse files
committed
Merge branch 'master' of github.com:typeclasses/haskell-phrasebook
2 parents 41374b6 + f0a13cb commit 9d9b8c1

File tree

6 files changed

+37
-16
lines changed

6 files changed

+37
-16
lines changed

outputs/threads.txt

+9
Original file line numberDiff line numberDiff line change
@@ -1 +1,10 @@
1+
main: 1
2+
main: 2
3+
main: 3
4+
...
5+
...
6+
...
7+
...
8+
...
9+
...
110
done

outputs/transactions.txt

+4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1+
...
12
Total: 1000
3+
...
24
Total: 1000
5+
...
36
Total: 1000
7+
...
48
Total: 1000

readme.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ In addition to the code examples themselves, the results from running the exampl
3333
3434
When any source code or dependency versions change, run `./tools/generate-outputs`. This script runs all of the examples and updates the output files.
3535
36-
Only the standard output stream (`stdout`) is captured in the output files. Any examples that include nondeterministic behavior (such as `threads.hs`) print the nondeterministic portion of their output to the standard error stream (`stderr`) to avoid including non-repeatable results in the output files.
36+
Any examples that include nondeterministic behavior (such as `threads.hs`) have the nondeterministic portion of their output redacted as "..." to avoid including non-repeatable results in the output files.
3737
3838
## Nix dependency versions
3939

threads.hs

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ import System.IO
66

77
main =
88
do
9-
hSetBuffering stderr LineBuffering
9+
hSetBuffering stdout LineBuffering
1010

1111
tasksCompleted <- atomically (newTVar 0)
1212

1313
let
1414
task x =
1515
do
1616
for_ [1..3] $ \i ->
17-
hPutStrLn stderr (x ++ ": " ++ show i)
17+
putStrLn (x ++ ": " ++ show i)
1818
atomically $
1919
modifyTVar' tasksCompleted (+ 1)
2020

tools/outputs.nix

+9-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
{ runCommand, linkFarm, haskell }:
1+
{ runCommand, linkFarm, haskell, lib }:
22

33
let
4-
run = name: hsFile:
4+
inherit (lib) concatMapStringsSep escapeShellArg;
5+
6+
run = name: hsFile: run' name hsFile {};
7+
8+
run' = name: hsFile: { sed ? [] }:
59
{
610
inherit name;
711
path = runCommand "phrasebook-output-${name}.txt"
@@ -10,7 +14,7 @@ let
1014
inherit hsFile;
1115
}
1216
''
13-
runhaskell "$hsFile" > $out
17+
runhaskell "$hsFile" ${concatMapStringsSep " " (x: "| sed -e ${escapeShellArg x}") sed} > $out
1418
'';
1519
};
1620

@@ -22,8 +26,8 @@ in
2226
(run "hashing.txt" ../hashing.hs)
2327
(run "hello-world.txt" ../hello-world.hs)
2428
(run "mutable-references.txt" ../mutable-references.hs)
25-
(run "threads.txt" ../threads.hs)
29+
(run' "threads.txt" ../threads.hs { sed = ["s!^fork.*$!...!"]; })
2630
(run "timeouts.txt" ../timeouts.hs)
27-
(run "transactions.txt" ../transactions.hs)
31+
(run' "transactions.txt" ../transactions.hs { sed = ["s!\\[.*\\]!...!"]; })
2832
(run "variables.txt" ../variables.hs)
2933
]

transactions.hs

+12-8
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1-
import Control.Concurrent (forkIO, threadDelay)
2-
import Control.Monad (forever, when)
3-
import Data.Foldable (asum, for_)
4-
import Data.Traversable (for)
5-
import System.IO
6-
71
import Control.Monad.STM
82
import Control.Concurrent.STM.TVar
93

4+
import Control.Concurrent (forkIO, threadDelay)
5+
106
import System.Random.MWC (createSystemRandom, uniformR)
7+
118
import qualified Data.Sequence as Seq
129

10+
import Control.Monad (forever, when)
11+
import Data.Foldable (asum, for_)
12+
import Data.Traversable (for)
13+
1314
main =
1415
do
1516
accountList <-
@@ -24,12 +25,15 @@ main =
2425
i <- uniformR (1, Seq.length accountSeq) rng
2526
return (Seq.index accountSeq (i - 1))
2627

27-
for_ [1..20] $ \_ ->
28+
for_ [1..500] $ \_ ->
2829
forkIO $
2930
do
3031
rng <- createSystemRandom
3132
forever $
3233
do
34+
d <- uniformR (10, 50) rng
35+
threadDelay d
36+
3337
sender <- randomAccount rng
3438
recipient <- randomAccount rng
3539

@@ -53,5 +57,5 @@ main =
5357
do
5458
threadDelay 500000
5559
balances <- atomically (for accountList readTVar)
56-
hPutStrLn stderr (show balances)
60+
putStrLn (show balances)
5761
putStrLn ("Total: " ++ show (sum balances))

0 commit comments

Comments
 (0)