So... add maxCount and maxPos?

Ivan Kazmenko via Digitalmars-d digitalmars-d at puremagic.com
Thu Jan 21 04:49:07 PST 2016


On Thursday, 21 January 2016 at 12:17:26 UTC, default0 wrote:
> On Thursday, 21 January 2016 at 02:36:05 UTC, Ivan Kazmenko 
> wrote:
>> An alternative would be to define min(one argument) to just be 
>> that argument.  That would be consistent with what we have 
>> now, but violates the principle of least astonishment for 
>> newcomers: why would min ([3, 1, 2]) return [3, 1, 2] and not 
>> 1?  Currently, it just does not compile.
>
> As a newcomer to the language, and as a reader of 
> https://dlang.org/phobos/std_algorithm_comparison.html#min 
> which states "Iterates the passed arguments and returns the 
> minimum value." my intuitive understanding would be that yes of 
> course min(x) returns x.
> However the above could of course also be reworded as "Iterates 
> the passed list of arguments and returns the minimum value." to 
> be even more clear about it NOT iterating individual arguments 
> but the argument list as a whole :-)
>
> If I were to be a newcomer to programming in general this might 
> be confusing, though. However, it's certainly consistent and 
> easy to wrap your head around and also what I would have 
> expected it to do the first time I came across the function.

Still, looks like Java, C# and Python have min work this way: 
given a single argument which is a collection, it returns the 
minimum in that collection, not the collection itself.  A Python 
example:

min ([1])  // 1
min ([2, 1])  // 1
min ((([1, 2], [2, 3])))  // [1, 2]
min ((([1, 2])))  // 1
min ((([1, 2],),))  // ([1, 2],)

In Python, the what happens is also tricky:

def g(): return (1, 2)  // tuple of two values
g()  // (1, 2)
min (g())  // 1 from tuple
min (*g())  // 1 from expanded tuple

def f(): return (1,)  // tuple of a single value
f()  // (1,)
min (f())  // 1
min (*f())  // error, cannot expand



More information about the Digitalmars-d mailing list