Skip to content

Commit 628e6b7

Browse files
committed
FEAT/CHANGE: added support for find on map!
This feature was introduced in Red language so it is good to be compatible. Example: ``` >> find #("a" 1 "b" "c") "A" == "a" >> find #("a" 1 "b" "c") 1 == none >> find/case #("a" 1 "b" "c") "A" == none ```
1 parent 0ffc8f6 commit 628e6b7

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

src/boot/actions.r

+1-1
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ pick: action [
193193

194194
find: action [
195195
{Searches for a value; for series returns where found, else none.}
196-
series [series! gob! port! bitset! typeset! object! none!]
196+
series [series! gob! port! bitset! typeset! object! map! none!]
197197
value [any-type!]
198198
/part {Limits the search to a given length or position}
199199
length [number! series! pair!]

src/core/t-map.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -488,9 +488,10 @@
488488

489489
case A_PICK: // same as SELECT for MAP! datatype
490490
case A_SELECT:
491+
case A_FIND:
491492
n = Find_Entry(series, arg, 0, Find_Refines(ds, AM_SELECT_CASE) ? AM_FIND_CASE : 0);
492493
if (!n) return R_NONE;
493-
*D_RET = *VAL_BLK_SKIP(val, ((n-1)*2)+1);
494+
*D_RET = *VAL_BLK_SKIP(val, ((n-1)*2)+((action == A_FIND)?0:1));
494495
break;
495496

496497
case A_INSERT:

src/tests/units/map-test.r3

+29
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,35 @@ Rebol [
7979
===end-group===
8080

8181

82+
===start-group=== "find"
83+
84+
--test-- "map-find-1"
85+
mf1-m: #(a: none b: 1 c: 2)
86+
--assert 'a = find mf1-m 'a
87+
--assert 'b = find mf1-m 'b
88+
--assert 'c = find mf1-m 'c
89+
--assert none = find mf1-m 'd
90+
91+
--test-- "map-find-2"
92+
mf2-m: #(a: 1 b: 2 c: 3)
93+
mf2-m/a: 'none
94+
mf2-m/b: none
95+
--assert 'a = find mf2-m 'a
96+
--assert 'b = find mf2-m 'b
97+
--assert 'c = find mf2-m 'c
98+
99+
--test-- "map-find-3"
100+
mf3-m: #(aB: 1 ab: 2 AB: 3)
101+
--assert 'ab = find/case mf3-m 'ab
102+
--assert none = find/case mf3-m 'Ab
103+
104+
--test-- "map-find-4"
105+
mf4-m: make map! [b 1]
106+
--assert 'b = find mf4-m first [b:]
107+
--assert 'b = find mf4-m first [b:]
108+
===end-group===
109+
110+
82111
===start-group=== "map issues"
83112
;@@ https://github.com/Oldes/Rebol-issues/issues/770
84113
--test-- "map-issue-770"

0 commit comments

Comments
 (0)