Advertisement
Guest User

Untitled

a guest
May 6th, 2016
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. {-#LANGUAGE BangPatterns #-}
  2. import Control.Foldl (Fold(..))
  3. import qualified Control.Foldl as L
  4. import qualified Data.Vector.Unboxed as V
  5. import qualified Data.Sequence as Seq
  6. import Control.Applicative
  7. import Criterion.Main
  8. import System.Environment
  9. a >< b = fmap (,) a <*> b
  10.  
  11. main = do
  12. f:n:_ <- getArgs
  13. case f of
  14. "v" -> do
  15. let folder = L.purely_ (\a b -> V.foldl a b (V.enumFromTo 1 (10^6::Int)))
  16. case n of
  17. "1" -> print $ folder $ index (10^4+1)
  18. "2" -> print $ folder $ index (10^4) >< index (10^4+1)
  19. "3" -> print $ folder $ index (10^4-1) >< index (10^4) >< index (10^4+1)
  20. "s" -> do
  21. let folder = flip L.fold (Seq.replicate (10^6) (1::Int))
  22. case n of
  23. "1" -> print $ folder $ index (10^4+1)
  24. "2" -> print $ folder $ index (10^4) >< index (10^4+1)
  25. "3" -> print $ folder $ index (10^4-1) >< index (10^4) >< index (10^4+1)
  26.  
  27. "l" -> do
  28. let folder = flip L.fold (enumFromTo 1 (10^6::Int))
  29. case n of
  30. "1" -> print $ folder $ index (10^4+1)
  31. "2" -> print $ folder $ index (10^4) >< index (10^4+1)
  32. "3" -> print $ folder $ index (10^4-1) >< index (10^4) >< index (10^4+1)
  33.  
  34.  
  35. -- L.fold folds $ Seq.replicate (10^4) (1::Int) where
  36. -- -- folds = (index (10^6+1))
  37. -- folds = liftA2 (,) (index (10^2-1)) $ liftA2 (,) (index (10^2)) (index (10^2+1))
  38.  
  39. data IndexSt a = NotYet !Int | Yet !a
  40.  
  41. index :: Int -> Fold a (Maybe a)
  42. index n = Fold step (NotYet n) done where
  43. step (Yet a) _ = Yet a
  44. step (NotYet 0) a = Yet a
  45. step (NotYet n) a = NotYet (n-1)
  46. done (NotYet _) = Nothing
  47. done (Yet a) = Just a
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement