在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):adambard/functools-for-matlab开源软件地址(OpenSource Url):https://github.com/adambard/functools-for-matlab开源编程语言(OpenSource Language):Objective-C 86.0%开源软件介绍(OpenSource Introduction):functools package for MATLABDid you know that MATLAB has built-in support for closures? Develop for MATLAB in a functional style with functools. HUGE DISCLAIMERMATLAB is incredibly slow at applying anonymous functions. Don't use this library for performance, use it for convenience. I tend to use it for string manipulations mostly. Tools IncludedSpecifically, the following tools are implemented: Basic functional programming operations:
Collection operations
Utilities
All the following examples assume you've done
ExamplesMost of these examples are taken from the tests. Sorry if they're a bit contrived. Compose nth_even = compose(@(x) x - 1, @(x) x * 2)
nth_even(1) % => 0
nth_even(2) % => 2
nth_even(3) % => 4 Apply adder = @(x, y) x + y;
tot = partial(@reduce, adder);
sumargs = @(varargin) sum(varargin);
sumargs(1, 2, 3, 4) % => 10
apply(sumargs, [1, 2, 3, 4]) % => 10 Partial & rpartialThese two are probably the most capable of making MATLAB code easier to read and less painful to write. % partial
a = 1;
b = ones(2, 1) / 2;
movingavg2 = functools.partial(@filter, b, a);
movingavg2(1:4) % => [0.5, 1.5, 2.5, 3.5] % rpartial
isodate = rpartial(@datestr, 'yyyy-mm-dd HH:MM:SS');
isodate(datenum(2010, 1, 1)) % => '2010-01-01 00:00:00' Rpartial is notable for having a particularly useful function in MATLAB: pre-applying those key/value parameters that almost every base matlab function has: % Annoying
cellfun(@(x) x + 2, {-1, 0, 1, 2}, 'UniformOutput', false) % => {1, 2, 3, 4} % Awesome
map_ = rpartial(@cellfun, 'UniformOutput', false)
map_(@(x) x + 2, {-1, 0, 1, 2}) % => {1, 2, 3, 4} Map % Unfortunately, function application in MATLAB is even slower than its slow for loops.
% So, you probably shouldn't use this for math, although you can.
map(@(x) x * 2, {1, 2, 3}) % => {2, 4, 6} % Better to use this as a utility function
pwd % => /some/directory
filepaths = map(partial(@fullfile, pwd), {'1.txt', '2.txt', '3.txt', '4.txt'});
filepaths{2} % => '/some/directory/2.txt' Reduce reduce(@(x, y) x + y, [1 2 3 4]) % => 10
reduce(@(x, y) x + y, [1 2 3 4], 10) % => 20 % Something cleverer
join = @(sep, args) ...
reduce(@(x, y) [x sep y], ...
map(@num2str, args));
join(', ', [1 2 3 4]) % => '1, 2, 3, 4')
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论