Skip to content

Commit

Permalink
feat!: convert fallible List methods to return Option (#460)
Browse files Browse the repository at this point in the history
  • Loading branch information
phated authored and ospencer committed Dec 11, 2020
1 parent aa3767d commit a08768e
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 27 deletions.
25 changes: 15 additions & 10 deletions compiler/test/stdlib/list.test.gr
Original file line number Diff line number Diff line change
Expand Up @@ -83,18 +83,21 @@ assert reject(x => x == 3, list) == [1, 2]

// List.head

assert head(list) == 1
assert head([2]) == 2
assert head(list) == Some(1)
assert head([2]) == Some(2)
assert head([]) == None

// List.tail

assert tail(list) == [2, 3]
assert tail([1]) == []
assert tail(list) == Some([2, 3])
assert tail([1]) == Some([])
assert tail([]) == None

// List.nth

assert nth(0, list) == 1
assert nth(2, list) == 3
assert nth(0, list) == Some(1)
assert nth(2, list) == Some(3)
assert nth(10, list) == None

// List.flatten

Expand Down Expand Up @@ -152,12 +155,14 @@ assert dropWhile(x => x > 5, list) == list

// List.find

assert find(x => x == 2, list) == 2
assert find(x => x == 2, list) == Some(2)
assert find(x => false, list) == None

// List.findIndex

assert findIndex(x => x == 1, list) == 0
assert findIndex(x => x == 2, list) == 1
assert findIndex(x => x == 1, list) == Some(0)
assert findIndex(x => x == 2, list) == Some(1)
assert findIndex(x => false, list) == None

// List.product

Expand Down Expand Up @@ -194,4 +199,4 @@ let listSub = [1, 2, 3]

assert sub(0, 1, listSub) == [1]
assert sub(0, 2, listSub) == [1, 2]
assert sub(1, 2, listSub) == [2, 3]
assert sub(1, 2, listSub) == [2, 3]
6 changes: 3 additions & 3 deletions stdlib/array.gr
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ export let fillRange = (value, start, stop, array) => {
let length = length(array)
let startIndex = if (start < 0) length + start else start
let stopIndex = if (stop < 0) length + stop else stop

if (startIndex > length) {
ignore(fail 'The start index is outside the array')
}
Expand Down Expand Up @@ -255,9 +255,9 @@ export let count = (fn, array) => {
while (position < length) {
if(fn(array[position])) {
count = incr(count)
position = incr(position)
position = incr(position)
} else {
position = incr(position)
position = incr(position)
}
}
count
Expand Down
24 changes: 12 additions & 12 deletions stdlib/list.gr
Original file line number Diff line number Diff line change
Expand Up @@ -131,25 +131,25 @@ let rec reject = (fn, list) => {

let head = (list) => {
match (list) {
[] => fail 'head cannot get the first element of an empty List',
[first, ..._] => first
[] => None,
[first, ..._] => Some(first)
}
}

let tail = (list) => {
match (list) {
[] => fail 'tail cannot get any elements from an empty List',
[_, ...rest] => rest
[] => None,
[_, ...rest] => Some(rest)
}
}

let rec nth = (index, list) => {
if (index < 0) {
fail 'nth index cannot be a negative number'
None
} else {
match (list) {
[] => fail 'nth index is out-of-bounds',
[first, ...rest] => if (index == 0) first else nth(index - 1, rest)
[] => None,
[first, ...rest] => if (index == 0) Some(first) else nth(index - 1, rest)
}
}
}
Expand Down Expand Up @@ -246,22 +246,22 @@ let rec take = (n, list) => {
let rec takeWhile = (p, list) => {
match (list) {
[] => [],
[first, ...rest] => if (p(first)) [first, ...takeWhile(p, rest)] else []
[first, ...rest] => if (p(first)) [first, ...takeWhile(p, rest)] else []
}
}

let rec find = (fn, list) => {
match (list) {
[] => fail 'could not find a match',
[first, ...rest] => if (fn(first)) first else find(fn, rest)
[] => None,
[first, ...rest] => if (fn(first)) Some(first) else find(fn, rest)
}
}

let findIndex = (fn, list) => {
let rec findItemIndex = (l, index) => {
match (l) {
[] => fail 'could not find a match',
[first, ...rest] => if (fn(first)) index else findItemIndex(rest, index + 1)
[] => None,
[first, ...rest] => if (fn(first)) Some(index) else findItemIndex(rest, index + 1)
}
}
findItemIndex(list, 0)
Expand Down
4 changes: 2 additions & 2 deletions stdlib/queue.gr
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export let isEmpty = (queue) => {
export let head = (queue) => {
match (queue) {
{ forwards: [], backwards: [] } => None,
{ forwards, backwards } => Some(List.head(forwards))
{ forwards, backwards } => List.head(forwards)
}
}

Expand All @@ -34,4 +34,4 @@ export let dequeue = (queue) => {
{ forwards: [head], backwards } => { forwards: List.reverse(backwards), backwards: [] },
{ forwards: [head, ...ftail], backwards } => { forwards: ftail, backwards }
}
}
}

0 comments on commit a08768e

Please sign in to comment.