Advertisement
Guest User

Untitled

a guest
Mar 21st, 2019
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. {-# Language RankNTypes, MultiParamTypeClasses #-}
  2. module Data.Nonempty where
  3.  
  4. import Data.Functor.Identity
  5. import Control.FIFO.FIFO
  6. import Control.Unfoldable.Unfoldable
  7. import Control.Traversable.Traversable
  8.  
  9. infixr 5 :|
  10.  
  11. data Nonempty a = a :| Nonempty a | End a deriving (Show,Eq, Ord)
  12.  
  13. --
  14.  
  15. instance Foldable0' Nonempty where
  16. foldMap0' = foldMap0'Default
  17.  
  18. instance Get0 Nonempty where
  19. get0 = get0Default
  20.  
  21. instance Unfoldable0 Nonempty
  22.  
  23. instance Set0 Nonempty where
  24. set0 = set0Default
  25.  
  26. instance Stream Nonempty
  27.  
  28. --
  29.  
  30. instance Foldable1' Nonempty where
  31.  foldMap1' = foldMap1'Default
  32.  
  33. instance Get1 Nonempty where
  34.  get1 (x :| xs) = (x,Just xs)
  35.  get1 (End x)   = (x,Nothing)
  36.  
  37.  
  38. instance Unfoldable1 Nonempty where
  39.  unfoldr1 = unfoldr1Default
  40.  
  41. instance Set1 Nonempty where
  42.  set1 (x,Just xs) = (x :| xs)
  43.  set1 (x,Nothing) = (End x)
  44.  
  45. instance Traversable1' Nonempty where
  46. traverse1' = convert1
  47.  
  48. instance Linear Nonempty
  49.  
  50. instance Functor' Nonempty where
  51. fmap' = fmap'Default
  52.  
  53. instance Foldable' Nonempty where
  54.  foldMap' = foldMap'Default
  55.  
  56. instance Traversable' Nonempty where
  57. traverse' = traverse'Default'
  58.  
  59. --
  60.  
  61. test :: Nonempty Int
  62. test = unfoldr1 10 (\n -> if n == 0 then (n,Nothing) else (n,Just (n-1)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement