News for hackers. Stuff that matters.
Re:C++14 lambda tutorial by tdsamardzhiev on Thursday August 28, 2014 @11:10AM (#8237558)
I work as a C++ developer, but damn, C++ has the ugliest lambda syntax ever.
Re:C++14 lambda tutorial by claudius on Thursday August 28, 2014 @11:56AM (#8237847)
Do you have an alternative suggestion? It seems reasonably straightforward to me and I don’t find it all that impossible. Not having to muck around with a specific lambda() factory-like function with horrible inputs (as is needed in LPC where I sort-of learnt to code) is a plus :)
Re:C++14 lambda tutorial by repsilat on Thursday August 28, 2014 @12:40PM (#8238177)
I don't mind C++'s lambdas, but in some other languages they use "arrows", like

   square = (x) -> x * x
C++ won't want to give up the different kinds of capturing, though, so it's hard to see anything much better than what's available now.

In imaginary, never-to-be-implemented language daydreams I toy with the idea of using curly braces and implicit positional arguments to have lambdas look like

    square = { _1 * _1 }
and

    factorial = { (_1 < 2) ? 1 : _0(_1 - 1) }
and with explicit captures and arguments,

    partial_sort = [&lt_vec, &ge_vec, pivot](item){
        if (item < pivot) lt_vec.add(item)
        else ge_vec.add(item)
    }
I suspect, though, those ideas are just horrible shortcuts to a Perl-like hell :-)
Re:C++14 lambda tutorial by claudius on Thursday August 28, 2014 @01:11PM (#8238411)
Hrm, I don’t quite like the arrows. C++ lambdas can contain arbitrary code and these arrows always remind me of “pure” functions (in a sense), or at least functions whose main purpose it is to map input arguments to output arguments.

For example, consider something like this relatively common block to do some complex operation on the elements of a vector using some (naive) threading (it might be nicer to first create some lambdas, push them into a queue and then spawn threads to work on that queue, but the syntax is essentially the same):

  for(std::size_t i(0); i != somevec.size(); ++i) {
    workers.push_back(std::thread([&somevec, i]() mutable { calc(somevec, i); }));
  }
That is, we want an anonymous lambda which captures some elements of its environment (by both reference and value, also it’s essentially a closure by now) and call some function on these elements. That lambda doesn’t have any return value and doesn’t take any input arguments. Sure one could throw in an extra lambda somewhere or put an arrow between mutable and {, but I have to admit I don’t see the value.
Re:C++14 lambda tutorial by archgrove on Thursday August 28, 2014 @01:11PM (#8238418)
You might like Apple's Swift:

    class Example {
        let ExtraMul = 5
    }

    let weakly = Example()
    let squareAndABit : Int -> Int = { [weak weakly] in $0 * $0 * weakly!.ExtraMul }
    squareAndABit(2) // 20
Positional lambda arguments, implicit returns for single expression arguments, capture lists if you want to alter the default capture mode. It's got really rather nice first class function support.
Re:C++14 lambda tutorial by fit2rule on Saturday August 30, 2014 @06:52AM (#8246117)
""In imaginary, never-to-be-implemented language daydreams I toy..""

Your mockup code sure looks like Lua.

Re:C++14 lambda tutorial by dllthomas on Thursday August 28, 2014 @11:37AM (#8237720)
Not as ugly as having to create a new class, IMO.
Re:C++14 lambda tutorial by r-s on Thursday August 28, 2014 @12:27PM (#8238075)
Much better than writing a functor, a class which overloads ().