{-# LANGUAGE ViewPatterns #-}
module Text.Pandoc.Readers.Odt.Generic.Utils
( uncurry3
, uncurry4
, uncurry5
, uncurry6
, swap
, reverseComposition
, tryToRead
, Lookupable(..)
, readLookupables
, readLookupable
, readPercent
, findBy
, swing
, composition
) where
import Control.Category (Category, (<<<), (>>>))
import qualified Control.Category as Cat (id)
import Control.Monad (msum)
import qualified Data.Foldable as F (Foldable, foldr)
import Data.Maybe
composition :: (Category cat, F.Foldable f) => f (cat a a) -> cat a a
composition :: f (cat a a) -> cat a a
composition = (cat a a -> cat a a -> cat a a)
-> cat a a -> f (cat a a) -> cat a a
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
F.foldr cat a a -> cat a a -> cat a a
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
(<<<) cat a a
forall k (cat :: k -> k -> *) (a :: k). Category cat => cat a a
Cat.id
reverseComposition :: (Category cat, F.Foldable f) => f (cat a a) -> cat a a
reverseComposition :: f (cat a a) -> cat a a
reverseComposition = (cat a a -> cat a a -> cat a a)
-> cat a a -> f (cat a a) -> cat a a
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
F.foldr cat a a -> cat a a -> cat a a
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
(>>>) cat a a
forall k (cat :: k -> k -> *) (a :: k). Category cat => cat a a
Cat.id
swing :: (((a -> b) -> b) -> c -> d) -> c -> a -> d
swing :: (((a -> b) -> b) -> c -> d) -> c -> a -> d
swing = (a -> c -> d) -> c -> a -> d
forall a b c. (a -> b -> c) -> b -> a -> c
flip((a -> c -> d) -> c -> a -> d)
-> ((((a -> b) -> b) -> c -> d) -> a -> c -> d)
-> (((a -> b) -> b) -> c -> d)
-> c
-> a
-> d
forall b c a. (b -> c) -> (a -> b) -> a -> c
.((((a -> b) -> b) -> c -> d) -> (a -> (a -> b) -> b) -> a -> c -> d
forall b c a. (b -> c) -> (a -> b) -> a -> c
.((a -> b) -> a -> b) -> a -> (a -> b) -> b
forall a b c. (a -> b -> c) -> b -> a -> c
flip (a -> b) -> a -> b
forall a. a -> a
id)
tryToRead :: (Read r) => String -> Maybe r
tryToRead :: String -> Maybe r
tryToRead = ReadS r
forall a. Read a => ReadS a
reads ReadS r -> ([(r, String)] -> Maybe r) -> String -> Maybe r
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> [(r, String)] -> Maybe (r, String)
forall a. [a] -> Maybe a
listToMaybe ([(r, String)] -> Maybe (r, String))
-> (Maybe (r, String) -> Maybe r) -> [(r, String)] -> Maybe r
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> ((r, String) -> r) -> Maybe (r, String) -> Maybe r
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (r, String) -> r
forall a b. (a, b) -> a
fst
readPercent :: ReadS Int
readPercent :: ReadS Int
readPercent String
s = [ (Int
i,String
s') | (Int
i , String
r ) <- ReadS Int
forall a. Read a => ReadS a
reads String
s
, (String
"%" , String
s') <- ReadS String
lex String
r
]
class Lookupable a where
lookupTable :: [(String, a)]
readLookupables :: (Lookupable a) => String -> [(a,String)]
readLookupables :: String -> [(a, String)]
readLookupables String
s = [ (a
a,String
rest) | (String
word,String
rest) <- ReadS String
lex String
s,
a
a <- Maybe a -> [a]
forall a. Maybe a -> [a]
maybeToList (String -> [(String, a)] -> Maybe a
forall a b. Eq a => a -> [(a, b)] -> Maybe b
lookup String
word [(String, a)]
forall a. Lookupable a => [(String, a)]
lookupTable)
]
readLookupable :: (Lookupable a) => String -> Maybe a
readLookupable :: String -> Maybe a
readLookupable String
s = [Maybe a] -> Maybe a
forall (t :: * -> *) (m :: * -> *) a.
(Foldable t, MonadPlus m) =>
t (m a) -> m a
msum
([Maybe a] -> Maybe a) -> [Maybe a] -> Maybe a
forall a b. (a -> b) -> a -> b
$ ((String, String) -> Maybe a) -> [(String, String)] -> [Maybe a]
forall a b. (a -> b) -> [a] -> [b]
map ((String -> [(String, a)] -> Maybe a
forall a b. Eq a => a -> [(a, b)] -> Maybe b
`lookup` [(String, a)]
forall a. Lookupable a => [(String, a)]
lookupTable)(String -> Maybe a)
-> ((String, String) -> String) -> (String, String) -> Maybe a
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(String, String) -> String
forall a b. (a, b) -> a
fst)
([(String, String)] -> [Maybe a])
-> [(String, String)] -> [Maybe a]
forall a b. (a -> b) -> a -> b
$ ReadS String
lex String
s
uncurry3 :: (a->b->c -> z) -> (a,b,c ) -> z
uncurry4 :: (a->b->c->d -> z) -> (a,b,c,d ) -> z
uncurry5 :: (a->b->c->d->e -> z) -> (a,b,c,d,e ) -> z
uncurry6 :: (a->b->c->d->e->f -> z) -> (a,b,c,d,e,f ) -> z
uncurry3 :: (a -> b -> c -> z) -> (a, b, c) -> z
uncurry3 a -> b -> c -> z
fun (a
a,b
b,c
c ) = a -> b -> c -> z
fun a
a b
b c
c
uncurry4 :: (a -> b -> c -> d -> z) -> (a, b, c, d) -> z
uncurry4 a -> b -> c -> d -> z
fun (a
a,b
b,c
c,d
d ) = a -> b -> c -> d -> z
fun a
a b
b c
c d
d
uncurry5 :: (a -> b -> c -> d -> e -> z) -> (a, b, c, d, e) -> z
uncurry5 a -> b -> c -> d -> e -> z
fun (a
a,b
b,c
c,d
d,e
e ) = a -> b -> c -> d -> e -> z
fun a
a b
b c
c d
d e
e
uncurry6 :: (a -> b -> c -> d -> e -> f -> z) -> (a, b, c, d, e, f) -> z
uncurry6 a -> b -> c -> d -> e -> f -> z
fun (a
a,b
b,c
c,d
d,e
e,f
f ) = a -> b -> c -> d -> e -> f -> z
fun a
a b
b c
c d
d e
e f
f
swap :: (a,b) -> (b,a)
swap :: (a, b) -> (b, a)
swap (a
a,b
b) = (b
b,a
a)
findBy :: (a -> Maybe b) -> [a] -> Maybe b
findBy :: (a -> Maybe b) -> [a] -> Maybe b
findBy a -> Maybe b
_ [] = Maybe b
forall a. Maybe a
Nothing
findBy a -> Maybe b
f ((a -> Maybe b
f -> Just b
x):[a]
_ ) = b -> Maybe b
forall a. a -> Maybe a
Just b
x
findBy a -> Maybe b
f ( a
_:[a]
xs) = (a -> Maybe b) -> [a] -> Maybe b
forall a b. (a -> Maybe b) -> [a] -> Maybe b
findBy a -> Maybe b
f [a]
xs