From: Thomas Sawyer <transfire@gmail•com>
To: ruby-dev@ruby-lang.org (ruby developers list)
Subject: [ruby-dev:44121] [Ruby 1.9 - Feature #4787] Integer#each_modulo(n)
Date: Sun, 17 Jul 2011 08:42:46 +0900 [thread overview]
Message-ID: <redmine.journal-19206.20110717084245@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-4787.20110527131832@ruby-lang.org
Issue #4787 has been updated by Thomas Sawyer.
Why not just let #modulo/#divmod take a block, rather than define a new method?
----------------------------------------
Feature #4787: Integer#each_modulo(n)
http://redmine.ruby-lang.org/issues/4787
Author: Kenta Murata
Status: Open
Priority: Normal
Assignee: Yukihiro Matsumoto
Category: core
Target version: 1.9.x
I suggest a new feature of Integer to enumerate by iterated Integer#modulo.
An example implementation in Ruby is the following code:
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:
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.
--
http://redmine.ruby-lang.org
next prev parent reply other threads:[~2011-07-16 23:29 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 ` Thomas Sawyer [this message]
2012-10-25 11:16 ` [ruby-dev:46268] [ruby-trunk " yhara (Yutaka HARA)
2016-06-07 5:35 ` [ruby-dev:49646] [Ruby trunk Feature#4787] Integer#each_modulo(n) nobu
2016-06-07 6:18 ` [ruby-dev:49647] " 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-19206.20110717084245@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).