[英]Given a function g :: a -> b -> … -> z, how can I use it like h :: (a, b, …) -> z?
Background: I'm working with wxHaskell's fileOpenDialog, which takes 6 non-obvious parameters (curried). 背景:我正在使用wxHaskell的fileOpenDialog,它有6个非显而易见的参数(curried)。 My code is currently: 我的代码目前是:
maybePath <- fileOpenDialog w useLastSelectedDir canSelectReadOnly
frameName possibleFiles initialDir defaultFilename
with a let
statement above that to define all my parameters. 用上面的let
语句来定义我的所有参数。 What I would love to do though, is save my parameters somewhere. 我喜欢做的是在某处保存我的参数。 I somewhat understand why Haskell would't support something like say: 我有点理解为什么Haskell不会支持像:
myParams = ( ... ) -- tuple of params
maybePath <- fileOpenDialog myParams
However, is there something close to this in the spirit of not repeating myself? 但是,在不重复自己的精神下,是否有接近这一点的东西?
It seems like you would naturally like the input to this function to be a record of parameters: 看起来您自然希望此函数的输入是参数记录:
{-# LANGUAGE RecordWildCards #-}
-- Defined by your library
foo :: String -> Int -> IO ()
foo = ...
data Opts = Opts { optString :: String, optInt :: Int }
bar :: Opts -> IO ()
bar Opts{..} = foo optString optInt
Now, you can use any of the following equivalent syntaxes (some use RecordWildCards): 现在,您可以使用以下任何等效语法(一些使用RecordWildCards):
main = do
let optString = <...>
optInt = <...>
bar Opts{..}
main = do
let x = <...>
y = <...>
myParams = Opts x y
bar myParams
main = do
bar $ Opts
{ optString = <...>
, optInt = <...>
}
main = do
let optString = <...>
optInt = <...>
myParams = Opts{..}
bar myParams
There is also the (less clean) possibility of writing an uncurry
variant (see here ) having more arguments: 还有(不太干净)编写具有更多参数的不uncurry
变体(见此处 )的可能性:
uncurry6 :: (a -> b -> c -> d -> e -> f -> g) -> ((a,b,c,d,e,f) -> g)
uncurry6 fun (a,b,c,d,e,f) = fun a b c d e f
Having that, uncurry6 fileOpenDialog
will make fileOpenDialog
accept a 6-tuple. 有了这个, uncurry6 fileOpenDialog
将使fileOpenDialog
接受一个6元组。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.