-
Notifications
You must be signed in to change notification settings - Fork 16
/
list.addRemove.fmfn
56 lines (47 loc) · 2.48 KB
/
list.addRemove.fmfn
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/*
* list.addRemove ( value; valuelist )
* RETURNS: ( string ) The supplied list with the value either removed or concatenated
*
*/
Let ( [ //------------------------- VARIABLES
var.list = Substitute ( If ( Left ( valuelist; 1 ) ≠ ¶; "^") & valuelist & "^"; ¶; "^^" ); // List minus all returns
var.match = "^" & value & "^"; // Value to match
var.exists = PatternCount ( var.list; var.match )
]; //------------------------- RESULT
Case (
var.exists;
Let ( [
var.removed = Substitute ( var.list; var.match; "" ); // Kill matching values
var.list = Substitute ( var.removed; "^^"; ¶ ); // Put returns back in
var.list = Middle ( var.list; 2; 100000 ); // Trim leading ^
var.list = Left ( var.list; Length ( var.list ) - 1 ); // Trim trailing ^
var.cleaned = Substitute ( var.list; ¶; "" ); // Remove returns for trimming
var.char.first = Position ( var.list; Left ( var.cleaned; 1 ); 0; 1 ); // Position of firt non cleaned char
var.char.last = Position ( var.list; Right ( var.cleaned; 1 ); Length ( var.list ); -1 ) // Position of last non cleaned char
];
Middle ( var.list; var.char.first; var.char.last - var.char.first + 1 )
);
If ( valuelist ≠ ""; valuelist & ¶ ) & value // default
)
)
/*
* Unit tests
*/
/*
Let (
_ = "--------------------";
List (
List ( "At beginning ( one )"; _; list.addRemove ( "one"; List ( "one"; "two"; "three" ) ); _ );
List ( "At ending ( three )"; _; list.addRemove ( "three"; List ( "one"; "two"; "three" ) ); _ );
List ( "With multiple spread out ( one )"; _; list.addRemove ( "one"; List ( "one"; "two"; "one"; "three" ) ); _ );
List ( "With multiple at beginning ( one )"; _; list.addRemove ( "one"; List ( "one"; "one"; "two"; "three" ) ); _ );
List ( "With multiple at ending ( three )"; _; list.addRemove ( "three"; List ( "one"; "two"; "three"; "three" ) ); _ );
List ( "With multiple in middle ( two )"; _; list.addRemove ( "two"; List ( "one"; "two"; "two"; "three" ) ); _ );
List ( "With even returns at start ( two )"; _; list.addRemove ( "two"; "¶¶¶¶one¶two¶three" ); _ );
List ( "With odd returns at start ( two )"; _; list.addRemove ( "two"; "¶¶¶one¶two¶three" ); _ );
List ( "With even returns at end ( two )"; _; list.addRemove ( "two"; "one¶two¶three¶¶¶¶" ); _ );
List ( "With odd returns at end ( two )"; _; list.addRemove ( "two"; "one¶two¶three¶¶¶" ); _ );
List ( "Without value ( one )"; _; list.addRemove ( "one"; List ( "two"; "three" ) ); _ );
)
)
*/