Grid edges

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import Data.List          (sort, nub)
import Math.Geometry.Grid (Grid, neighbours, indices)

------------------------------------------------------------------------------
-- | A list of all edges in a Grid where an edge would represent the
-- connection of two tiles, for example ((0,0),(0,1)). The list is sorted and
-- duplicate edges are removed.
--
edges :: (Grid g s x, Ord x) => g -> [(x,x)]
edges grid = sort . nub . concat $ zipOrd ics neighbourList
  where           
    ics           = indices grid
    neighbourList = map (flip neighbours grid) ics
    zipOrd        = zipWith (\i is -> zipWith orderPairs (repeat i) is)

    orderPairs a b | a < b     = (a,b)
                   | otherwise = (b,a)
13:25: Warning: Use section
Found:
(flip neighbours grid)
Why not:
(`neighbours` grid)

Grid edges (improved)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import Data.List          (sort, nub)
import Math.Geometry.Grid (Grid, neighbours, indices)

------------------------------------------------------------------------------
-- | A list of all edges in a Grid where an edge represents the connection of
-- two adjacent tiles. The list is sorted and duplicate edges are removed.
--
edges :: (Grid g s x, Ord x) => g -> [(x,x)]
edges grid = sort . nub . concat $ zipOrd ics neighbourList
  where           
    ics           = indices grid
    neighbourList = map (`neighbours` grid) ics
    zipOrd        = zipWith (\i is -> zipWith orderPairs (repeat i) is)

    orderPairs a b | a < b     = (a,b)
                   | otherwise = (b,a)

Grid edges (refactored)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import Data.List          (sort, nub)
import Math.Geometry.Grid (Grid, neighbours, indices)


------------------------------------------------------------------------------
-- | A list of all edges in a Grid where an edge represents the connection of
-- two adjacent tiles. The list is sorted and duplicate edges are removed.
--
edges :: (Grid g s x, Ord x) => g -> [(x,x)]
edges grid = concat $ zipWith connect ics neighbourList
           where
             ics           = indices grid
             neighbourList = map (`neighbours` grid) ics

connect :: x -> [x] -> [(x,x)]
connect _ []     = []
connect a (b:bs) | a < b     = (a, b) : connect a bs
                 | otherwise =          connect a bs

Grid edges

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import Data.List          (sort, nub)
import Math.Geometry.Grid (Grid, neighbours, indices)


------------------------------------------------------------------------------
-- | A list of all edges in a Grid where an edge represents the connection of
-- two adjacent tiles. The list is sorted and duplicate edges are removed.
--
edges :: (Grid g s x, Ord x) => g -> [(x,x)]
edges grid = concat $ zipWith connect ics neighbourList
           where
             ics           = indices grid
             neighbourList = map (`neighbours` grid) ics

connect :: (Ord x) => x -> [x] -> [(x,x)]
connect _ []     = []
connect a (b:bs) | a < b     = (a, b) : connect a bs
                 | otherwise =          connect a bs