www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - delegates and functions

reply OlegZ <black80 bk.ru> writes:
I want "int delegate( string )", but hz most probably is "int 
function( string )"
 auto hz = (string s) => { writeln( s ); return cast( int 
 )s.length; };
but pragma (msg, typeid( (string s) => { writeln( s ); return cast( int )s.length; })); shows
 typeid( int delegate()  safe function(string s) pure nothrow 
  safe )
Why my lambda is function(string) that returns int delegate()? How I should to write lambda of type "int function( string )"? And how I should to write lambda of type "int delegate( string )"?
Jun 09 2018
parent reply OlegZ <black80 bk.ru> writes:
On Saturday, 9 June 2018 at 20:03:15 UTC, OlegZ wrote:

 auto hz = (string s) => { writeln( s ); return cast( int 
 )s.length; }
How I should to write lambda of type "int delegate( string )?
I found one way:
 auto hz = delegate int( string s ) { writeln( s ); return cast( 
 int )s.length; };
but still don't understand why
 return cast( int )s.length;
at topmost line returns "int delegate()" not just int?
Jun 09 2018
parent reply drug <drug2004 bk.ru> writes:
On 09.06.2018 23:39, OlegZ wrote:
 On Saturday, 9 June 2018 at 20:03:15 UTC, OlegZ wrote:
 
 auto hz = (string s) => { writeln( s ); return cast( int )s.length; }
How I should to write lambda of type "int delegate( string )?
I found one way:
 auto hz = delegate int( string s ) { writeln( s ); return cast( int 
 )s.length; };
but still don't understand why
 return cast( int )s.length;
at topmost line returns "int delegate()" not just int?
just remove `=>`, either: ``` auto hz = (string s) { writeln( s ); return cast( int )s.length; } ``` or ``` auto hz = (s) => writeln( s ); ``` In fact using `=>` you really define a function returning delegate. ``` auto hz = (string s) => { writeln( s ); return cast( int )s.length; } ``` is equivalent to ``` auto hz = (string s) { return () {writeln( s ); return cast( int )s.length;} } ```
Jun 09 2018
parent reply OlegZ <black80 bk.ru> writes:
On Saturday, 9 June 2018 at 20:55:21 UTC, drug wrote:
 In fact using `=>` you really define a function returning 
 delegate.
I see. Thanks.
Jun 09 2018
parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 06/09/2018 02:09 PM, OlegZ wrote:
 On Saturday, 9 June 2018 at 20:55:21 UTC, drug wrote:
 In fact using `=>` you really define a function returning delegate.
I see. Thanks.
There is some explanation at the following page, of how the lambda syntax is related to the full syntax: http://ddili.org/ders/d.en/lambda.html#ix_lambda.=%3E Ali
Jun 09 2018
parent reply OlegZ <black80 bk.ru> writes:
On Saturday, 9 June 2018 at 22:28:22 UTC, Ali Çehreli wrote:
 There is some explanation at the following page, of how the 
 lambda syntax is related to the full syntax:
   http://ddili.org/ders/d.en/lambda.html#ix_lambda.=%3E
copy rect from article as image https://i.gyazo.com/c23a9139688b7ed59fbe9c6cdcf91b93.png well, I can to remember that lambda () => { return ..; } returns another implicit lambda but my mind refuses to understand fact that ...
 auto l1 = (int a) => a + 1; // returns incremented value
 auto l2 = (int a) => { return a + 1; }; // return another 
 lambda that increment value
... very different artifacts. WHY? it's looks same, but in DLang it's some kind of mindtrap, looks mindness and falsy. I am upset and angry cause somebody just told me "There Is No Santa Claus in Dlang!" (I fell same yesterday and almost calmed down already) D-compiler: you want one lambda? you are lucky today! I'll give you two lambda in square(^2) for same price! do not thank! really, if I want lambda that returns another lambda I can write
 auto l1 = (int a) => () => a + 1; // maybe I need it in some 
 special case
but if I want lambda with return... well, I should forget about shorter syntax and write
 auto l1 = (int a) { /*...*/ return a + 1; } // why I can't use 
 "=>" here?
