@@ -4095,6 +4095,64 @@ version(mir_test)
40954095 assert (equal! equal(rc, [ [3 , 100 ], [], [400 , 102 ] ]));
40964096}
40974097
4098+ // / Filter out NaNs
4099+ version (mir_test)
4100+ @safe pure nothrow
4101+ unittest {
4102+ import mir.algorithm.iteration: equal, filter;
4103+ import mir.ndslice.slice: sliced;
4104+ import std.math.traits : isNaN;
4105+
4106+ static immutable result1 = [1.0 , 2 ];
4107+
4108+ double x;
4109+ auto y = [1.0 , 2 , x].sliced;
4110+ auto z = y.filter! (a => ! isNaN(a));
4111+ assert (z.equal(result1));
4112+ }
4113+
4114+ // / Filter out NaNs by row and by column
4115+ version (mir_test)
4116+ @safe pure
4117+ unittest {
4118+ import mir.algorithm.iteration: equal, filter;
4119+ import mir.ndslice.fuse: fuse;
4120+ import mir.ndslice.topology: byDim, map;
4121+ import std.math.traits : isNaN;
4122+
4123+ static immutable result1 = [[1.0 , 2 ], [3.0 , 4 , 5 ]];
4124+ static immutable result2 = [[1.0 , 3 ], [2.0 , 4 ], [5.0 ]];
4125+
4126+ double x;
4127+ auto y = [[1.0 , 2 , x], [3.0 , 4 , 5 ]].fuse;
4128+
4129+ auto z1 = y.byDim! 0. map! (filter! (a => ! isNaN(a)));
4130+ assert (z1.equal! equal(result1));
4131+ auto z2 = y.byDim! 1. map! (filter! (a => ! isNaN(a)));
4132+ assert (z2.equal! equal(result2));
4133+ }
4134+
4135+ // / Filter entire rows/columns that have NaNs
4136+ version (mir_test)
4137+ @safe pure
4138+ unittest {
4139+ import mir.algorithm.iteration: equal, filter;
4140+ import mir.ndslice.fuse: fuse;
4141+ import mir.ndslice.topology: byDim, map;
4142+ import std.math.traits : isNaN;
4143+
4144+ static immutable result1 = [[3.0 , 4 , 5 ]];
4145+ static immutable result2 = [[1.0 , 3 ], [2.0 , 4 ]];
4146+
4147+ double x;
4148+ auto y = [[1.0 , 2 , x], [3.0 , 4 , 5 ]].fuse;
4149+
4150+ auto z1 = y.byDim! 0. filter! (a => a.all! (b => ! isNaN(b)));
4151+ assert (z1.equal! equal(result1));
4152+ auto z2 = y.byDim! 1. filter! (a => a.all! (b => ! isNaN(b)));
4153+ assert (z2.equal! equal(result2));
4154+ }
4155+
40984156/+ +
40994157Implements the higher order filter and map function. The predicate and map functions are passed to
41004158`mir.functional.naryFun`, and can either accept a string, or any callable
0 commit comments