View mode: basic / threaded / horizontal-split · Log in · Help
July 19, 2012
Just where has this language gone wrong?
Hi,

I'm an occasional lurker on the D forums just to see where the 
language is going,but I'm a little puzzled. In another thread I 
found this code

auto r = [5, 3, 5, 6, 8].sort.uniq.map!(x => x.to!string);

I don't understand whats going on here. Int array is getting 
sorted, then Uniqued, then what? What type is x? What kind of 
operator is =>, why is x.to!string allowed template 
specialization should say x.to!(string), which leads me to think 
that there are multiple syntaxes for things(why I hate dynamic 
languages, love compiled)

On another note, (copied from wikipedia)

foreach(item; set) {
  // do something to item
}

what's with the lax syntax being allowed? Shouldn't it be at 
least specified "auto item"?

I'm sorry I don't mean to be a criticizer, but it seems to me 
that D is trying to be a dynamic-like compiled language way too 
hard.
July 19, 2012
Re: Just where has this language gone wrong?
Am 19.07.2012 16:21, schrieb Petr Janda:
> Hi,
>
> I'm an occasional lurker on the D forums just to see where the language
> is going,but I'm a little puzzled. In another thread I found this code
>
> auto r = [5, 3, 5, 6, 8].sort.uniq.map!(x => x.to!string);
>
> I don't understand whats going on here. Int array is getting sorted,
> then Uniqued, then what? What type is x? What kind of operator is =>,
> why is x.to!string allowed template specialization should say
> x.to!(string), which leads me to think that there are multiple syntaxes
> for things(why I hate dynamic languages, love compiled)
>
> On another note, (copied from wikipedia)
>
> foreach(item; set) {
>    // do something to item
> }
>
> what's with the lax syntax being allowed? Shouldn't it be at least
> specified "auto item"?
>
> I'm sorry I don't mean to be a criticizer, but it seems to me that D is
> trying to be a dynamic-like compiled language way too hard.
>

=> → New Lambda Syntax
.sort.uniq.map(…) → UFCS (Uniform Function Call Syntax, iirc)

Array gets sorted, then doubles are removed (uniq) and then everything 
is converted to a string (map).

Everything was recently introduced around 2.059.
July 19, 2012
Re: Just where has this language gone wrong?
On Thursday, 19 July 2012 at 14:21:47 UTC, Petr Janda wrote:
> Hi,
>
> I'm an occasional lurker on the D forums just to see where the 
> language is going,but I'm a little puzzled. In another thread I 
> found this code
>
> auto r = [5, 3, 5, 6, 8].sort.uniq.map!(x => x.to!string);
>
> I don't understand whats going on here. Int array is getting 
> sorted, then Uniqued, then what? What type is x? What kind of 
> operator is =>, why is x.to!string allowed template 
> specialization should say x.to!(string), which leads me to 
> think that there are multiple syntaxes for things(why I hate 
> dynamic languages, love compiled)
>
> On another note, (copied from wikipedia)
>
> foreach(item; set) {
>   // do something to item
> }
>
> what's with the lax syntax being allowed? Shouldn't it be at 
> least specified "auto item"?
>
> I'm sorry I don't mean to be a criticizer, but it seems to me 
> that D is trying to be a dynamic-like compiled language way too 
> hard.

(arguments) => result-expression is a lambda expression; sort of 
shorthand for (arguments) { return result-expression; } anonymous 
function. x.to!string thingy is the new UFCS stuff which 
basically allows you to transform any foo(x, y, z) to x.foo(y, z) 
(so instead of calling a(b(c(d(e(f))))) you can just call 
a.b.c.d.e.f())

dynamic languages are awesome, they have their pros and cons, for 
some things they're way better than static languages but for 
other things static will fit better.
July 19, 2012
Re: Just where has this language gone wrong?
btw - as for your complains - I would blame poor D documentation 
more than the feature itself; as for "what type is x", it's 
inferred from the prototype of the called function; type 
inference is a standard feature in many static languages.
July 19, 2012
Re: Just where has this language gone wrong?
On Thursday, 19 July 2012 at 14:21:47 UTC, Petr Janda wrote:
> Hi,

