File: Maybe.hs

package info (click to toggle)
bali-phy 4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 15,392 kB
  • sloc: cpp: 120,442; xml: 13,966; haskell: 9,975; python: 2,936; yacc: 1,328; perl: 1,169; lex: 912; sh: 343; makefile: 26
file content (43 lines) | stat: -rw-r--r-- 974 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
{-# LANGUAGE NoImplicitPrelude #-}
module Data.Maybe where

import Data.Bool
import Data.Eq

data Maybe a = Just a | Nothing

maybe n _ Nothing  = n
maybe _ f (Just x) = f x

isJust Nothing = False
isJust _       = True

isNothing Nothing = True
isNothing _       = False

fromJust (Just x) = x
-- fromJust Nothing  = error "Maybe.fromJust: Nothing"

fromMaybe d x = case x of Nothing -> d
                          Just v -> v

maybeToList Nothing = []
maybeToList (Just x) = [x]

listToMaybe [] = Nothing
listToMaybe (x:_) = Just x
-- listToMaybe = foldr (const . Just) Nothing
-- GHC uses this to fuse via the foldr/build rule.

catMaybes ls = [ x | Just x <- ls]

mapMaybes _     [] = []
mapMaybes f (x:xs) = let rs = mapMaybes f xs
                     in case f x of Nothing -> rs
                                    Just r  -> r:rs

instance Eq a => Eq (Maybe a) where
    Nothing == Nothing  =  True
    Just x  == Just y   =  x==y
    _       == _        =  False