module Graphics.Gnuplot.Utility where

import Text.Printf (printf, )

import qualified Data.Char as Char
import Data.List (intersperse, )


functionToGraph :: [x] -> (x -> y) -> [(x,y)]
functionToGraph :: [x] -> (x -> y) -> [(x, y)]
functionToGraph [x]
args x -> y
f = (x -> (x, y)) -> [x] -> [(x, y)]
forall a b. (a -> b) -> [a] -> [b]
map (\x
x -> (x
x, x -> y
f x
x)) [x]
args
-- functionToGraph args f = map swap $ attachKey f args


linearScale :: Fractional a => Integer -> (a,a) -> [a]
linearScale :: Integer -> (a, a) -> [a]
linearScale Integer
n (a
x0,a
x1) =
   (Integer -> a) -> [Integer] -> [a]
forall a b. (a -> b) -> [a] -> [b]
map (\Integer
m -> a
x0 a -> a -> a
forall a. Num a => a -> a -> a
+ (a
x1a -> a -> a
forall a. Num a => a -> a -> a
-a
x0) a -> a -> a
forall a. Num a => a -> a -> a
* Integer -> a
forall a b. (Integral a, Num b) => a -> b
fromIntegral Integer
m a -> a -> a
forall a. Fractional a => a -> a -> a
/ Integer -> a
forall a b. (Integral a, Num b) => a -> b
fromIntegral Integer
n) [Integer
0..Integer
n]


showTriplet :: (Show a, Show b, Show c) => (a,b,c) -> String
showTriplet :: (a, b, c) -> String
showTriplet (a
x,b
y,c
z) = [String] -> String
unwords [a -> String
forall a. Show a => a -> String
show a
x, b -> String
forall a. Show a => a -> String
show b
y, c -> String
forall a. Show a => a -> String
show c
z]


commaConcat, semiColonConcat :: [String] -> String
commaConcat :: [String] -> String
commaConcat = [String] -> String
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat ([String] -> String)
-> ([String] -> [String]) -> [String] -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> [String] -> [String]
forall a. a -> [a] -> [a]
intersperse String
", "
semiColonConcat :: [String] -> String
semiColonConcat = [String] -> String
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat ([String] -> String)
-> ([String] -> [String]) -> [String] -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> [String] -> [String]
forall a. a -> [a] -> [a]
intersperse String
"; "


{-
In former versions this was simply 'show'.
However, Haskell formats non-ASCII characters with decimal backslash sequences
whereas gnuplot expects octal ones.
gnuplot does also not accept bigger octal numbers.
The current version writes non-ASCII printable characters
with the system default encoding.
-}
quote, _quote :: String -> String
quote :: String -> String
quote String
str = Char
'"' Char -> String -> String
forall a. a -> [a] -> [a]
: (String -> String -> String) -> String -> [String] -> String
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr String -> String -> String
forall a. [a] -> [a] -> [a]
(++) String
"\"" ((Char -> String) -> String -> [String]
forall a b. (a -> b) -> [a] -> [b]
map Char -> String
escapeChar String
str)
_quote :: String -> String
_quote String
str = Char
'"' Char -> String -> String
forall a. a -> [a] -> [a]
: (Char -> String) -> String -> String
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap Char -> String
escapeChar String
str String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"\""

escapeChar :: Char -> String
escapeChar :: Char -> String
escapeChar Char
c =
   case Char
c of
      Char
'"'  -> String
"\\\""
      Char
'\\' -> String
"\\\\"
      Char
'\t' -> String
"\\t"
      Char
'\n' -> String
"\\n"
      Char
_ -> if Char -> Bool
Char.isPrint Char
c then [Char
c] else String -> Int -> String
forall r. PrintfType r => String -> r
printf String
"\\%o" (Int -> String) -> Int -> String
forall a b. (a -> b) -> a -> b
$ Char -> Int
Char.ord Char
c

assembleCells :: [[ShowS]] -> String
assembleCells :: [[String -> String]] -> String
assembleCells [[String -> String]]
ps =
   ((String -> String) -> String -> String)
-> String -> [String -> String] -> String
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (String -> String) -> String -> String
forall a b. (a -> b) -> a -> b
($) String
""
      (([String -> String] -> [String -> String])
-> [[String -> String]] -> [String -> String]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap
         (\[String -> String]
p ->
            (String -> String) -> [String -> String] -> [String -> String]
forall a. a -> [a] -> [a]
intersperse (String -> String -> String
showString String
", ") [String -> String]
p [String -> String] -> [String -> String] -> [String -> String]
forall a. [a] -> [a] -> [a]
++
            [String -> String -> String
showString String
"\n"])
         [[String -> String]]
ps)


listFromMaybeWith :: (a -> b) -> Maybe a -> [b]
listFromMaybeWith :: (a -> b) -> Maybe a -> [b]
listFromMaybeWith a -> b
f = [b] -> (a -> [b]) -> Maybe a -> [b]
forall b a. b -> (a -> b) -> Maybe a -> b
maybe [] ((b -> [b] -> [b]
forall a. a -> [a] -> [a]
:[]) (b -> [b]) -> (a -> b) -> a -> [b]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> b
f)

formatBool :: String -> Bool -> String
formatBool :: String -> Bool -> String
formatBool String
name Bool
b =
   if Bool
b then String
name else String
"no"String -> String -> String
forall a. [a] -> [a] -> [a]
++String
name