]> gitweb.michael.orlitzky.com - spline3.git/blob - src/Misc.hs
4b285e4cc3b0cff1f8344aa09c905ed6b0c3fe99
[spline3.git] / src / Misc.hs
1 {-# LANGUAGE BangPatterns #-}
2 -- | The Misc module contains helper functions that seem out of place
3 -- anywhere else.
4 module Misc
5 where
6
7 import qualified Data.Vector as V (Vector, elem, empty, filter)
8 import Test.Framework (Test, testGroup)
9 import Test.Framework.Providers.HUnit (testCase)
10 import Test.Framework.Providers.QuickCheck2 (testProperty)
11 import Test.HUnit (Assertion, assertEqual)
12 import Test.QuickCheck
13
14
15 -- | The standard factorial function. See
16 -- <http://www.willamette.edu/~fruehr/haskell/evolution.html> for
17 -- possible improvements.
18 --
19 -- Examples:
20 --
21 -- >>> factorial 0
22 -- 1
23 --
24 -- >>> factorial 4
25 -- 24
26 --
27 factorial :: Int -> Int
28 factorial !n
29 | n > 20 = error "integer overflow in factorial function"
30 | otherwise = go 1 n
31 where go !acc !i
32 | i <= 1 = acc
33 | otherwise = go (acc * i) (i - 1)
34
35 -- | Takes a three-dimensional list, and flattens it into a
36 -- one-dimensional one.
37 --
38 -- Examples:
39 --
40 -- >>> flatten [ [[1,2], [3,4]], [[5,6], [7,8]] ]
41 -- [1,2,3,4,5,6,7,8]
42 --
43 flatten :: [[[a]]] -> [a]
44 flatten xs = concat $ concat xs
45
46
47 -- | Switch the x and z dimensions of a three-dimensional list.
48 transpose_xz :: [[[a]]] -> [[[a]]]
49 transpose_xz [] = []
50 transpose_xz [[]] = [[]]
51 transpose_xz [[[]]] = [[[]]]
52 transpose_xz m =
53 [[[ m !! x !! y !! z | x <- [0..xsize]]
54 | y <- [0..ysize]]
55 | z <- [0..zsize]]
56 where
57 zsize = (length m) - 1
58 ysize = length (head m) - 1
59 xsize = length (head $ head m) - 1
60
61 -- | Takes a list, and returns True if its elements are pairwise
62 -- equal. Returns False otherwise.
63 all_equal :: (Eq a) => [a] -> Bool
64 all_equal xs =
65 all (== first_element) other_elements
66 where
67 first_element = head xs
68 other_elements = tail xs
69
70
71 -- | Returns 'True' if the vectors xs and ys are disjoint, 'False'
72 -- otherwise.
73 --
74 -- Examples:
75 --
76 -- >>> let xs = Data.Vector.fromList [1,2,3]
77 -- >>> let ys = Data.Vector.fromList [4,5,6]
78 -- >>> disjoint xs ys
79 -- True
80 --
81 -- >>> let ys = Data.Vector.fromList [3,4,5]
82 -- >>> disjoint xs ys
83 -- False
84 --
85 disjoint :: (Eq a) => V.Vector a -> V.Vector a -> Bool
86 disjoint xs ys =
87 intersect xs ys == V.empty
88 where
89 intersect :: (Eq a) => V.Vector a -> V.Vector a -> V.Vector a
90 intersect ws zs =
91 V.filter (`V.elem` zs) ws
92
93 prop_factorial_greater :: Int -> Property
94 prop_factorial_greater n =
95 n <= 20 ==> factorial n >= n
96
97
98 test_flatten1 :: Assertion
99 test_flatten1 =
100 assertEqual "flatten actually works" expected_list actual_list
101 where
102 target = [[[1::Int]], [[2, 3]]]
103 expected_list = [1, 2, 3]
104 actual_list = flatten target
105
106
107 misc_tests :: Test.Framework.Test
108 misc_tests =
109 testGroup "Misc Tests" [
110 testCase "flatten (1)" test_flatten1 ]
111
112
113 misc_properties :: Test.Framework.Test
114 misc_properties =
115 testGroup "Misc Properties" [
116 testProperty "factorial greater" prop_factorial_greater ]