summaryrefslogtreecommitdiff
path: root/Com/Simatime/Alpha.hs
blob: cc3d23c7a2b9fb0727e96bb9aec9cb29afac68e1 (plain)
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
{-# LANGUAGE NoImplicitPrelude #-}
-- | Commonly useful functions, a Prelude replacement.
module Com.Simatime.Alpha
  (
  -- * Re-export Protolude
    module X
  -- * Applying
  , (<|)
  , (|>)
  -- * Mapping
  , (/>)
  , (</)
  , (<//)
  -- * Text
  , chomp
  , lchomp
  -- * Debugging tools
  , say
  -- * TODO: remove this
  , Prelude.read
  )
where

import           Data.Function                  ( (&) )
import           Data.Functor                   ( (<&>) )
import           Data.String
import           Data.Text                      ( Text )
import qualified Data.Text                     as Text
import qualified Data.Text.Lazy                as LazyText
import qualified Prelude
import           Protolude                     as X

-- | Debugging printf
say :: Text -> IO ()
say msg = putStrLn msg

-- $operators
--
-- Operators have a pattern to their characters
--
-- `|` normal function-level applications
-- `/` indicates doing something inside a functor
-- `<` and `>` indicate the direction in which values flow btw functions

-- | Alias for map, fmap, <$>
(</) :: Functor f => (a -> b) -> f a -> f b
(</) = fmap

-- | Double fmap. A function on the left goes "into" two functors
-- (i.e. it goes "two levels deep"), applies the function to the inner
-- values, then returns the result wrapped in the two functors.
(<//) :: (Functor f0, Functor f1) => (a -> b) -> f0 (f1 a) -> f0 (f1 b)
(<//) = fmap . fmap

-- | Normal function application. Do the right side, then pass the
-- return value to the function on the left side.
(<|) :: (a -> b) -> a -> b
(<|) = ($)
infixr 0 <|

-- | Reverse function application. Do the left side, then pass the
-- return value to the function on the right side.
(|>) :: a -> (a -> b) -> b
(|>) = (&)

-- | Alias for <&>. Can be read as "and then". Basically does into a
-- functor, does some computation, then returns the same kind of
-- functor. Could also be defined as `f >>= return . g`
(/>) :: Functor f => f a -> (a -> b) -> f b
(/>) = (<&>)

-- | Removes newlines from text.
chomp :: Text -> Text
chomp = Text.filter (/= '\n')

-- | Removes newlines from lazy text.
lchomp :: LazyText.Text -> LazyText.Text
lchomp = LazyText.filter (/= '\n')