silent cry. yes, this version is shorter than with "=>", but my before today) and I try to do something in Dlang. my state when I stick to such things looks like this (pic about JavaScript) https://pics.me.me/javascript-%3E-53-53-5-2-5-2-%3E-53-ah-gonna-29130223.png DON'T READ NEXT I readed 2 weeks ago post "Dlang is a crap". well, I agree with author in many thing. * and such small things like (not)using "=>" can kickass you for hours. * and naming exceptions for example in std.concurrency - some ends with "Exception" some not - told me that no one center in Dlang development, no good organization and no standard. * and VisualD sometimes lost autocompletion/intellisense and there's no way to get it back, except to restart the computer. * and parsing Binance(cryptoexchange) JSON-response "All Market Tickers Stream" from https://github.com/binance-exchange/binance-official-api-docs/blob/master/w b-socket-streams.md (~150 submessages about 200KB) with ctRegex(that told as very fast) spend 13ms. task is find every submessage that starts with '{' and ends with '}' and parse it from JSON to some struct. but simple replace ctRegex to indexOf( '{'|'}' ) decrease time to 2ms only - 90% of time getting filled 150 structs with data (that means find all string values in submessage and convert it to some types like double or long with my CTFE-code, not Dlang-JSON) spend compile-time Regex for searching start{ and end}. WTF? well, Go(version and no string-spans(that means alloc every substring in GC) and all strings are Unicode(WebSocket sent UTF8, that adds Encoding time to Unicode) - spend only 3ms for all. Dlang can do things better and faster but it don't want (or people don't want) * and many "nothrow nogc trusted pure" near every function in Phobos and you should read every function attrubute when you try to understand exactly what does and how works this function, cause you can miss some important attribute. I don't know how it should be done/removed from eyes. probably better such stuff describe before function like [context: nogc noBoundsCheck nothrow pure trusted] int func( args... ) cause you can vissually ignore [context..] and look at function without urban garbage.. maybe it should looks in editor like checkbox spans in right side of editor where you can turn off/on such attributes but text is clear not over..loaded? (English is not my native) * and everybody will change nothing with textual representation of code cause this things used already in many code and packages. (except VisualD that in some days will be fixed) it's sad. over Dlang' code textual representation should be used another sugar representation that hides and reorganize some things (like TypeScript over JavaScript or MOC in Qt) when Rust will add GC I will go to Rust - I dont like ownership stuff. when Go will add OOP & exceptions I will go to Go - I dont like check every function call with if err.. also I can live with type traits without classes in most cases. probably I will go. I like in Dlang: native (with asm), GC, CTFE, OOP, functional style... (I am not a professional with Dlang, I learning it) but sometimes Dlang is little baby who can piss/shit you in moments when you really not expect and you haven't diaper or any protection for that cases. YOU CAN READ AGAIN can somebody tell me please what was the reason that lambda (blah) => { return value + 1; } actionally returns another lambda that returns value+1? what case it can be usefull? why Dlang gives me lambda of lambda where I want lambda written with "=> { return"? I am expecting some magic and simple trick with lambdas.
Jun 10 2018
parent reply drug <drug2004 bk.ru> writes:
On 10.06.2018 12:21, OlegZ wrote:
 On Saturday, 9 June 2018 at 22:28:22 UTC, Ali Çehreli wrote:
 There is some explanation at the following page, of how the lambda 
 syntax is related to the full syntax:
   http://ddili.org/ders/d.en/lambda.html#ix_lambda.=%3E
copy rect from article as image https://i.gyazo.com/c23a9139688b7ed59fbe9c6cdcf91b93.png > well, I can to remember that lambda () => { return ..; } returns another implicit lambda
you shouldn't remember it. you need to understand that `a=>2*a` is short form of `(a) { return 2a; }. So using `a=>{ return 2*a; }` you get ``` (a) { return (){ return 2*a; }; } ``` i.e. a function returning delegate. ``` a => { return 2*a; } /\ \ / || \ / || \ / || \ / || \ / This is \ / function \ This is definition of delegate definition \ so you have a function that returns delegate. ``` it's like a => a => 2*a; or (a){ return () { return 2*a; } just first version is much simpler and namely convenience is the reason of this syntax I guess. >
 can somebody tell me please what was the reason that lambda (blah) => { 
 return value + 1; } actionally returns another lambda that returns 
 value+1? what case it can be usefull? why Dlang gives me lambda of 
 lambda where I want lambda written with "=> { return"?
 I am expecting some magic and simple trick with lambdas.
You misuse two different form of the same. The form with "=>" is very convenient if expression is short, in other cases the form `(){}` is suited better.
Jun 10 2018
parent reply SrMordred <patric.dexheimer gmail.com> writes:
 a => { return 2*a; }
   /\  \           /
   ||   \         /
   ||    \       /
   ||     \     /
   ||      \   /
  This is   \ /
  function   \  This is definition of delegate
  definition  \

 	so you have a function that returns delegate.

 ```
 it's like
 a => a => 2*a;
 or
 (a){ return () { return 2*a; }
I believe that a lot of other languages work differently, so when their knowledge are transposed to D, people get confused at first. Eg. in JS: x = x => x * 2; x = x =>{ return x * 2; } //equivalent
Jun 10 2018
parent drug <drug2004 bk.ru> writes:
On 10.06.2018 20:58, SrMordred wrote:
 a => { return 2*a; }
   /\  \           /
   ||   \         /
   ||    \       /
   ||     \     /
   ||      \   /
  This is   \ /
  function   \  This is definition of delegate
  definition  \

     so you have a function that returns delegate.

 ```
 it's like
 a => a => 2*a;
 or
 (a){ return () { return 2*a; }
I believe that a lot of other languages work differently, so when their knowledge are transposed to D, people get confused at first. Eg. in JS: x = x => x * 2; x = x =>{ return x * 2; } //equivalent
Ah, I see. From this point of view there is some inconsistency indeed.
Jun 10 2018