We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent d20bae2 commit e5d80f3Copy full SHA for e5d80f3
CHANGELOG.md
@@ -7,6 +7,7 @@ Notable changes to this project are documented in this file. The format is based
7
Breaking changes:
8
9
New features:
10
+- Added `transpose` to `Array` (#225 by @newlandsvalley and @JordanMartinez)
11
12
Bugfixes:
13
src/Data/Array.purs
@@ -84,6 +84,7 @@ module Data.Array
84
, foldMap
85
, fold
86
, intercalate
87
+ , transpose
88
, scanl
89
, scanr
90
@@ -781,6 +782,46 @@ fold = F.fold
781
782
intercalate :: forall a. Monoid a => a -> Array a -> a
783
intercalate = F.intercalate
784
785
+-- | The 'transpose' function transposes the rows and columns of its argument.
786
+-- | For example,
787
+-- |
788
+-- | ```purescript
789
+-- | transpose
790
+-- | [ [1, 2, 3]
791
+-- | , [4, 5, 6]
792
+-- | ] ==
793
+-- | [ [1, 4]
794
+-- | , [2, 5]
795
+-- | , [3, 6]
796
+-- | ]
797
+-- | ```
798
799
+-- | If some of the rows are shorter than the following rows, their elements are skipped:
800
801
802
803
+-- | [ [10, 11]
804
+-- | , [20]
805
+-- | , [30, 31, 32]
806
807
+-- | [ [10, 20, 30]
808
+-- | , [11, 31]
809
+-- | , [32]
810
811
812
+transpose :: forall a. Array (Array a) -> Array (Array a)
813
+transpose xs = go 0 []
814
+ where
815
+ go :: Int -> Array (Array a) -> Array (Array a)
816
+ go idx allArrays = case buildNext idx of
817
+ Nothing -> allArrays
818
+ Just next -> go (idx + 1) (snoc allArrays next)
819
+
820
+ buildNext :: Int -> Maybe (Array a)
821
+ buildNext idx = do
822
+ xs # flip foldl Nothing \acc nextArr -> do
823
+ maybe acc (\el -> Just $ maybe [el] (flip snoc el) acc) $ index nextArr idx
824
825
-- | Fold a data structure from the left, keeping all intermediate results
826
-- | instead of only the final result. Note that the initial value does not
827
-- | appear in the result (unlike Haskell's `Prelude.scanl`).
test/Test/Data/Array.purs
@@ -265,6 +265,20 @@ testArray = do
265
assert $ A.modifyAtIndices [0, 2, 8] not [true, true, true, true] ==
266
[false, true, false, true]
267
268
+ log "transpose swaps rows and columns for a regular two-dimension array"
269
+ assert $ A.transpose [[1,2,3], [4,5,6], [7,8,9]] ==
270
+ [[1,4,7], [2,5,8], [3,6,9]]
271
272
+ log "transpose skips elements when rows don't match"
273
+ assert $ A.transpose [[10,11], [20], [30,31,32]] ==
274
+ [[10,20,30], [11,31], [32]]
275
276
+ log "transpose [] == []"
277
+ assert $ A.transpose [] == ([] :: Array (Array Int))
278
279
+ log "transpose (singleton []) == []"
280
+ assert $ A.transpose (A.singleton []) == ([] :: Array (Array Int))
281
282
log "scanl should return an array that stores the accumulated value at each step"
283
assert $ A.scanl (+) 0 [1,2,3] == [1, 3, 6]
284
assert $ A.scanl (-) 10 [1,2,3] == [9, 7, 4]
0 commit comments