view general/map_fields.m @ 44:3cedfd4549ef

Code added since initial check in.
author samer
date Tue, 13 Jan 2015 14:03:17 +0000
parents
children
line wrap: on
line source
% map_fields - Apply function to all member fields of structure
%
% map_fields :: 
%    (A->B)              ~'function from As to Bs',
%    struct(fields(N,A)) 
% -> struct(fields(N,B)).
%
% fields(N,A) denotes a list of struct field type declarates with
% names from list N and types all equal to A, eg
%    fields({'a','b','c'},real) == {a::real, b::real, c::real}.

function y=map_fields(fn,x)
	y=foldl(@(s,f)setfield(s,f,fn(s.(f))),x,fields(x));