Mercurial > hg > ishara
changeset 9:45aaf9b2d7b0
Moved high-order sequence/list functions to @cell class.
author | samer |
---|---|
date | Mon, 14 Jan 2013 15:49:04 +0000 |
parents | f0a3d7d7a0e3 |
children | f7fb679637ff |
files | general/cellutils/@cell/foldl.m general/cellutils/@cell/foreach.m general/cellutils/@cell/map.m general/cellutils/@cell/scanl.m general/cellutils/@cell/select.m general/cellutils/@cell/zipwith.m general/funutils/feval2cell.m general/funutils/fold.m general/funutils/foldl.m general/funutils/foreach.m general/funutils/map.m general/funutils/scanl.m general/funutils/tbind.m general/funutils/zipwith.m |
diffstat | 14 files changed, 86 insertions(+), 86 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/general/cellutils/@cell/foldl.m Mon Jan 14 15:49:04 2013 +0000 @@ -0,0 +1,13 @@ +function X=foldl(fn,e,args) +% foldl - Fold fom the left combinator +% +% This function applies an associative operator to cell array, +% starting from the left using the given starting element. +% eg fold(@plus,0,{1,2,3,4}) = ((((0+1)+2)+3)+4). +% +% foldl :: +% (X,Y->X) ~'associative binary operator', +% X ~'initial element', +% {[N]->Y} ~'list (cell array) of arguments (at least 2 elements)' +% -> X. +X=e; for i=1:length(args), X=fn(X,args{i}); end
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/general/cellutils/@cell/foreach.m Mon Jan 14 15:49:04 2013 +0000 @@ -0,0 +1,8 @@ +function foreach(f,X,varargin) +% foreach - do an action for each element in a cell array in order +% +% foreach :: (A=>void), {[N]->A} => void. +% + +if nargin>2, error('Options for foreach no longer supported'); end +for i=1:numel(X), f(x{i}); end
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/general/cellutils/@cell/map.m Mon Jan 14 15:49:04 2013 +0000 @@ -0,0 +1,4 @@ +% map - Map a function over a cell array +% +% map :: (A->B, {[Size]->A}) -> {[Size]->B} +function Y=map(fn,X), Y=cellmap(fn,X); end
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/general/cellutils/@cell/scanl.m Mon Jan 14 15:49:04 2013 +0000 @@ -0,0 +1,24 @@ +% scanl - scanl for cell arrays +% +% scanl :: +% (S,X->S) ~'scannning function', +% S ~'initial value', +% {[N]->X} ~'data to scan, sequence of length L' +% -> {[N]->S}. + +function Y=scanl(f,y,X,varargin) + Y=cell(size(X)); + if nargin>3 + opts=prefs('draw',0,varargin{:}); + for i=1:size(X,2) + y1=f(y,X{i}); + Y{i}=y1; + if opts.draw, opts.plotfn(i,y,X{i},y1); end + optpause(opts); + y=y1; + end + else + for i=1:size(X,2), y=f(y,X{i}); Y{i}=y; end + end + +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/general/cellutils/@cell/select.m Mon Jan 14 15:49:04 2013 +0000 @@ -0,0 +1,7 @@ +% select - Filter cell array with boolean test +% +% select :: (A->bool), {[N]->A} -> {[M]->A}. +% select :: (A->bool), {[1,N]->A} -> {[1,M]->A}. +% +% eg, select(@iseven,{1,2,3,4}) = {2,4} +function Y=select(fn,X), Y=cellfilt(fn,X);
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/general/cellutils/@cell/zipwith.m Mon Jan 14 15:49:04 2013 +0000 @@ -0,0 +1,11 @@ +function Y=zipwith(fn,varargin) +% zipwith - Zip cell arrays with a function +% +% zipwith :: ( +% (D1,...,Dn)->R ~ function of n arguments, +% {[size]->D1}, ..., {[size]->Dn} ~ n cell arrays of appropriate types +% ) -> {[size]->R} ~ cell array of results + +Y=cellzip(fn,varargin{:}); + +
--- a/general/funutils/feval2cell.m Mon Jan 14 14:54:10 2013 +0000 +++ b/general/funutils/feval2cell.m Mon Jan 14 15:49:04 2013 +0000 @@ -1,12 +1,11 @@ -function Y=feval2cell(I,F,varargin) -% feval2cell - Evaluate function and return multiple values in cell array +function Y=returns(I,F,varargin) +% returns - Evaluate function and return multiple values in cell array % -% Usage: -% -% if F is a function that returns M >= max(I) values, then -% Y = feval2cell(I,F,...) -% evaluates [Z{1:max(I)}]=F(...) -% and returns Z(I). +% returns :: +% I:[[M]->[N]] ~'M numbers between 1 and N', +% (A(1),...,A(L) -> B(1), ..., B(N)) ~'function with L in and N outputs', +% A(1), ..., A(L) ~'arguments for function' +% -> cell { B(I(1)), ..., B(I(M)) } ~'selected returns in a cell array'. Z=cell(max(I),1); [Z{:}]=F(varargin{:});
--- a/general/funutils/fold.m Mon Jan 14 14:54:10 2013 +0000 +++ b/general/funutils/fold.m Mon Jan 14 15:49:04 2013 +0000 @@ -1,13 +1,13 @@ -function X=fold(fn,args) -% fold - Fold combinator from functional programming +function X=fold(fn,x) +% fold - polypmorphic Fold combinator +% +% fold :: (X,X->X), Type(A) -> X :- sequence_class(Type). % % This function applies an associative operator to a list of arguments, % as if the list was written out with the operator between consecutive % elements, eg fold(@plus,{1,2,3,4}) = 1+2+3+4. -% -% fold :: -% (X,X->X) ~'associative binary operator', -% {[N]->X} ~'list (cell array) of arguments (at least 2 elements)' -% -> X. +% +% Works for any sequence class that has head and next methods, +% including seq(A) and cells(A). -if isempty(args), X=[]; else X=foldl(fn,args{1},args(2:end)); end +if isempty(x), X=[]; else X=foldl(fn,head(x),next(x)); end
--- a/general/funutils/foldl.m Mon Jan 14 14:54:10 2013 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,13 +0,0 @@ -function X=foldl(fn,e,args) -% foldl - Fold fom the left combinator -% -% This function applies an associative operator to cell array, -% starting from the left using the given starting element. -% eg fold(@plus,0,{1,2,3,4}) = ((((0+1)+2)+3)+4). -% -% foldl :: -% (X,Y->X) ~'associative binary operator', -% X ~'initial element', -% {[N]->Y} ~'list (cell array) of arguments (at least 2 elements)' -% -> X. -X=e; for i=1:length(args), X=fn(X,args{i}); end
--- a/general/funutils/foreach.m Mon Jan 14 14:54:10 2013 +0000 +++ b/general/funutils/foreach.m Mon Jan 14 15:49:04 2013 +0000 @@ -1,15 +1,1 @@ -function foreach(f,X,varargin) -% foreach - do an action for each element in a cell array in order -% -% foreach :: (A->action), {[N]->A} -> action. -% foreach :: (A->action), {[N]->A}, options {} -> action. -% -% Takes the same options as iterate. - - iterate(@g,X,varargin{:}); - - function x=g(x) - f(x{1}); - x=x(2:end); - end -end +function foreach(f,x), if ~isempty(x), error('foreach not supported for this class'); end
--- a/general/funutils/map.m Mon Jan 14 14:54:10 2013 +0000 +++ b/general/funutils/map.m Mon Jan 14 15:49:04 2013 +0000 @@ -1,23 +1,11 @@ -function y=map(f,x,dim) +function y=map(f,x) % map - Map function over collection % % map :: (A->B), collection(A) -> collection(B). % -% collection(A) ::= seq A | {D->A} | [D->A]. -% -% This implementation handles cell arrays and ordinary arrays. -% Sequences should be taken care of by data/map +% collection(A) ::= seq(A) | cells(A). -if isempty(x) - y=x; -elseif iscell(x), - y=cellmap(f,x); -elseif isnumeric(x) - if nargin<3, y=reshape(f(x),size(x)); - elseif dim==1, y=maprows(f,x); - elseif dim==2, y=mapcols(f,x); - else error(sprintf('Cannot map over dimension %d',dim)); - end +if isempty(x), y=x; else error(sprintf('Cannot map over objects of class %s',class(x))); end
--- a/general/funutils/scanl.m Mon Jan 14 14:54:10 2013 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,24 +0,0 @@ -% scanl - scanl for cell arrays -% -% scanl :: -% (S,X->S) ~'scannning function', -% S ~'initial value', -% {[N]->X} ~'data to scan, sequence of length L' -% -> {[N]->S}. - -function Y=scanl(f,y,X,varargin) - Y=cell(size(X)); - if nargin>3 - opts=prefs('draw',0,varargin{:}); - for i=1:size(X,2) - y1=f(y,X{i}); - Y{i}=y1; - if opts.draw, opts.plotfn(i,y,X{i},y1); end - optpause(opts); - y=y1; - end - else - for i=1:size(X,2), y=f(y,X{i}); Y{i}=y; end - end - -