ruby-dev (Japanese) list archive (unofficial mirror)
 help / color / mirror / Atom feed
From: Yukihiro Matsumoto <matz@ruby-lang.org>
To: ruby-dev@ruby-lang.org (ruby developers list)
Subject: [ruby-dev:43989] [Ruby 1.9 - Feature #4787] Integer#each_modulo(n)
Date: Wed, 29 Jun 2011 13:33:40 +0900	[thread overview]
Message-ID: <redmine.journal-18656.20110629133339@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-4787.20110527131832@ruby-lang.org


Issue #4787 has been updated by Yukihiro Matsumoto.

Target version changed from 1.9.3 to 1.9.x


----------------------------------------
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

  reply	other threads:[~2011-06-29  4:23 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 ` Yukihiro Matsumoto [this message]
2011-07-16 23:42 ` [ruby-dev:44121] [Ruby 1.9 - Feature #4787] Integer#each_modulo(n) Thomas Sawyer
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-18656.20110629133339@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).