Created
May 12, 2018 03:35
-
-
Save russmatney/d478fda180c5f8122338ac2b7920384f to your computer and use it in GitHub Desktop.
This file contains 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
previewExamples :: IO () | |
previewExamples = do | |
let maybeIntA = Just 1 | |
-- Have to tell the compiler this was a 'Maybe Int' for it to be printable | |
maybeIntB = Nothing :: Maybe Int | |
print "maybeIntA" | |
print $ maybeIntA ^? _Just | |
-- Just 1 | |
print "maybeIntB" | |
print $ maybeIntB ^? _Just | |
-- Nothing | |
let justiceCity = Just 1 | |
crashCity = Nothing :: Maybe Int | |
print "Unwrap this Maybe Int or die trying!" | |
print $ justiceCity ^?! _Just | |
-- 1 | |
print "Crash city!" | |
-- print $ crashCity ^?! _Just | |
-- This will throw an 'empty fold' exception. `^?!` can be useful for | |
-- forcing Maybes to unwrap when writing tests. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment