goby
goby copied to clipboard
Implement Enumerable Module with Enumerator Class
Enumerable
module in Ruby originally was included in Array class and it has pre-defined methods such as the #map
, #select
and #reduce
. In original Ruby, it requires also to define the #each
method when included Enumerable module.
Hint: * means if no block given, then returns the Enumerator itself
Method list implementation:
- [ ]
Enumerable#all?
Returnstrue
if the block never returnsfalse
ornil
- [ ]
Enumerable#any?
Returnstrue
if the block ever returns a value other thanfalse
ornil
- [ ]
*Enumerable#chunk
Enumerates over items and chunking them together based on the return value of block - [ ]
Enumerable#chunk_while
Creates a enumerator for each chunked elements - [ ]
*Enumerable#map
and*Enumerable#collect
- [ ]
*Enumerable#flat_map
and*Enumerable#collect_concat
- [ ]
Enumerable#count
- Returns the number of items in Enumerator
- If argument is given, count the number of argument value in enumerator
- If block is given, counts the number of elements yielding
true
value
- [ ]
*Enumerable#cycle
Default runs forever, if specified numbern
then runs enumeratorn
times repeated - [ ]
*Enumerable#detect
Enumerator#find
- [ ]
Enumerable#drop
Drops firstn
elements from enumerator and return rest elements in an array - [ ]
*Enumerable#drop_while
Drops elements up to, but not including, the first element for which the block returnsnil
orfalse
and returns an array containing the remaining elements - [ ]
*Enumerable#each_cons
andEnumerable#each_slice
Iterates the given block for each array of consecutiven
elements - [ ]
*Enumerable#each_entry
Converting multiple values from yield to an array - [ ]
*Enumerable#each_with_index
- [ ]
*Enumerable#each_with_object
- [ ]
Enumerable#entries
andEnumerable#to_a
Returns array containing items in enumerator - [ ]
Enumerable#empty?
Returnstrue
if no elements - [ ]
*Enumerable#find_all
and*Enumerable#select
- [ ]
*Enumerable#find_index
- Returns the index for the first for which the evaluated value is not
false
- If pass in argument, return the index of the value which is equal to the argument
- Returns the index for the first for which the evaluated value is not
- [ ]
Enumerable#first
Returns firstn
element (defaultn
is 1) - [ ]
Enumerable#grep
- Range input as an argument will enumerates self and returns map of values which is included in range
- RegExp input as an argument will returns map of values which matches the regex
- [ ]
Enumerable#grep_v
Inverted version of#grep
- [ ]
*Enumerable#group_by
Groups collection by the result of the block which returns a hash - [ ]
Enumerable#include?
andEnumerable#member?
- [ ]
Enumerable#inject
andEnumerable#reduce
- [ ]
Enumerable#lazy
(Lazy Enumerator need more survey) - [ ]
Enumerable#max
Returns the object of enum withn
maximun value (defaultn
is 1) - [ ]
*Enumerable#max_by
- [ ]
Enumerable#min
Inversed version of#max
- [ ]
*Enumerable#min_by
Inversed version of#max_by
- [ ]
Enumerable#minmax
(???) - [ ]
*Enumerable#minmax_by
(???) - [ ]
Enumerable#none?
Returnstrue
if none of the block value resultstrue
([nil]
is none but not empty!) - [ ]
Enumerable#one?
Returnstrue
if block result returnstrue
exactly once - [ ]
Enumerable#partition
Returns two array elements in an array which the first array stores the value that the block evaluatestrue
while the second array stores value that the block evaluatesfalse
- [ ]
*Enumerable#reject
Returns array of all elements for which block resultfalse
- [ ]
*Enumerable#reverse_each
- [ ]
Enumerable#slice_after
- [ ]
Enumerable#slice_before
- [ ]
Enumerable#slice_when
- [ ]
Enumerable#sort
- [ ]
*Enumerable#sort_by
- [ ]
Enumerable#take
Returns firstn
elements (different from#first
,#take
needs to specifiy argument as an integer) - [ ]
*Enumerable#take_while
- [ ]
Enumerable#to_a
- [ ]
Enumerable#to_h
- [ ]
Enumerable#unique
Returns a new array by removing duplicated value inself
- [ ]
Enumerable#zip
@Maxwell-Alexius Are you still interested in working on this?
I'm not sure I can handle this ton of works and I haven't been using Ruby for a long time since working on JS related things, it might take me time to pick up and inspect the Enumerable
mechanism in Ruby again.
I would prefer others can pick up this issue and build the main Enumerator class.
If the main class is built, I think I can assist in implementing the Enumerable#method
part
Seeing Maxwell's recent comment, I've been surveying around Enumerator in Ruby and Goby, and I built a very primitive Enumerator class as a mock on my local rep that can be compiled so far. I think it is possible to build.
But now I'm wondering how to proceed farther. In other words, I think we need a grand design around organizing Enumerator and Enumerable (and perhaps Lazy and Block) classes in Goby.
- I wonder if it is certainly good for us to inherit the architecture or design of Ruby's Enumerator and Enumerable. Sharing methods for iteration via including Enumerable module and implementing
each
is good for consistency, but I also guess that Enumerator class might be just for instantiating Enumerator as Block class does like that. - I suppose the current LazyEnumerator class that is written in Goby is just a temporal and they should be finally integrated into the upcoming Enumerator.
- I recognize that st0012 does not want to make the inheritance path more fat.
This is just a draft memorandum and is not final. If needed, I create a new issue for that. I'd be glad if @st0012 would show ideas around this.
Thank you,
@hachi8833 sorry for the late response
I don't use either Enumerator
or Enumerable
directly very often, so I have limited understanding about those 2. I think we can start by
- creating the
Enumerable
module - moving most of the Array's methods into
Enumerable
and make Array include it - Implement
Enumerator
class
And from the screenshot, you uploaded your implementation looks cool! Can you open a PR for it?
@st1102 thank you for the checking! I just wanted a grand design from you around Enumerable😊
I'd send the WIP PR
@Maxwell-Alexius I hope you could check https://github.com/goby-lang/goby/pull/801