Skip to content

Commit

Permalink
cr
Browse files Browse the repository at this point in the history
  • Loading branch information
radeusgd committed Aug 4, 2022
1 parent 8ea629c commit 46695cf
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 43 deletions.
60 changes: 18 additions & 42 deletions distribution/lib/Standard/Base/0.0.0-dev/src/Data/Vector.enso
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from Standard.Base import all
import Standard.Base.Runtime.Ref
import Standard.Base.Runtime.Unsafe
from Standard.Base.Data.Index_Sub_Range import First, Last, While, By_Index, Sample, Every
from Standard.Base.Data.Index_Sub_Range import While, By_Index, Sample, Every
import Standard.Base.Random

## Creates a new vector of the given length, initializing elements using
Expand Down Expand Up @@ -29,10 +29,6 @@ new length constructor =
0.up_to length . each ix-> arr.set_at ix (constructor ix)
Vector arr

## Creates an empty vector.
empty : Vector Any
empty = Vector Array.empty

## Creates a new vector of the given length, filling the elements with
the provided constant.

Expand Down Expand Up @@ -660,7 +656,7 @@ type Vector
slice self start end =
slice_start = Math.max 0 start
slice_end = Math.min self.length end
if slice_start >= slice_end then empty else
if slice_start >= slice_end then [] else
if (slice_start == 0) && (slice_end == self.length) then self else
len = slice_end - slice_start
arr = Array.new len
Expand All @@ -678,21 +674,11 @@ type Vector
take : (Index_Sub_Range | Range) -> Vector Any
take self range=(First 1) = case range of
Range _ _ _ -> self.take (By_Index range)
First count ->
if count <= 0 then empty else
if count >= self.length then self else
self.slice 0 (Math.min self.length count)
Last count ->
if count <= 0 then empty else
if count >= self.length then self else
self.slice self.length-count self.length
First count -> self.slice 0 (Math.min self.length count)
Last count -> self.slice self.length-count self.length
While predicate ->
go ix = if ix >= self.length then ix else
case predicate (self.unsafe_at ix) of
False -> ix
True -> @Tail_Call go (ix+1)
end = go 0
self.slice 0 end
end = 0.up_to self.length . find i-> (predicate (self.at i)).not
if end.is_nothing then self else self.slice 0 end
By_Index one_or_many_descriptors -> Panic.recover [Index_Out_Of_Bounds_Error, Illegal_Argument_Error] <|
indices = case one_or_many_descriptors of
Vector _ -> one_or_many_descriptors
Expand All @@ -704,7 +690,7 @@ type Vector
Random.sample self count rng
Every step start ->
if step <= 0 then Error.throw (Illegal_Argument_Error "Step within Every must be positive.") else
if start >= self.length then empty else
if start >= self.length then [] else
range = Range start self.length step
self.take (By_Index range)

Expand All @@ -719,21 +705,11 @@ type Vector
drop : (Index_Sub_Range | Range) -> Vector Any
drop self range=(First 1) = case range of
Range _ _ _ -> self.drop (By_Index range)
First count ->
if count <= 0 then self else
if count >= self.length then empty else
self.slice count self.length
Last count ->
if count <= 0 then self else
if count >= self.length then empty else
self.slice 0 self.length-count
First count -> self.slice count self.length
Last count -> self.slice 0 self.length-count
While predicate ->
go ix = if ix >= self.length then ix else
case predicate (self.unsafe_at ix) of
False -> ix
True -> @Tail_Call go (ix+1)
end = go 0
self.slice end self.length
end = 0.up_to self.length . find i-> (predicate (self.at i)).not
if end.is_nothing then [] else self.slice end self.length
By_Index one_or_many_descriptors -> Panic.recover [Index_Out_Of_Bounds_Error, Illegal_Argument_Error] <|
indices = case one_or_many_descriptors of
Vector _ -> one_or_many_descriptors
Expand Down Expand Up @@ -1218,13 +1194,13 @@ resolve_ranges ranges length =
Assumes that the ranges have been already bounds-checked (for example by
passing them through `resolve_ranges`).
slice_ranges vector ranges =
if ranges.length == 0 then empty else case ranges.length == 1 of
True -> case ranges.first of
Integer -> Vector (Array.new_1 (vector.unsafe_at ranges.first))
Range start end step -> case step == 1 of
True -> vector.slice start end
False -> slice_many_ranges vector ranges
False -> slice_many_ranges vector ranges
if ranges.length == 0 then [] else
if ranges.length != 1 then slice_many_ranges vector ranges else
case ranges.first of
Integer -> [vector.unsafe_at ranges.first]
Range start end step -> case step == 1 of
True -> vector.slice start end
False -> slice_many_ranges vector ranges

## PRIVATE
See `slice_ranges`.
Expand Down
2 changes: 1 addition & 1 deletion test/Tests/src/Data/Vector_Spec.enso
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from Standard.Base import all
from Standard.Base.Data.Index_Sub_Range import First, Last, While, By_Index, Sample, Every
from Standard.Base.Data.Index_Sub_Range import While, By_Index, Sample, Every

import Standard.Test

Expand Down

0 comments on commit 46695cf

Please sign in to comment.