Skip to content

Commit

Permalink
dynamic.hs
Browse files Browse the repository at this point in the history
  • Loading branch information
chris-martin committed Sep 2, 2019
1 parent 26b9cea commit 33a1141
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
25 changes: 25 additions & 0 deletions dynamic.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import Data.Dynamic
import Data.Foldable

mixedList =
[ toDyn True
, toDyn (5 :: Integer)
, toDyn "hey"
]

main =
for_ mixedList $ \d ->
putStrLn (message d)

recognizeType d =
asum
[ (fromDynamic d :: Maybe Integer) >>= \x ->
Just (show x ++ " is an integer")
, (fromDynamic d :: Maybe Bool) >>= \x ->
Just ("The answer is " ++ (if x then "yes" else "no"))
]

message d =
case (recognizeType d) of
Just x -> x
Nothing -> "Unrecognized type: " ++ show (dynTypeRep d)
3 changes: 3 additions & 0 deletions outputs/dynamic.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
The answer is yes
5 is an integer
Unrecognized type: [Char]
1 change: 1 addition & 0 deletions tools/outputs.nix
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ in
(run' "branching.txt" ../branching.hs { sed = ["s!^It's .* noon$!It's ... noon!"]; })
(run "common-types.txt" ../common-types.hs)
(run "crypto-hashing.txt" ../crypto-hashing.hs)
(run "dynamic.txt" ../dynamic.hs)
(run "for-loops.txt" ../for-loops.hs)
(run "hashing.txt" ../hashing.hs)
(run "hello-world.txt" ../hello-world.hs)
Expand Down

0 comments on commit 33a1141

Please sign in to comment.