Skip to content

Commit 3068661

Browse files
committed
adding utility type and converter for varargs
1 parent 361e677 commit 3068661

File tree

4 files changed

+20
-6
lines changed

4 files changed

+20
-6
lines changed

Foreign/Matlab/Array.hsc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,8 @@ foreign import ccall unsafe mxDestroyArray :: MXArrayPtr -> IO ()
128128
freeMXArray :: MXArray a -> MIO ()
129129
freeMXArray a = withMXArray a mxDestroyArray
130130

131-
-- |Create and populate an MXArray in one go.
131+
-- | Create and populate an MXArray in one go. Named without 'mx' due to possible
132+
-- | conformity to a typeclass function.
132133
fromListIO :: (Foldable t, MXArrayComponent a) => t a -> MIO (MXArray a)
133134
fromListIO xs = do
134135
arr <- createMXArray [length xs]
@@ -138,7 +139,8 @@ fromListIO xs = do
138139
xsList = toList xs
139140

140141
-- | 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+
-- | to a MATLAB cell array of strings. Named in conjunction with `fromListIO`, which is used
143+
-- | as part of the implementation.
142144
cellFromListsIO :: (Traversable s, Foldable t, MXArrayComponent a) => s (t a) -> MIO (MXArray MCell)
143145
cellFromListsIO xss = do
144146
listOfStructArrays <- sequence $ fromListIO <$> xss

Foreign/Matlab/Engine/Wrappers.hs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,15 @@ module Foreign.Matlab.Engine.Wrappers (
1111
, getArrayFromByteStream
1212
, getByteStreamFromArray
1313
, MEither(..), isMLeft, isMRight
14+
, VarArgIn, mxVarArgs
1415
) where
1516

16-
import Foreign.Matlab
17-
import Foreign.Matlab.Engine
18-
import Path
17+
import qualified Data.Map.Strict as DM
18+
import Foreign.Matlab
19+
import Foreign.Matlab.Engine
20+
import Path
21+
22+
type VarArgIn = DM.Map String MAnyArray
1923

2024
-- | We require an absolute path in this case
2125
addpath :: Engine -> Path Abs Dir -> IO ()
@@ -55,3 +59,10 @@ isMRight :: MEither -> IO Bool
5559
isMRight me = do
5660
sFields <- mStructFields $ unMXEither me
5761
pure $ "right" `elem` sFields
62+
63+
-- | Utility function to create an eval-able list of arguments for a vararg map.
64+
mxVarArgs :: VarArgIn -> [EngineEvalArg MAny]
65+
mxVarArgs varargin = DM.toList varargin >>= kvToArg
66+
where
67+
kvToArg :: (String, MAnyArray) -> [EngineEvalArg MAny]
68+
kvToArg kv = [EvalStr $ fst kv, EvalArray $ snd kv]

Foreign/Matlab/Types.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ module Foreign.Matlab.Types (
3535

3636
-- * Opaque types
3737
MXArray,
38-
MAnyArray,
38+
MAny, MAnyArray,
3939
MCell(MCell), mCell,
4040
MStruct,
4141
MFun

matlab.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ library
6868

6969
if flag(engine)
7070
Exposed-modules: Foreign.Matlab.Engine
71+
Build-depends: containers
7172
extra-libraries: eng
7273

7374
Source-repository head

0 commit comments

Comments
 (0)