Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Enumerable Module with Enumerator Class #342

Open
Alexius-Huang opened this issue Aug 19, 2017 · 7 comments
Open

Implement Enumerable Module with Enumerator Class #342

Alexius-Huang opened this issue Aug 19, 2017 · 7 comments

Comments

@Alexius-Huang
Copy link
Member

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.

@Alexius-Huang
Copy link
Member Author

Alexius-Huang commented Aug 24, 2017

Hint: * means if no block given, then returns the Enumerator itself

Method list implementation:

  • Enumerable#all? Returns true if the block never returns false or nil
  • Enumerable#any? Returns true if the block ever returns a value other than false or nil
  • *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 number n then runs enumerator n times repeated
  • *Enumerable#detect Enumerator#find
  • Enumerable#drop Drops first n 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 returns nil or false and returns an array containing the remaining elements
  • *Enumerable#each_cons and Enumerable#each_slice Iterates the given block for each array of consecutive n elements
  • *Enumerable#each_entry Converting multiple values from yield to an array
  • *Enumerable#each_with_index
  • *Enumerable#each_with_object
  • Enumerable#entries and Enumerable#to_a Returns array containing items in enumerator
  • Enumerable#empty? Returns true 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
  • Enumerable#first Returns first n element (default n 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? and Enumerable#member?
  • Enumerable#inject and Enumerable#reduce
  • Enumerable#lazy (Lazy Enumerator need more survey)
  • Enumerable#max Returns the object of enum with n maximun value (default n 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? Returns true if none of the block value results true ([nil] is none but not empty!)
  • Enumerable#one? Returns true if block result returns true exactly once
  • Enumerable#partition Returns two array elements in an array which the first array stores the value that the block evaluates true while the second array stores value that the block evaluates false
  • *Enumerable#reject Returns array of all elements for which block result false
  • *Enumerable#reverse_each
  • Enumerable#slice_after
  • Enumerable#slice_before
  • Enumerable#slice_when
  • Enumerable#sort
  • *Enumerable#sort_by
  • Enumerable#take Returns first n 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 in self
  • Enumerable#zip

@Alexius-Huang Alexius-Huang changed the title Implement Enumerable Module Implement Enumerable Module with Enumerator Class Aug 26, 2017
@st0012
Copy link
Member

st0012 commented Oct 27, 2019

@Maxwell-Alexius Are you still interested in working on this?

@Alexius-Huang
Copy link
Member Author

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

@st0012 st0012 added this to the version 0.2.0 milestone Oct 31, 2019
@hachi8833
Copy link
Member

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.

image

image

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,

@st0012
Copy link
Member

st0012 commented Nov 12, 2019

@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

  1. creating the Enumerable module
  2. moving most of the Array's methods into Enumerable and make Array include it
  3. Implement Enumerator class

And from the screenshot, you uploaded your implementation looks cool! Can you open a PR for it?

@hachi8833
Copy link
Member

@st1102 thank you for the checking!
I just wanted a grand design from you around Enumerable😊

I'd send the WIP PR

@hachi8833
Copy link
Member

@Maxwell-Alexius I hope you could check #801

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants