-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtils.hs
More file actions
80 lines (65 loc) · 2.5 KB
/
Utils.hs
File metadata and controls
80 lines (65 loc) · 2.5 KB
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
79
80
-- This file is part of KSQuant2.
-- Copyright (c) 2010 - 2011, Kilian Sprotte. All rights reserved.
-- This program is free software: you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation, either version 3 of the License, or
-- (at your option) any later version.
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-- GNU General Public License for more details.
-- You should have received a copy of the GNU General Public License
-- along with this program. If not, see <http://www.gnu.org/licenses/>.
-- | Some utilities.
module Utils (neighbours
, isForAllNeighbours
, stickToLast
, repeatList
, rationalPairToTimePair
, appendNewline
, dxsToXs
, oneOfEq)
where
import qualified Types as T (Time)
-- | Return adjacent elements as pairs.
--
-- >>> neighbours [1,2,3]
-- [(1,2),(2,3)]
-- >>> neighbours [1,2]
-- [(1,2)]
-- >>> neighbours [1]
-- []
-- >>> neighbours []
-- []
neighbours :: [a] -> [(a, a)]
neighbours list = zip list (tail list)
-- | Test if the given binary predicate is satisfied for
-- all neighbours of the given list.
--
-- >>> isForAllNeighbours (<) [1,2,3]
-- True
isForAllNeighbours :: (a -> a -> Bool) -> [a] -> Bool
isForAllNeighbours p list = all (uncurry p) (neighbours list)
-- | Build an infinite list by endlessly repeating the last element.
stickToLast :: [a] -> [a]
stickToLast list = list ++ repeat (last list)
-- | Expand first list by repeating elements according
-- to the repetition counts given by the second list.
repeatList :: (Num b, Ord b) => [a] -> [b] -> [a]
repeatList [] _ = []
repeatList _ [] = []
repeatList (x:xs) (1:rs) = x:repeatList xs rs
repeatList (x:xs) (r:rs) | r > 1 = x:repeatList (x:xs) (r-1:rs)
| r < 1 = repeatList xs rs
| otherwise = undefined
rationalToTime :: Rational -> T.Time
rationalToTime = fromRational
rationalPairToTimePair :: (Rational, Rational) -> (T.Time, T.Time)
rationalPairToTimePair (x,y) = (rationalToTime x, rationalToTime y)
appendNewline :: String -> String
appendNewline s = s ++ "\n"
dxsToXs :: [Rational] -> [Rational]
dxsToXs = scanl (+) 0
oneOfEq :: (Show a, Eq a) => a -> a -> a
oneOfEq a b | a == b = a
| otherwise = error $ "expected to be Eq: " ++ show a ++ " " ++ show b