Project

General

Profile

Feature #4787

Integer#each_modulo(n)

Added by mrkn (Kenta Murata) almost 6 years ago. Updated 12 months ago.

Status:
Assigned
Priority:
Normal
Target version:
-
[ruby-dev:43586]

Description

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.

each_modulo.patch View (3.91 KB) mrkn (Kenta Murata), 05/27/2011 01:18 PM


Related issues

Related to Ruby trunk - Feature #12116: `Fixnum#divmod`, `Bignum#divmod` with multiple arguments Open
Related to Ruby trunk - Feature #12447: Integer#digits for extracting digits of place-value notation in any base Closed

History

#1 [ruby-core:36503] Updated by mrkn (Kenta Murata) almost 6 years ago

  • ruby -v changed from ruby 1.9.3dev (2011-05-27 trunk 31745) [x86_64-darwin10.7.0] to -

Hi,

I believe I've sent the following feature request to ruby-core,
but I sent to ruby-dev by mistake.

--
Kenta Murata
Sent with Sparrow (http://www.sparrowmailapp.com)

Forwarded Message:

From: Kenta Murata muraken@gmail.com
In Reply To: ruby-dev@ruby-lang.org
To: ruby developers list ruby-dev@ruby-lang.org
Date: 2011-05-27 13:21:06
Subject: [Ruby 1.9 - Bug #4787][Open] Integer#each_modulo(n)

Issue #4787 has been reported by Kenta Murata.


Bug #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.3
ruby -v: ruby 1.9.3dev (2011-05-27 trunk 31745) [x86_64-darwin10.7.0]

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

#2 Updated by nobu (Nobuyoshi Nakada) almost 6 years ago

  • Tracker changed from Bug to Feature

#3 [ruby-dev:43989] Updated by matz (Yukihiro Matsumoto) almost 6 years ago

  • Target version changed from 1.9.3 to 2.0.0

#4 [ruby-dev:44121] Updated by trans (Thomas Sawyer) almost 6 years ago

Why not just let #modulo/#divmod take a block, rather than define a new method?

#5 Updated by shyouhei (Shyouhei Urabe) about 5 years ago

  • Status changed from Open to Assigned

#6 [ruby-dev:46268] Updated by yhara (Yutaka HARA) over 4 years ago

  • Target version changed from 2.0.0 to next minor

#7 Updated by mrkn (Kenta Murata) 12 months ago

  • Related to Feature #12116: `Fixnum#divmod`, `Bignum#divmod` with multiple arguments added

#8 Updated by mrkn (Kenta Murata) 12 months ago

  • Related to Feature #12447: Integer#digits for extracting digits of place-value notation in any base added

#9 [ruby-dev:49646] Updated by nobu (Nobuyoshi Nakada) 12 months ago

  • Description updated (diff)

#10 [ruby-dev:49647] Updated by duerst (Martin Dürst) 12 months ago

This only gives the 'mod' part. Why not extend this to get both the 'div' part and the 'mod' part back (see also comment #4)?

And why not allow an array of integers as an argument? Some conventions for displaying large numbers with separators are not uniform, in particular in India (see https://en.wikipedia.org/wiki/Indian_numbering_system).

Also available in: Atom PDF