Hey all! 

In anticipation of v2.0, I'd like to propose a *big honking API change*, 
namely, a protocol that has been discussed 
<https://groups.google.com/forum/#!searchin/elixir-lang-core/indexable%7Csort:relevance/elixir-lang-core/F1vV3mSG-xw/wD79gywVAwAJ>
 
here for years 
<https://groups.google.com/forum/#!searchin/elixir-lang-core/index%7Csort:relevance/elixir-lang-core/lEMa3VK6eVE/D3yj8w_SbFAJ>
 
but never implemented: Sequential.

Context

We have several modules dealing with 'collections', each capturing a unique 
property of being a collection:

Types:

- Tuple <https://hexdocs.pm/elixir/Tuple.html#content>: a collection of 
values stored contiguously in memory
- List <https://hexdocs.pm/elixir/List.html#content>: a collection of 
values stored in a linked list
- Binary: a collection of bytes
- String <https://hexdocs.pm/elixir/String.html#content>: a collection of 
UTF-8-encoded unicode characters stored in a binary
- Range <https://hexdocs.pm/elixir/Range.html#content>: a collection of 
integer values explicitly denoted by a discrete start and end integers

Utilities:

- Enum <https://hexdocs.pm/elixir/Enum.html#content>: stratagems for 
exhaustively traversing a collection
- Stream <https://hexdocs.pm/elixir/Stream.html#content>: stratagems for 
lazily traversing a collection

Protocols:

- Enumerable <https://hexdocs.pm/elixir/Enumerable.html#content>: logic for 
traversing a collection
- Collectable <https://hexdocs.pm/elixir/Collectable.html#content>: logic 
for mapping a collection of one type to another

So what would the Sequential protocol bring to the table? Well, it would 
specify the contract for *countable* collections, be they infinite or not. 
Much like Enum compliments Enumerable, we would implement a Sequence module 
to interact with those collections that can be accessed via an index, 
stealing several functions from existing modules.

Protocol

The Sequential protocol would act like the Enumerable protocol, with two 
callbacks:

- seek(Sequence.t, index): Scans through the sequence until it reaches the 
non_neg_integer value at the index position, and returns it.
- terminable?(Sequence.t): Returns whether or not the provided sequence can 
be exhaustively iterated.

Any collection function that hinges on *indexabliity* would be unified and 
moved into the Sequence module. Any collection function that hinges on 
*exhaustive 
countability* would be, too, so we could check if the Sequential was 
terminable?, raising a Sequence.InterminableError if it wasn't, and 
removing the caveat from the Enum module:

Since the majority of the functions in Enum 
> <https://hexdocs.pm/elixir/Enum.html#content> enumerate the whole 
> enumerable and return a list as result, infinite streams need to be 
> carefully used with such functions, as they can potentially run forever.


Module

The Sequence module would utilize Sequential.seek/2 and Sequential
.terminable?/1 and take on functions from the Enum, Stream, Tuple, List, 
and String modules, by encapsulating utilities for constructing and 
manipulating countable collections:

Manipulation:

- Sequence.at(Sequence.t, index): uses seek to find the value at the index, 
raises if the index is negative and the sequence isn't terminable.

- Sequence.first(Sequence.t): seeks the first value of the sequence.
- Sequence.last(Sequence.t): seeks last value of the sequence, raises if 
the sequence isn't terminable.
- Sequence.random(Sequence.t): seeks a random value of the sequence, raises 
if the sequence isn't terminable.
- Sequence.to_list(Sequence.t): converts the sequence to a list, raises if 
the sequence isn't terminable.
- Sequence.replace_at(Sequence.t, index, value): replaces the value at the 
index with the new value.

-- 
You received this message because you are subscribed to the Google Groups 
"elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/elixir-lang-core/fb8bbe97-7c04-46df-96ac-ece6da4717b3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to