Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #105823

Re: lambda functions in C

From jacobnavia <jacob@jacob.remcomp.fr>
Newsgroups comp.lang.c
Subject Re: lambda functions in C
Date 2017-03-11 21:45 +0100
Organization A noiseless patient Spider
Message-ID <oa1ngf$7ml$1@dont-email.me> (permalink)
References <da2ac707-2a39-41c2-9ac4-20c4811318e8@googlegroups.com>

Show all headers | View raw


Le 08/03/2017 à 19:04, Thiago Adams a écrit :
>
> One feature I miss from C++ is lambdas expressions.
>
> In C++, we can create a function pointer like this:
>
> int (*F)(int) = [](int i) -> int
> {
>   return i * 2;
> };
>
> F(1);
>
> and also pass it as a function argument.
>
> void F2(int (*F)(int));
>
> F2([](int i) -> int
> {
>   return i * 2;
> });
>
> When a C++ lambda doesn't have any capture of variables
> ( the [] part )  it is convertible to normal function pointer.
>
> This feature could be added as it is into the C language.
> The generated code is an internal linkage anonymous function.
>

What about

int anonymousFunction1(int i)
{
     return i*2;
}

int (*F2)(int);

F2 = anonymousFunction1;


> This is a kind of feature I would add in my C' extensions.
>
> My question is what would be the syntax for C? Like C++?
>

What's wrong with the current syntax? It should be fairly easy to write 
a macro to shorten the typing or have a keyboard shortcut in the IDE.

> I think the possible syntax for C could be something similar
> of struct initializer.
>
>
> typedef struct{
>   int x;
>   int y;
> } Point;
>
> void F(Point pt){}
>
> Point pt = { .x = 1, .y = 2};
> F((Point){ .x = 1, .y = 2});
>

And why not

Point NewPoint(int x,int y);

????

Why make it obfuscated when we can write it clearly like that?


> Using an similar syntax:
>
> int (*F)(int) = int (*)(int i){ return 2*i; }
>
> F2(int (*)(int i)
>    { return 2*i;
>    });
>
> What do think?

Maybe, but that looks fairly incomprehensible to do the same thing...


> Do you see any problem to parse this?

No, the problem is not parsing.

I just do not see what this obfuscated syntax brings as improvement.

> Would you prefer the C++ syntax?
>

I would prefer the C syntax.

> I assume everyone would agree that this feature is useful.
> If not, why?
>

I do not see what do you want to do, i.e. a practical application of 
that syntax.


Or such.

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

