From: nobu@ruby-lang.org
To: ruby-dev@ruby-lang.org
Subject: [ruby-dev:49646] [Ruby trunk Feature#4787] Integer#each_modulo(n)
Date: Tue, 07 Jun 2016 05:35:22 +0000 [thread overview]
Message-ID: <redmine.journal-59040.20160607053521.547cf316db31d379@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-4787.20110527131832@ruby-lang.org
Issue #4787 has been updated by Nobuyoshi Nakada.
Description updated
----------------------------------------
Feature #4787: Integer#each_modulo(n)
https://bugs.ruby-lang.org/issues/4787#change-59040
* Author: Kenta Murata
* Status: Assigned
* Priority: Normal
* Assignee: Yukihiro Matsumoto
----------------------------------------
I suggest a new feature of Integer to enumerate by iterated Integer#modulo.
An example implementation in Ruby is the following code:
```ruby
class Integer
def each_modulo(n)
raise ArgumentError, "argument must be an Integer" unless n.is_a? Integer
raise ArgumentError, "argument must be larger than 1" if n <= 1
return Enumerator.new(self, :each_modulo, n) unless block_given?
q = self
while q > 0
q, r = q.divmod(n)
yield(r)
end
end
end
p 133.each_modulo(3).to_a #=> [1, 2, 2, 1, 1]
```
The following code is an example use of the feature:
```ruby
class Integer
def each_thousand_separation
each_modulo(1000)
end
def thousand_separated_string(sep=',')
each_thousand_separation.map(&'%03d'.method(:%)).inject{|s, n| n + sep + s }
end
end
p 100_000_000_200.thousand_separated_string #=> "100,000,000,200"
```
I make an implementation in C, and attach the patch for that.
---Files--------------------------------
each_modulo.patch (3.91 KB)
--
https://bugs.ruby-lang.org/
next prev parent reply other threads:[~2016-06-07 5:00 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2011-05-27 4:21 [ruby-dev:43586] [Ruby 1.9 - Bug #4787][Open] Integer#each_modulo(n) Kenta Murata
2011-06-29 4:33 ` [ruby-dev:43989] [Ruby 1.9 - Feature #4787] Integer#each_modulo(n) Yukihiro Matsumoto
2011-07-16 23:42 ` [ruby-dev:44121] " Thomas Sawyer
2012-10-25 11:16 ` [ruby-dev:46268] [ruby-trunk " yhara (Yutaka HARA)
2016-06-07 5:35 ` nobu [this message]
2016-06-07 6:18 ` [ruby-dev:49647] [Ruby trunk Feature#4787] Integer#each_modulo(n) duerst
2017-12-08 8:50 ` [ruby-dev:50339] [Ruby trunk Feature#4787][Closed] Integer#each_modulo(n) muraken
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-list from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.ruby-lang.org/en/community/mailing-lists/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=redmine.journal-59040.20160607053521.547cf316db31d379@ruby-lang.org \
--to=ruby-dev@ruby-lang.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).