Skip to content

Commit 361e677

Browse files
committed
added cellFromListsIO for interop with lists of strings
1 parent 99c42ed commit 361e677

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

Foreign/Matlab/Array.hsc

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ module Foreign.Matlab.Array (
3131
-- | array list access
3232
mxArrayGetList, mxArraySetList,
3333
mxArrayGetAll, mxArraySetAll,
34-
fromListIO,
34+
fromListIO, cellFromListsIO,
3535

3636
-- * Struct access
3737
-- |Structs in Matlab are always arrays, and so can be accessed using most array accessors.
@@ -50,6 +50,7 @@ module Foreign.Matlab.Array (
5050
) where
5151

5252
import Control.Monad
53+
import Data.Foldable (toList)
5354
import Foreign
5455
import Foreign.C.String
5556
import Foreign.C.Types
@@ -128,10 +129,21 @@ freeMXArray :: MXArray a -> MIO ()
128129
freeMXArray a = withMXArray a mxDestroyArray
129130

130131
-- |Create and populate an MXArray in one go.
131-
fromListIO :: MXArrayComponent a => [a] -> MIO (MXArray a)
132+
fromListIO :: (Foldable t, MXArrayComponent a) => t a -> MIO (MXArray a)
132133
fromListIO xs = do
133134
arr <- createMXArray [length xs]
134-
mxArraySetAll arr xs
135+
mxArraySetAll arr xsList
136+
pure arr
137+
where
138+
xsList = toList xs
139+
140+
-- | Like fromListIO but wraps elements in a cell. Most useful for converting a list of strings
141+
-- | to a MATLAB cell array of strings.
142+
cellFromListsIO :: (Traversable s, Foldable t, MXArrayComponent a) => s (t a) -> MIO (MXArray MCell)
143+
cellFromListsIO xss = do
144+
listOfStructArrays <- sequence $ fromListIO <$> xss
145+
arr <- createMXArray [length xss]
146+
mxArraySetAll arr (toList $ (MCell . anyMXArray) <$> listOfStructArrays)
135147
pure arr
136148

137149
-- |The class of standardly typeable array elements

0 commit comments

Comments
 (0)