lambda functions in C Thiago Adams <thiago.adams@gmail.com> - 2017-03-08 10:04 -0800
  Re: lambda functions in C BartC <bc@freeuk.com> - 2017-03-08 18:20 +0000
    Re: lambda functions in C Chad <cdalten@gmail.com> - 2017-03-08 11:12 -0800
      Re: lambda functions in C Thiago Adams <thiago.adams@gmail.com> - 2017-03-08 11:36 -0800
        Re: lambda functions in C scott@slp53.sl.home (Scott Lurndal) - 2017-03-08 20:43 +0000
        Re: lambda functions in C David Brown <david.brown@hesbynett.no> - 2017-03-08 21:55 +0100
          Re: lambda functions in C Thiago Adams <thiago.adams@gmail.com> - 2017-03-08 13:18 -0800
            Re: lambda functions in C Thiago Adams <thiago.adams@gmail.com> - 2017-03-10 12:44 -0800
        Re: lambda functions in C Chad <cdalten@gmail.com> - 2017-03-08 13:03 -0800
    Re: lambda functions in C Thiago Adams <thiago.adams@gmail.com> - 2017-03-08 11:33 -0800
      Re: lambda functions in C scott@slp53.sl.home (Scott Lurndal) - 2017-03-08 20:46 +0000
        Re: lambda functions in C Thiago Adams <thiago.adams@gmail.com> - 2017-03-08 13:25 -0800
      Re: lambda functions in C "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2017-03-10 13:55 -0500
        Re: lambda functions in C supercat@casperkitty.com - 2017-03-10 11:09 -0800
          Re: lambda functions in C "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2017-03-10 14:51 -0500
        Re: lambda functions in C BartC <bc@freeuk.com> - 2017-03-10 19:19 +0000
          Re: lambda functions in C "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2017-03-10 14:53 -0500
            Re: lambda functions in C BartC <bc@freeuk.com> - 2017-03-11 14:17 +0000
              Re: lambda functions in C "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2017-03-11 06:55 -0800
                Re: lambda functions in C "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2017-03-11 07:05 -0800
  Re: lambda functions in C Malcolm McLean <malcolm.mclean5@btinternet.com> - 2017-03-08 13:55 -0800
    Re: lambda functions in C Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-03-08 23:41 +0000
      Re: lambda functions in C Malcolm McLean <malcolm.mclean5@btinternet.com> - 2017-03-09 05:03 -0800
        Re: lambda functions in C Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-03-09 14:06 +0000
        Re: lambda functions in C BartC <bc@freeuk.com> - 2017-03-09 14:20 +0000
          Re: lambda functions in C Malcolm McLean <malcolm.mclean5@btinternet.com> - 2017-03-09 08:07 -0800
            Re: lambda functions in C David Brown <david.brown@hesbynett.no> - 2017-03-09 21:50 +0100
              Re: lambda functions in C GOTHIER Nathan <nathan.gothier@gmail.com> - 2017-03-09 23:38 +0100
                Re: lambda functions in C Ike Naar <ike@iceland.freeshell.org> - 2017-03-09 22:46 +0000
                Re: lambda functions in C GOTHIER Nathan <nathan.gothier@gmail.com> - 2017-03-10 08:39 +0100
                Re: lambda functions in C Ike Naar <ike@iceland.freeshell.org> - 2017-03-10 08:03 +0000
                Re: lambda functions in C GOTHIER Nathan <nathan.gothier@gmail.com> - 2017-03-10 09:22 +0100
                Re: lambda functions in C Ike Naar <ike@iceland.freeshell.org> - 2017-03-10 19:24 +0000
                Re: lambda functions in C GOTHIER Nathan <nathan.gothier@gmail.com> - 2017-03-10 23:23 +0100
                Re: lambda functions in C Ike Naar <ike@iceland.freeshell.org> - 2017-03-11 18:02 +0000
                Re: lambda functions in C Jerry Stuckle <jstucklex@attglobal.net> - 2017-03-09 20:48 -0500
                Re: lambda functions in C GOTHIER Nathan <nathan.gothier@gmail.com> - 2017-03-10 08:57 +0100
                Re: lambda functions in C Jerry Stuckle <jstucklex@attglobal.net> - 2017-03-10 11:26 -0500
                Re: lambda functions in C GOTHIER Nathan <nathan.gothier@gmail.com> - 2017-03-10 17:53 +0100
                Re: lambda functions in C Jerry Stuckle <jstucklex@attglobal.net> - 2017-03-10 13:17 -0500
                Re: lambda functions in C David Brown <david.brown@hesbynett.no> - 2017-03-11 17:11 +0100
                Re: lambda functions in C Malcolm McLean <malcolm.mclean5@btinternet.com> - 2017-03-10 00:38 -0800
                Re: lambda functions in C GOTHIER Nathan <nathan.gothier@gmail.com> - 2017-03-10 10:23 +0100
                Re: lambda functions in C Jerry Stuckle <jstucklex@attglobal.net> - 2017-03-10 11:37 -0500
                Re: lambda functions in C Malcolm McLean <malcolm.mclean5@btinternet.com> - 2017-03-10 09:07 -0800
                Re: lambda functions in C Jerry Stuckle <jstucklex@attglobal.net> - 2017-03-10 13:22 -0500
                Re: lambda functions in C Malcolm McLean <malcolm.mclean5@btinternet.com> - 2017-03-10 10:47 -0800
                Re: lambda functions in C Jerry Stuckle <jstucklex@attglobal.net> - 2017-03-10 14:57 -0500
                Re: lambda functions in C Malcolm McLean <malcolm.mclean5@btinternet.com> - 2017-03-10 12:26 -0800
                Re: lambda functions in C Chad <cdalten@gmail.com> - 2017-03-10 12:49 -0800
                Re: lambda functions in C GOTHIER Nathan <nathan.gothier@gmail.com> - 2017-03-10 23:32 +0100
                Re: lambda functions in C Jerry Stuckle <jstucklex@attglobal.net> - 2017-03-11 12:26 -0500
                Re: lambda functions in C David Brown <david.brown@hesbynett.no> - 2017-03-11 17:17 +0100
                Re: lambda functions in C Malcolm McLean <malcolm.mclean5@btinternet.com> - 2017-03-11 10:26 -0800
                Re: lambda functions in C David Brown <david.brown@hesbynett.no> - 2017-03-11 19:48 +0100
                Re: lambda functions in C Malcolm McLean <malcolm.mclean5@btinternet.com> - 2017-03-11 12:02 -0800
                Re: lambda functions in C Ian Collins <ian-news@hotmail.com> - 2017-03-12 09:14 +1300
                Re: lambda functions in C "James R. Kuyper" <jameskuyper@verizon.net> - 2017-03-11 15:44 -0500
                Re: lambda functions in C Ian Collins <ian-news@hotmail.com> - 2017-03-12 09:51 +1300
                Re: lambda functions in C "James R. Kuyper" <jameskuyper@verizon.net> - 2017-03-11 16:11 -0500
                Re: lambda functions in C jacobnavia <jacob@jacob.remcomp.fr> - 2017-03-11 22:21 +0100
                Re: lambda functions in C jameskuyper@verizon.net - 2017-03-11 13:36 -0800
                Re: lambda functions in C jacobnavia <jacob@jacob.remcomp.fr> - 2017-03-11 23:48 +0100
                Re: lambda functions in C Ian Collins <ian-news@hotmail.com> - 2017-03-12 11:55 +1300
                Re: lambda functions in C "James R. Kuyper" <jameskuyper@verizon.net> - 2017-03-11 18:34 -0500
                Re: lambda functions in C Ian Collins <ian-news@hotmail.com> - 2017-03-12 15:23 +1300
                Re: lambda functions in C BartC <bc@freeuk.com> - 2017-03-12 11:42 +0000
                Re: lambda functions in C GOTHIER Nathan <nathan.gothier@gmail.com> - 2017-03-12 13:49 +0100
                Re: lambda functions in C "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2017-03-12 10:56 -0700
                Re: lambda functions in C jameskuyper@verizon.net - 2017-03-11 15:30 -0800
                Re: lambda functions in C jacobnavia <jacob@jacob.remcomp.fr> - 2017-03-12 00:37 +0100
                Re: lambda functions in C Keith Thompson <kst-u@mib.org> - 2017-03-11 17:23 -0800
                Re: lambda functions in C David Brown <david.brown@hesbynett.no> - 2017-03-12 14:32 +0100
                Re: lambda functions in C Gareth Owen <gwowen@gmail.com> - 2017-03-12 07:22 +0000
                Re: lambda functions in C Ian Collins <ian-news@hotmail.com> - 2017-03-12 11:10 +1300
                Re: lambda functions in C Ian Collins <ian-news@hotmail.com> - 2017-03-12 11:08 +1300
                Re: lambda functions in C GOTHIER Nathan <nathan.gothier@gmail.com> - 2017-03-12 00:17 +0100
                Re: lambda functions in C David Brown <david.brown@hesbynett.no> - 2017-03-12 14:14 +0100
                Re: lambda functions in C Jerry Stuckle <jstucklex@attglobal.net> - 2017-03-12 09:54 -0400
                Re: lambda functions in C GOTHIER Nathan <nathan.gothier@gmail.com> - 2017-03-12 15:10 +0100
                Re: lambda functions in C Malcolm McLean <malcolm.mclean5@btinternet.com> - 2017-03-12 08:08 -0700
                Re: lambda functions in C GOTHIER Nathan <nathan.gothier@gmail.com> - 2017-03-12 16:49 +0100
                Re: lambda functions in C Jerry Stuckle <jstucklex@attglobal.net> - 2017-03-12 14:28 -0400
                Re: lambda functions in C GOTHIER Nathan <nathan.gothier@gmail.com> - 2017-03-13 00:03 +0100
                Re: lambda functions in C Jerry Stuckle <jstucklex@attglobal.net> - 2017-03-12 19:15 -0400
                Re: lambda functions in C David Brown <david.brown@hesbynett.no> - 2017-03-12 18:18 +0100
                Re: lambda functions in C Malcolm McLean <malcolm.mclean5@btinternet.com> - 2017-03-12 10:42 -0700
                Re: lambda functions in C David Brown <david.brown@hesbynett.no> - 2017-03-12 20:53 +0100
                Re: lambda functions in C supercat@casperkitty.com - 2017-03-10 07:09 -0800
                Re: lambda functions in C supercat@casperkitty.com - 2017-03-10 08:56 -0800
                Re: lambda functions in C GOTHIER Nathan <nathan.gothier@gmail.com> - 2017-03-10 08:49 +0100
    Re: lambda functions in C Thiago Adams <thiago.adams@gmail.com> - 2017-03-09 04:12 -0800
  Re: lambda functions in C Alain Ketterlin <alain@universite-de-strasbourg.fr.invalid> - 2017-03-09 10:38 +0100
  Re: lambda functions in C Siri Cruise <chine.bleu@yahoo.com> - 2017-03-09 08:48 -0800
    Re: lambda functions in C Malcolm McLean <malcolm.mclean5@btinternet.com> - 2017-03-09 09:45 -0800
      Re: lambda functions in C Thiago Adams <thiago.adams@gmail.com> - 2017-03-09 09:51 -0800
    Re: lambda functions in C Thiago Adams <thiago.adams@gmail.com> - 2017-03-09 09:50 -0800
      Re: lambda functions in C Malcolm McLean <malcolm.mclean5@btinternet.com> - 2017-03-09 11:08 -0800
        Re: lambda functions in C Ian Collins <ian-news@hotmail.com> - 2017-03-10 08:22 +1300
          Re: lambda functions in C Malcolm McLean <malcolm.mclean5@btinternet.com> - 2017-03-09 12:12 -0800
        Re: lambda functions in C Thiago Adams <thiago.adams@gmail.com> - 2017-03-09 11:29 -0800
      Re: lambda functions in C Siri Cruise <chine.bleu@yahoo.com> - 2017-03-09 13:33 -0800
        Re: lambda functions in C Thiago Adams <thiago.adams@gmail.com> - 2017-03-09 13:42 -0800
          Re: lambda functions in C Ian Collins <ian-news@hotmail.com> - 2017-03-10 11:02 +1300
          Re: lambda functions in C Thiago Adams <thiago.adams@gmail.com> - 2017-03-09 14:30 -0800
          Re: lambda functions in C Siri Cruise <chine.bleu@yahoo.com> - 2017-03-09 15:07 -0800
  Re: lambda functions in C jacobnavia <jacob@jacob.remcomp.fr> - 2017-03-11 21:45 +0100
    Re: lambda functions in C Thiago Adams <thiago.adams@gmail.com> - 2017-03-13 06:13 -0700

csiph-web