Project

General

Profile

Actions

Feature #4787

closed

Integer#each_modulo(n)

Added by mrkn (Kenta Murata) almost 13 years ago. Updated over 6 years ago.

Status:
Closed
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.


Files

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

Related issues 2 (1 open1 closed)

Related to Ruby master - Feature #12116: `Fixnum#divmod`, `Bignum#divmod` with multiple argumentsOpenActions
Related to Ruby master - Feature #12447: Integer#digits for extracting digits of place-value notation in any baseClosedmatz (Yukihiro Matsumoto)Actions

Updated by mrkn (Kenta Murata) almost 13 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
In Reply To:
To: ruby developers list
Date: 2011-05-27 13:21:06
Subject: [ruby-dev:43586] [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

Actions #2

Updated by nobu (Nobuyoshi Nakada) almost 13 years ago

  • Tracker changed from Bug to Feature

Updated by matz (Yukihiro Matsumoto) almost 13 years ago

  • Target version changed from 1.9.3 to 2.0.0

Updated by trans (Thomas Sawyer) almost 13 years ago

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

Actions #5

Updated by shyouhei (Shyouhei Urabe) about 12 years ago

  • Status changed from Open to Assigned

Updated by yhara (Yutaka HARA) over 11 years ago

  • Target version changed from 2.0.0 to 2.6
Actions #7

Updated by mrkn (Kenta Murata) almost 8 years ago

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

Updated by mrkn (Kenta Murata) almost 8 years ago

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

Updated by nobu (Nobuyoshi Nakada) almost 8 years ago

  • Description updated (diff)

Updated by duerst (Martin Dürst) almost 8 years 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).

Updated by mrkn (Kenta Murata) over 6 years ago

  • Status changed from Assigned to Closed

This was resolved by #12447.

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0