首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Matlab中运行

在Matlab中运行
EN

Code Review用户
提问于 2015-05-14 22:06:03
回答 1查看 1.7K关注 0票数 3

由于MATLAB有最多10个输入参数,因此可以(但很难看)以这种方式实现curry函数:

代码语言:javascript
复制
%% turns a standard function into a curried one
%  if g has 3 input arguments then you can call:
%  h = curry(g)
%  h(1)(2)(3) == g(1, 2, 3)
%  Matlab doesn't like h(1)(2)(3), but in principle it works.
function fOut = curry(fIn)
  fOut = fIn;

  % can't curry a vararg function
  if (nargin(fOut) < 0)
    return
  end

  if (nargin(fOut) == 10)
    fOut = @(bind)(curry(@(free1, free2, free3, free4, free5, free6, free7, free8, free9) (fIn(bind, free1, free2, free3, free4, free5, free6, free7, free8, free9))));
    return
  end

  if (nargin(fOut) == 9)
    fOut = @(bind)(curry(@(free1, free2, free3, free4, free5, free6, free7, free8) (fIn(bind, free1, free2, free3, free4, free5, free6, free7, free8))));
    return
  end

  if (nargin(fOut) == 8)
    fOut = @(bind)(curry(@(free1, free2, free3, free4, free5, free6, free7) (fIn(bind, free1, free2, free3, free4, free5, free6, free7))));
    return
  end

  if (nargin(fOut) == 7)
    fOut = @(bind)(curry(@(free1, free2, free3, free4, free5, free6) (fIn(bind, free1, free2, free3, free4, free5, free6))));
    return
  end

  if (nargin(fOut) == 6)
    fOut = @(bind)(curry(@(free1, free2, free3, free4, free5) (fIn(bind, free1, free2, free3, free4, free5))));
    return
  end

  if (nargin(fOut) == 5)
    fOut = @(bind)(curry(@(free1, free2, free3, free4) (fIn(bind, free1, free2, free3, free4))));
    return
  end

  if (nargin(fOut) == 4)
    fOut = @(bind)(curry(@(free1, free2, free3) (fIn(bind, free1, free2, free3))));
    return
  end

  if (nargin(fOut) == 3)
    fOut = @(bind)(curry(@(free1, free2) (fIn(bind, free1, free2))));
    return
  end

  if (nargin(fOut) == 2)
    fOut = @(bind)(curry(@(free1) (fIn(bind, free1))));
    return
  end

end

有什么不那么可怕的方法吗?

最初,我尝试使用varargin,这是我假设答案会出现的方式。我当时的经历是这样的:

代码语言:javascript
复制
function fOut = curry(fIn)
  fOut = fIn;

  % can't curry a vararg function (nargin = -1) or a function with less than
  % arity of 2
  if (nargin(fOut) < 2)
    return
  end

  fOut = @(bind)(curry(@(varargin)(fIn(bind, varargin{:}))));

end
EN

回答 1

Code Review用户

回答已采纳

发布于 2015-05-15 00:48:37

我要做的第一件事是使用switch语句而不是if语句。它清理了一堆东西。

代码语言:javascript
复制
function fOut = curry(fIn)
fOut = fIn;
switch nargin(fOut)
  case 5
    fOut = @(bind)(curry(@(free1, free2, free3, free4) (fIn(bind, free1, free2, free3, free4))));
  case 4
    fOut = @(bind)(curry(@(free1, free2, free3) (fIn(bind, free1, free2, free3))));
  case 3
    fOut = @(bind)(curry(@(free1, free2) (fIn(bind, free1, free2))));
  case 2
    fOut = @(bind)(curry(@(free1) (fIn(bind, free1))));
  otherwise
    % Do nothing, 
end    
return

另外,在Matlab中有10多个输入是可以的:

代码语言:javascript
复制
f = @(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12)cat(2,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12);

曾在R2008b和R2012b工作过:

代码语言:javascript
复制
f(1,2,3,4,5,6,7,8,9,0,1,2) 
ans = 
1   2   3   4   5   6   7   8   9   0   1   2

值得注意的是,在curry上的功能库中有一个MatlabCentral函数。(我本可以对此发表评论的,但我还没有代表.)

票数 4
EN
页面原文内容由Code Review提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codereview.stackexchange.com/questions/90781

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档