Hi

> I'm an occasional lurker on the D forums just to see where the 
> language is going,but I'm a little puzzled. In another thread I 
> found this code
>
> auto r = [5, 3, 5, 6, 8].sort.uniq.map!(x => x.to!string);

Here's list what happens:

 1) Array gets sorted
 2) Duplicate elements gets removed (only unique stay)
 3) Then it get's maped by delegate. It converts numbers into 
strings.
    `r` variable will be ["3", "5", "6", "8"]

> What type is x?
Type of x is inferred.

> What kind of operator is =>
Syntatic sugar for delegates.

> On another note, (copied from wikipedia)
>
> foreach(item; set) {
>   // do something to item
> }

Item type is inferred from `set`, it's just syntactic sugar. Of 
course you can use `auto` but you don't have to.
July 19, 2012
Re: Just where has this language gone wrong?
> Array gets sorted, then doubles are removed (uniq) and then 
> everything is converted to a string (map).
>
> Everything was recently introduced around 2.059.

Ok, but what is map!(). What's the point of the exclamation mark, 
is it a template specialization?
July 19, 2012
Re: Just where has this language gone wrong?
"q66" , dans le message (digitalmars.D:172716), a écrit :
> (so instead of calling a(b(c(d(e(f))))) you can just call 
> a.b.c.d.e.f())

rather f.e.d.c.b.a, if you omit the empty parenthesis after each letter 
(but f).
July 19, 2012
Re: Just where has this language gone wrong?
On 19-07-2012 16:31, Petr Janda wrote:
>> Array gets sorted, then doubles are removed (uniq) and then everything
>> is converted to a string (map).
>>
>> Everything was recently introduced around 2.059.
>
> Ok, but what is map!(). What's the point of the exclamation mark, is it
> a template specialization?

It means template instantiation. The parameter list following the 
exclamation mark are passed to the template, while the second parameter 
list contains the normal runtime arguments.

-- 
Alex Rønne Petersen
alex@lycus.org
http://lycus.org
July 19, 2012
Re: Just where has this language gone wrong?
On Thursday, 19 July 2012 at 14:31:41 UTC, Petr Janda wrote:
>> Array gets sorted, then doubles are removed (uniq) and then 
>> everything is converted to a string (map).
>>
>> Everything was recently introduced around 2.059.
>
> Ok, but what is map!(). What's the point of the exclamation 
> mark, is it a template specialization?

stuff after ! specifies template arguments, in this case a 
predicate; "map" is a standard function in many languages; what 
it does basically is to go over an iterable object, apply the 
predicate function given in the template argument to each element 
and returns a new iterable containing the results of the 
predicate call on each element.
July 19, 2012
Re: Just where has this language gone wrong?
On Thursday, 19 July 2012 at 14:33:49 UTC, q66 wrote:
> On Thursday, 19 July 2012 at 14:31:41 UTC, Petr Janda wrote:
>>> Array gets sorted, then doubles are removed (uniq) and then 
>>> everything is converted to a string (map).
>>>
>>> Everything was recently introduced around 2.059.
>>
>> Ok, but what is map!(). What's the point of the exclamation 
>> mark, is it a template specialization?
>
> stuff after ! specifies template arguments, in this case a 
> predicate; "map" is a standard function in many languages; what 
> it does basically is to go over an iterable object, apply the 
> predicate function given in the template argument to each 
> element and returns a new iterable containing the results of 
> the predicate call on each element.

for example, auto x = [ 5, 10, 15, 20 ]; assert(map!(x => x + 
1)(x) == [ 6, 11, 16, 21 ])
« First   ‹ Prev
1 2 3 4 5
Top | Discussion index | About this forum | D home