extend foreach to work on non-arrays

IntegratedDimensions IntegratedDimensions at gmail.com
Fri May 25 19:06:54 UTC 2018


On Friday, 25 May 2018 at 09:57:20 UTC, meppl wrote:
> On Friday, 25 May 2018 at 03:34:43 UTC, IntegratedDimensions 
> wrote:
>> On Friday, 25 May 2018 at 02:43:39 UTC, Jordan Wilson wrote:
>>> On Thursday, 24 May 2018 at 23:22:56 UTC, 
>>> IntegratedDimensions wrote:
>>>> [...]
>>>
>>> 3rd outcome: noobs like me who read the forums who benefit 
>>> from such discussion.
>>>
>>> Of course, you could be as disinterested in the 3rd outcome 
>>> as you are the 1st and 2nd, and that's completely fair.
>>>
>>> Jordan
>>
>
>>
>> So, I will solve the homework problem for you:
>>
>> auto max(T)(T t)
>> {
>>   baseTypeOf(T) a;
>>   foreach(a; t)
>>      a = max(a,t)
>>   return a;
>> }
>>
>> max(3) = 3
>> max([3,4,5]) = 5
>>
>
>
> you didnt take your time to give a proper example. That code is 
> wrong in multiple ways. People would have to reconstruct your 
> code

Why should I take my time to give a fucking proper example? Why 
the hell are you people so retarded?

https://dpaste.dzfl.pl/1f5578364b6d


Here is your "proper" example since you seem to need to be spoon 
fed like a little baby. I'm sorry, but if you can't read pseudo 
code you shouldn't be programming. To bitch at someone else for 
crossing your t's and dotting your i's when they have no 
obligation is not only moronic but selfish. I have better things 
to do then babysit you to make sure you can understand things. 
How bout you learn to code on your own so you can fill in the 
blanks rather than needing everything spelled out? I will only 
spell everything out once for you, don't expect it and don't ask 
for it any more:


import std.stdio;

auto elseOnly(T)(T t) {
   import std.range: isInputRange;
   static if (isInputRange!T) {
     return t;
   } else  {
     import std.range: only;
     return only(t);
   }
}

auto max(T)(T a, T b)
{
	return (a >= b) ? a : b;
}

auto max(T)(T t)
{
   import std.traits;
   static if (isArray!(T)) typeof(t[0]) c; else T c;
	

   foreach(a; t.elseOnly())
      c = max(a,c);
   return c;
}

void main()
{
	auto x = max(3);
	auto y = max([3,4,5]);
	
	writeln(x);
	writeln(y);
	
}

If there is some specific problem with you not understanding 
pseudo code then you can ask for clarification, but don't try to 
pretend it is my problem for your ignorance.





More information about the Digitalmars-d mailing list