ixid:

The prime factorization of 1 is an empty set, so surely to be correct it should return [] when given 1 and not throw an exception.

The Task says that the input can't be 1, so the input 1 needs to be a pre-condition violation:

Write a function which returns an array or collection which contains the prime decomposition of a given number, n, greater than 1<


This also suggests a possible modification to [].reduce!"a * b" as mathematically the product of the empty set is defined as 1.

reduce() is a general function, so it's not supposed to know that.

Python reduce does the same:


reduce(lambda a, b: a * b, [])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: reduce() of empty sequence with no initial value


If you want that, then you have to use:
reduce!"a * b"(1, items)

And some time from now:
items.reduce!"a * b"(1)


If we add a product() function to Phobos similar to sum() (http://d.puremagic.com/issues/show_bug.cgi?id=4725 ) then I agree that for empty ranges it will need to return the multiplicative identity element.

Bye,
bearophile

Reply via email to