www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - What does ! Stand for in map! and filter! function calls?

reply Rnd <r_narang yahoo.com> writes:
I have recently started using Dlang, hence this basic question. 
Thanks for your insight.
Jun 02 2019
next sibling parent reply Paul Backus <snarwin gmail.com> writes:
On Sunday, 2 June 2019 at 15:48:54 UTC, Rnd wrote:
 I have recently started using Dlang, hence this basic question. 
 Thanks for your insight.
map and filter are templates in D, and !(...) is D's syntax for passing arguments to templates: map!(x => x*x)([1, 2, 3, 4, 5]) ^ ^ | | | +- This array is a normal function argument | +- This lambda is a template argument
Jun 02 2019
parent reply Rnd <r_narang yahoo.com> writes:
On Sunday, 2 June 2019 at 15:55:46 UTC, Paul Backus wrote:
 On Sunday, 2 June 2019 at 15:48:54 UTC, Rnd wrote:
 I have recently started using Dlang, hence this basic 
 question. Thanks for your insight.
map and filter are templates in D, and !(...) is D's syntax for passing arguments to templates: map!(x => x*x)([1, 2, 3, 4, 5]) ^ ^ | | | +- This array is a normal function argument | +- This lambda is a template argument
Is it not possible in the language to have template map function also to called as map(x=>... ? This will reduce complexity which will attract more people to this language. Easy languages have great mass appeal as has been shown with Ruby and Python.
Jun 02 2019
next sibling parent Adam D. Ruppe <destructionator gmail.com> writes:
On Sunday, 2 June 2019 at 16:39:57 UTC, Rnd wrote:
 Is it not possible in the language to have template map 
 function also to called as map(x=>... ?
It could be done pretty easily, but you will have to learn what template arguments are eventually anyway. You'll see this pattern all over, like `to!string(...)` among others that come up pretty early using D.
Jun 02 2019
prev sibling next sibling parent Paul Backus <snarwin gmail.com> writes:
On Sunday, 2 June 2019 at 16:39:57 UTC, Rnd wrote:
 Is it not possible in the language to have template map 
 function also to called as map(x=>... ?

 This will reduce complexity which will attract more people to 
 this language.

 Easy languages have great mass appeal as has been shown with 
 Ruby and Python.
It's possible, but I believe the version that takes the function as a template argument is easier for the compiler to optimize, so doing it that way is a better choice for the standard library. Reducing complexity for first-time learners is a noble goal, but templates are used so pervasively in D that there's really no sense in trying to avoid them. If you'd like to learn more about D's templates, Philippe Sigaud has written an excellent tutorial: https://github.com/PhilippeSigaud/D-templates-tutorial/blob/master/D-templates-tutorial.md
Jun 02 2019
prev sibling parent Dennis <dkorpel gmail.com> writes:
On Sunday, 2 June 2019 at 16:39:57 UTC, Rnd wrote:
 Is it not possible in the language to have template map 
 function also to called as map(x=>... ?
That would be slower, and std.algorithm generally disallows easy but slow functions. In Java, you can sort a linked list. In D, sort requires an array (or random access range). D's `uniq` removes duplicates, but assumes the input range is sorted. Of course you can do myLinkedList.array.sort.uniq but it's not as easy as myLinkedList.uniq because that hides all the stuff that is required to happen behind the scenes. It may be less beginner friendly, but I've also seen newcomers come with a question among the lines of 'I'm trying out the language, why is my little benchmark slower than equivalent Javascript' after which they're told 'don't use dmd, don't use 80-bit floats, use these compiler flags' etc. So it's a trade-off between ease of use by default and performance by default. A clear error message on `map` without ! is the way to go in my opinion.
Jun 02 2019
prev sibling parent Cym13 <cpicard openmailbox.org> writes:
On Sunday, 2 June 2019 at 15:48:54 UTC, Rnd wrote:
 I have recently started using Dlang, hence this basic question. 
 Thanks for your insight.
To know more about D you should take the time to do the D tour [1] which provides you with general knowledge of most of D's functionalities and vocabulary. To know more about templates specifically, check out this tutorial [2]. [1]: https://tour.dlang.org/ [2]: http://nomad.uk.net/articles/templates-in-d-explained.html
Jun 02 2019