Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

list.findIndex() #448

Merged
merged 1 commit into from
Oct 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions docs/docs/collections/lists.md
Original file line number Diff line number Diff line change
Expand Up @@ -285,14 +285,30 @@ By default the initial value for `.reduce()` is 0, however we can change this to
print(["Dictu ", "is", " great!"].reduce(def (accumulate, element) => accumulate + element, "")); // 'Dictu is great!'
```

### list.find(func)
### list.find(func, number: start -> optional, number: end -> optional)

To find a single item within a list we use `.find()`. Find will search through each item in the list and as soon as the
callback returns a truthy value, the item that satisfied the callback is returned, if none of the items satisfy the callback
function then `nil` is returned.
function then `nil` is returned. The optional start and end parameters change the points at which the list will be searched.

Note: The first item to satisfy the callback is returned.

```cs
print([1, 2, 3].find(def (item) => item == 2)); // 2
print([1, 2, 3, 4, 5, 6].find(def (item) => item % 2 == 0, 2)); // 4
print([1, 2, 3, 4, 5, 6].find(def (item) => item % 2 == 0, 2, 3)); // nil
```

### list.findIndex(func, number: start -> optional, number: end -> optional)

To find a single item within a list we use `.findIndex()`. Find will search through each item in the list and as soon as the
callback returns a truthy value, the index at which the item that satisfied the callback is returned, if none of the items satisfy the callback
function then `nil` is returned. The optional start and end parameters change the points at which the list will be searched.

Note: The first item to satisfy the callback is returned.

```cs
print([1, 2, 3].findIndex(def (item) => item == 2)); // 1
print([1, 2, 3, 4, 5, 6].findIndex(def (item) => item % 2 == 0, 2)); // 3
print([1, 2, 3, 4, 5, 6].findIndex(def (item) => item % 2 == 0, 2, 3)); // nil
```
12 changes: 10 additions & 2 deletions src/vm/datatypes/lists/list-source.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,19 @@
" }\n" \
"}\n" \
"\n" \
"def find(list, func) {\n" \
" for (var i = 0; i < list.len(); i += 1) {\n" \
"def find(list, func, start=0, end=list.len()) {\n" \
" for (var i = start; i < end; i += 1) {\n" \
" if (func(list[i])) {\n" \
" return list[i];\n" \
" }\n" \
" }\n" \
"}\n" \
"\n" \
"def findIndex(list, func, start=0, end=list.len()) {\n" \
" for (var i = start; i < end; i += 1) {\n" \
" if (func(list[i])) {\n" \
" return i;\n" \
" }\n" \
" }\n" \
"}\n" \

12 changes: 10 additions & 2 deletions src/vm/datatypes/lists/list.du
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,18 @@ def forEach(list, func) {
}
}

def find(list, func) {
for (var i = 0; i < list.len(); i += 1) {
def find(list, func, start=0, end=list.len()) {
for (var i = start; i < end; i += 1) {
if (func(list[i])) {
return list[i];
}
}
}

def findIndex(list, func, start=0, end=list.len()) {
for (var i = start; i < end; i += 1) {
if (func(list[i])) {
return i;
}
}
}
6 changes: 5 additions & 1 deletion tests/lists/find.du
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@
const myList = [1, 2, 3, 4, 5];

assert(myList.find(def (item) => item == 3) == 3);
assert(myList.find(def (item) => item == 10) == nil);
assert(myList.find(def (item) => item == 10) == nil);
assert(myList.find(def (item) => item % 2 == 0) == 2);
assert(myList.find(def (item) => item % 2 == 0, 2) == 4);
assert(myList.find(def (item) => item % 2 == 0, 2, 3) == nil);
assert(myList.find(def (item) => item % 2 == 0, 5) == nil);
17 changes: 17 additions & 0 deletions tests/lists/findIndex.du
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* find.du
*
* Testing the list.findIndex() method
*
* .findIndex() runs a user defined function on each element in the list and returns the index of the item in the
* list that satisfies the callback
*/

const myList = [1, 2, 3, 4, 5];

assert(myList.findIndex(def (item) => item == 3) == 2);
assert(myList.findIndex(def (item) => item == 10) == nil);
assert(myList.findIndex(def (item) => item % 2 == 0) == 1);
assert(myList.findIndex(def (item) => item % 2 == 0, 2) == 3);
assert(myList.findIndex(def (item) => item % 2 == 0, 2, 3) == nil);
assert(myList.findIndex(def (item) => item % 2 == 0, 5) == nil);
1 change: 1 addition & 0 deletions tests/lists/import.du
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ import "filter.du";
import "reduce.du";
import "forEach.du";
import "find.du";
import "findIndex.du";
import "reverse.du";