extend foreach to work on non-arrays

Jordan Wilson wilsonjord at gmail.com
Fri May 25 04:28:31 UTC 2018


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:
>>> the idea in the first place. I needed a hammer but no one 
>>> invented it. If I give you my use case then there would be 
>>> two main outcomes: You attempt to find a workaround for the 
>>> use case or claim that it is not applicable. These are the "I 
>>> have a rock that should work as good as that hammer thingy 
>>> you want" and "Hammers are useless".
>>
>> 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
>
> Giving specific examples that are not applicable to your 
> knowledge base won't help you. If you are too noobish to see 
> how unifying foreach across non-arrays is useful then you have 
> no experience where it is. Sure I could give some examples but 
> then you have the option of excepting them as useful or saying 
> they are too trivial.
>
> If you can't think of examples on your own then you are 
> probably not ready for the increase expressiveness of what the 
> feature allows. It simply means don' t use the new feature. Why 
> worry about something you don't understand?
>
> void foo(T)(T t)
> {
>   foreach(a; t)
>   {
>
>   }
> }
>
> Does that help? I doubt it. If you are too much of a noob you 
> won't get why the above makes certain things easier. If you 
> don't have a good working knowledge of templates then you won't 
> see how such a feature enhancement can simplify things.
>
> 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
>
> See? and this is just an arbitrary example that I hope makes 
> you happy. My examples are more complex and I don't see why it 
> is necessary for me to waste 30m of my time extracting and 
> developing a demo just to show that the feature is useful. It 
> really boils down to the fact that if you can't see it being 
> useful to you then it isn't and if you can then it is. A noob 
> isn't going to be able to determine if the feature is sound or 
> not so it is pointless for a noob to really worry about it.

I genuinely read these forums to increase my learning, and it's 
slow progress, but I believe it does help (obviously actually 
programming and looking at the many resources available online 
plays a big part too).

My intention wasn't to make you justify yourself; not at all. My 
intention was to simply to say that there are some people (well, 
maybe it is just me) who genuinely learn from more advanced 
programmers simply by reading their discussions with other 
advanced programmers.

I suppose I was just trying to be encouraging, but clearly it was 
more annoying, so my apologies for that.







More information about the Digitalmars-d mailing list