diff 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 diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/general/map_fields.m	Tue Jan 13 14:03:17 2015 +0000
@@ -0,0 +1,13 @@
+% 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));