Skip to content

Commit

Permalink
Helper method to turn iterator to a Vector (#8035)
Browse files Browse the repository at this point in the history
  • Loading branch information
JaroslavTulach authored Oct 12, 2023
1 parent 0dcfc3e commit 31624b5
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
25 changes: 25 additions & 0 deletions distribution/lib/Standard/Base/0.0.0-dev/src/Data/Vector.enso
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,31 @@ type Vector a
new : Integer -> (Integer -> Any) -> Vector Any
new length constructor = Array_Like_Helpers.vector_from_function length constructor

## PRIVATE
ADVANCED
Collects elements of a sequence into a new vector. Various structures can be
converted into vector of elements. This helper methods allows to do so in an
iterative fashion. Enough to describe how to extract value from current item
and how to advance to next item.

Arguments:
- seq: the sequence to process.
- element: A function taking the `seq` and follow ups and extracting the value to put into the vector
- next: A function to advance to next _item_ of the sequence
- limit: maximal number of elements to collect. Defaults to infinity.
- stop_at: optional function like `(_==List.Nil)` to check for _end of sequence condition_.
By default checks for `Nothing` being the terminal element of a collection.

> Example
Turn a list into a vector.
Vector.collect (List.Cons 1 <| List.Cons 2 <| List.Nil) .x .xs stop_at=(_==List.Nil)
collect seq element:(Any -> Any) next:(Any->Any) limit:(Integer|Nothing)=Nothing stop_at:(Any->Boolean)=(_==Nothing) =
b = Vector.new_builder (if limit.is_nothing then 10 else limit)
iterate item remaining = if remaining == 0 || (stop_at item) then b.to_vector else
b.append <| element item
@Tail_Call iterate (next item) (if remaining.is_nothing then Nothing else remaining-1)
iterate seq limit

## PRIVATE
ADVANCED

Expand Down
28 changes: 28 additions & 0 deletions test/Tests/src/Data/Vector_Spec.enso
Original file line number Diff line number Diff line change
Expand Up @@ -813,6 +813,20 @@ spec =
e.length . should_equal 1
b.at 0 . should_equal 32

Test.specify "Vector.collect lazy" <|
seq = Fib.sequence
seq.take 5 . should_equal [1, 1, 2, 3, 5]

Test.specify "Vector.collect empty list" <|
l = List.Nil
v = Vector.collect l .x .xs limit=30 stop_at=(_==List.Nil)
v . should_equal []

Test.specify "Vector.collect finite" <|
l = List.Cons 1 <| List.Cons 2 <| List.Cons 3 <| List.Nil
v = Vector.collect l .x .xs limit=30 stop_at=(_==List.Nil)
v . should_equal [1, 2, 3]

Test.group "Vector/Array equality" <|
v1 = [1, 2, 3]
a1 = v1.to_array
Expand Down Expand Up @@ -865,4 +879,18 @@ spec =
sliced_array = sliced_vector.to_array
sliced_array



type Fib
Number n:Integer ~next:Fib

take self limit:Integer = Vector.collect self .n .next limit

sequence =
sum_two seq = Fib.Number seq.n+seq.next.n (sum_two seq.next)
start = Fib.Number 1 <| Fib.Number 1 (sum_two start)
start



main = Test_Suite.run_main spec

0 comments on commit 31624b5

Please sign in to comment.