Skip to content

Instantly share code, notes, and snippets.

@russmatney
Created May 12, 2018 03:35
Show Gist options
  • Save russmatney/d478fda180c5f8122338ac2b7920384f to your computer and use it in GitHub Desktop.
Save russmatney/d478fda180c5f8122338ac2b7920384f to your computer and use it in GitHub Desktop.
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