Skip to content

Commit

Permalink
Migrate Vector ops to unsafe_at
Browse files Browse the repository at this point in the history
  • Loading branch information
radeusgd committed Jan 21, 2022
1 parent 287a248 commit 350e9b3
Showing 1 changed file with 18 additions and 18 deletions.
36 changes: 18 additions & 18 deletions distribution/lib/Standard/Base/0.2.32-SNAPSHOT/src/Data/Vector.enso
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ type Vector
exists predicate =
len = this.length
go idx found = if found || (idx >= len) then found else
@Tail_Call go idx+1 (predicate (this.at idx))
@Tail_Call go idx+1 (predicate (this.unsafe_at idx))
go 0 False

## Returns the first element of the vector that satisfies the predicate or
Expand All @@ -248,8 +248,8 @@ type Vector
len = this.length
go idx =
if (idx >= len) then Error.throw Nothing else
elem = this.at idx
if (predicate elem) then elem else
elem = this.unsafe_at idx
if predicate elem then elem else
@Tail_Call go idx+1
go 0

Expand Down Expand Up @@ -342,7 +342,7 @@ type Vector
[1, 2, 3] . map +1
map : (Any -> Any) -> Vector Any
map function =
here.new this.length i-> function (this.at i)
here.new this.length i-> function (this.unsafe_at i)

## Applies a function to each element of the vector, returning the vector
that contains all results concatenated.
Expand Down Expand Up @@ -380,7 +380,7 @@ type Vector

[1, 2, 3].map_with_index (+)
map_with_index : (Integer -> Any -> Any) -> Vector Any
map_with_index function = here.new this.length i-> function i (this.at i)
map_with_index function = here.new this.length i-> function i (this.unsafe_at i)

## Applies a function to each element of the vector.

Expand All @@ -407,7 +407,7 @@ type Vector

[1, 2].reverse
reverse : Vector Any
reverse = here.new this.length (i -> this.at (this.length - (1 + i)))
reverse = here.new this.length (i -> this.unsafe_at (this.length - (1 + i)))

## Generates a human-readable text representation of the vector.

Expand All @@ -425,9 +425,9 @@ type Vector
generic_to_text : Text -> Text -> Text -> Text
generic_to_text prefix separator suffix =
if this.length == 0 then prefix+suffix else
folder = str -> ix -> str + separator + (this.at ix).to_text
folder = str -> ix -> str + separator + (this.unsafe_at ix).to_text
tail_elems = 1.up_to this.length . fold "" folder
prefix + (this.at 0 . to_text) + tail_elems + suffix
prefix + (this.unsafe_at 0 . to_text) + tail_elems + suffix

## UNSTABLE
Generates a human-readable text representation of the vector, keeping its
Expand Down Expand Up @@ -469,7 +469,7 @@ type Vector
[1, 2, 3] == [2, 3, 4]
== : Vector -> Boolean
== that =
eq_at i = this.at i == that.at i
eq_at i = this.unsafe_at i == that.unsafe_at i
if this.length == that.length then 0.up_to this.length . all eq_at else False

## Concatenates two vectors, resulting in a new vector, containing all the
Expand All @@ -487,9 +487,9 @@ type Vector
this_len = this.length
arr = Array.new (this_len + that.length)
0.up_to this_len . each i->
arr.set_at i (this.at i)
arr.set_at i (this.unsafe_at i)
this.length.up_to arr.length . each i->
arr.set_at i (that.at i-this_len)
arr.set_at i (that.unsafe_at i-this_len)
Vector arr

## Add `element` to the beginning of `this` vector.
Expand Down Expand Up @@ -529,8 +529,8 @@ type Vector
join : String -> Text
join separator =
if this.length == 0 then "" else
if this.length == 1 then this.at 0 else
this.at 0 + (1.up_to this.length . fold "" acc-> i-> acc + separator + this.at i)
if this.length == 1 then this.unsafe_at 0 else
this.unsafe_at 0 + (1.up_to this.length . fold "" acc-> i-> acc + separator + this.unsafe_at i)

## Creates a new vector with the first `count` elements in `this` removed.

Expand All @@ -543,7 +543,7 @@ type Vector
[1, 2, 3, 4, 5].drop_start 1
drop_start : Integer -> Vector Any
drop_start count = if count >= this.length then here.new 0 (x -> x) else
here.new (this.length - count) (i -> this.at i+count)
here.new (this.length - count) (i -> this.unsafe_at i+count)

## Creates a new vector with the last `count` elements in `this` removed.

Expand Down Expand Up @@ -610,7 +610,7 @@ type Vector
zip : Vector Any -> (Any -> Any -> Any) -> Vector Any
zip that function=[_,_] =
len = Math.min this.length that.length
here.new len i-> function (this.at i) (that.at i)
here.new len i-> function (this.unsafe_at i) (that.unsafe_at i)

## Extend `this` vector to the length of `n` appending elements `elem` to
the end.
Expand Down Expand Up @@ -653,7 +653,7 @@ type Vector

[1, 2, 3, 4].head
head : Any ! Empty_Error
head = if this.length >= 1 then this.at 0 else Error.throw Empty_Error
head = if this.length >= 1 then this.unsafe_at 0 else Error.throw Empty_Error

## Get all elements in the vector except the first.

Expand Down Expand Up @@ -682,7 +682,7 @@ type Vector

[1, 2, 3, 4].last
last : Vector ! Empty_Error
last = if this.length >= 1 then (this.take_end 1).at 0 else
last = if this.length >= 1 then this.unsafe_at (this.length-1) else
Error.throw Empty_Error

## Get the first element from the vector, or an `Empty_Error` if the vector
Expand All @@ -705,7 +705,7 @@ type Vector

[1, 2, 3, 4].second
second : Vector ! Singleton_Error
second = if this.length >= 2 then this.at 1 else
second = if this.length >= 2 then this.unsafe_at 1 else
Error.throw (Singleton_Error this)

## Get all elements in the vector except the first.
Expand Down

0 comments on commit 350e9b3

Please sign in to comment.