changeset 9674:4cf195710ae8

simplify arrayfun
author Jaroslav Hajek <highegg@gmail.com>
date Wed, 30 Sep 2009 21:59:44 +0200
parents acf9952463c3
children ef45d191d833
files scripts/ChangeLog scripts/general/arrayfun.m
diffstat 2 files changed, 27 insertions(+), 30 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/ChangeLog	Wed Sep 30 11:10:54 2009 -0400
+++ b/scripts/ChangeLog	Wed Sep 30 21:59:44 2009 +0200
@@ -1,3 +1,7 @@
+2009-09-30  Jaroslav Hajek  <highegg@gmail.com>
+
+	* general/arrayfun.m: Simplify.
+
 2009-09-30  John W. Eaton  <jwe@octave.org>
 
 	* mkdoc: Work on list of file names instead of a directory.
--- a/scripts/general/arrayfun.m	Wed Sep 30 11:10:54 2009 -0400
+++ b/scripts/general/arrayfun.m	Wed Sep 30 21:59:44 2009 +0200
@@ -1,4 +1,5 @@
 ## Copyright (C) 2006, 2007, 2008, 2009 Bill Denney
+## Copyright (C) 2009 Jaroslav Hajek
 ##
 ## This file is part of Octave.
 ##
@@ -128,6 +129,7 @@
 ## @end deftypefn
 
 ## Author: Bill Denney <denney@seas.upenn.edu>
+## Rewritten: Jaroslav Hajek <highegg@gmail.com>
 
 function varargout = arrayfun (func, varargin)
 
@@ -135,40 +137,31 @@
     print_usage ();
   endif
 
-  ## Convert everything to cells and call cellfun (let cellfun error
-  ## check the options in case more options come available).
-  sizetomatch = size (varargin{1});
-  m2cargs{1} = ones (size (varargin{1}, 1), 1);
-  m2cargs{2} = ones (size (varargin{1}, 2), 1);
-  cfarg{1} = mat2cell (varargin{1}, m2cargs{:});
-  stillmatches = true;
-  idx = 1;
-  len = length (varargin);
-  while (stillmatches && idx < len)
-    idx++;
-    thissize = size (varargin{idx});
-    if (numel (thissize) == numel (sizetomatch)
-        && all (thissize == sizetomatch))
-      if (ischar (varargin{idx})
-          && (strcmpi (varargin{idx}, "UniformOutput")
-              || strcmpi (varargin{idx}, "ErrorHandler")))
-        ## Catch these strings just in case they happen to be the same
-        ## size as the other input.
-        stillmatches = false;
-      else
-        cfarg{idx} = mat2cell (varargin{idx}, m2cargs{:});
-      endif
+  nargs = length (varargin);
+
+  recognized_opts = {"UniformOutput", "ErrorHandler"};
+
+  while (nargs >= 2)
+    maybeopt = varargin{nargs-1};
+    if (ischar (maybeopt) && any (strcmpi (maybeopt, recognized_opts)))
+      nargs -= 2;
     else
-      stillmatches = false;
+      break;
     endif
   endwhile
 
-  varargout = cell (max ([nargout, 1]), 1);
-  if (idx >= len)
-    [varargout{:}] = cellfun (func, cfarg{:});
-  else
-    [varargout{:}] = cellfun (func, cfarg{:}, varargin{idx:len});
-  endif
+  args = varargin(1:nargs);
+  opts = varargin(nargs+1:end);
+
+  args = cellfun (@num2cell, args, "UniformOutput", false,
+  "ErrorHandler",  @arg_class_error);
+
+  [varargout{1:nargout}] = cellfun (func, args{:}, opts{:});
+
+endfunction
+
+function arg_class_error (S, X)
+  error ("arrayfun: invalid argument of class %s", class (X))
 endfunction
 
 %% Test function to check the "Errorhandler" option