DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Latest Articles - DZone

article thumbnail
Five Anti-Patterns in DevOps
The purpose of this article is to share experiences and give a small collaboration to the growth of our DevOps community.
May 11, 2020
by Marcelo Oliveira
· 13,309 Views · 2 Likes
article thumbnail
Enabling CI-CD and Generating MSI Installations
In this series, we will demonstrate about the capabilities of Azure DevOps for CI-CD in software management.
May 11, 2020
by Aritra Nag
· 13,620 Views · 2 Likes
article thumbnail
How to Build a Coronavirus Dashboard in Java
In this article, we discuss how to build a Coronavirus dashboard in Java with Spring Boot, Vaadin, and an open source REST service as a data source.
Updated May 11, 2020
by Alejandro Duarte DZone Core CORE
· 20,785 Views · 11 Likes
article thumbnail
Best Performance Practices for Hibernate 5 and Spring Boot 2 (Part 1)
Make sure you are practicing the best performance practices in your Spring Boot and Hibernate projects.
Updated May 11, 2020
by Anghel Leonard DZone Core CORE
· 353,141 Views · 114 Likes
article thumbnail
CI/CD Processes And Tools For AWS Elastic Beanstalk
Take a look at how some of these CI/CD tools, including CircleCI, AWS CodePipeline, and Jenkins, can work with AWS Elastic Beanstalk.
May 11, 2020
by Raja Sekhar Mandava
· 11,044 Views · 7 Likes
article thumbnail
How to Configure Entity Framework Caching
Let's see how to configure entity framework caching.
Updated May 8, 2020
by Iqbal Khan
· 60,079 Views · 7 Likes
article thumbnail
Storing and Aggregating Time Series Data With Elastic Search
In this article, see a tutorial on how to store and aggregate time series data with elastic Search.
May 8, 2020
by Naveen Yalla
· 14,185 Views · 5 Likes
article thumbnail
All About Lambda Functions in C++ (From C++11 to C++17)
Lambda functions are quite an intuitive concept of Modern C++ introduced in C++11, so there are already tons of articles on lambda function tutorials over the internet. But still, there are some untold things (like IIFE, types of lambda, etc.) left, which nobody talks about. Therefore, here I am to not only show you lambda function in C++, but we'll also cover how it works internally and other aspects of Lambda. The title of this article is a bit misleading. Because lambda doesn't always synthesize to function pointer. It's an expression (precisely unique closure). But I have kept it that way for simplicity. So from now on, I will use lambda function and expression interchangeably. What Is a Lambda Function? A lambda function is a short snippet of code that: Isn't worth naming (unnamed, anonymous, disposable, etc. Whatever you can call it), and also will not be reused. In other words, it's just syntactic sugar. lambda function syntax is defined as: C++ xxxxxxxxxx 1 1 [ capture list ] (parameters) -> return-type 2 { 3 method definition 4 } Usually, the compiler evaluates a return type of a lambda function itself. So we don't need to specify a trailing return type explicitly i.e. -> return-type. But, in some complex cases, the compiler unable to deduce the return type and we need to specify that. Why Use a Lambda Function? C++ includes many useful generic functions, like std::for_each, which can be handy. Unfortunately, they can also be quite cumbersome to use, particularly if the functor you would like to apply is unique to the particular function. Consider the following code for an example: C++ xxxxxxxxxx 1 14 1 struct print 2 { 3 void operator()(int element) 4 { 5 cout << element << endl; 6 } 7 }; 8 9 int main(void) 10 { 11 std::vector v = {1, 2, 3, 4, 5}; 12 std::for_each(v.begin(), v.end(), print()); 13 return 0; 14 } If you use print once, in that specific place, it seems overkill to be writing a whole class just to do something trivial and one-off. However, this kind of situation inline code would be more suitable and appropriate which can be achieved by lambda function as follows: C++ xxxxxxxxxx 1 1 std::for_each(v.begin(), v.end(), [](int element) { cout << element << endl; }); How Lambda Functions Work Internally? C++ xxxxxxxxxx 1 11 1 [&i] ( ) { std::cout << i; } 2 // is equivalent to 3 struct anonymous 4 { 5 int &m_i; 6 anonymous(int &i) : m_i(i) {} 7 inline auto operator()() const 8 { 9 std::cout << i; 10 } 11 }; The compiler generates unique closure as above for each lambda function. Finally, the secret's revealed. Capture list will become a constructor argument in the closure. If you capture argument as a value, then the corresponding type data member is created within the closure. Moreover, you can declare a variable/object in the lambda function argument, which will become an argument to call the operator i.e. operator(). Benefits of Using a Lambda Function Zero cost abstraction. Yes! You read it right. Lambda doesn't cost you performance, and it's as fast as a normal function. In addition, code becomes compact, structured, and expressive. Learning Lambda Expressions Capture by Reference/Value C++ xxxxxxxxxx 1 1 int main() 2 { 3 int x = 100, y = 200; 4 auto print = [&] { // Capturing object by reference 5 std::cout << __PRETTY_FUNCTION__ << " : " << x << " , " << y << std::endl; 6 }; 7 print(); 8 return 0; 9 } Output: C++ xxxxxxxxxx 1 1 main():: : 100 , 200 In the above example, I have mentioned & in the capture list. This captures variable x and y as a reference. Similarly, = denotes captured by value, which will create data member of the same type within the closure and copy assignment will take place. Note that the parameter list is optional; you can omit the empty parentheses if you do not pass arguments to the lambda expression. Lambda Capture List The following table shows different use cases for the same: [ ] ( ) { } no captures [=] ( ) { } captures everything by copy(not recommendded) [&] ( ) { } captures everything by reference(not recommendded) [x] ( ) { } captures x by copy [&x] ( ) { } captures x by reference [&, x] ( ) { } captures x by copy, everything else by reference [=, &x] ( ) { } captures x by reference, everything else by copy Passing Lambda as a Parameter C++ xxxxxxxxxx 1 21 1 template 2 void f(Functor functor) 3 { 4 std::cout << __PRETTY_FUNCTION__ << std::endl; 5 } 6 7 /* Or alternatively you can use this 8 void f(std::function functor) 9 { 10 std::cout << __PRETTY_FUNCTION__ << std::endl; 11 } 12 */ 13 14 int g() { static int i = 0; return i++; } 15 16 int main() 17 { 18 auto lambda_func = [i = 0]() mutable { return i++; }; 19 f(lambda_func); // Pass lambda 20 f(g); // Pass function 21 } Output: C++ xxxxxxxxxx 1 1 Function Type : void f(Functor) [with Functor = main()::] 2 Function Type : void f(Functor) [with Functor = int (*)(int)] You can also pass lambda functions as an argument to other functions just like a normal function, which I have coded above. If you noticed, here I have declared a variable i in the capture list, which will become a data member. As a result, every time you call lambda_func, it will be returned and incremented. Capture Member Variable in Lambda or This Pointer C++ xxxxxxxxxx 1 17 1 class Example 2 { 3 public: 4 Example() : m_var(10) {} 5 void func() 6 { 7 [=]() { std::cout << m_var << std::endl; }(); // IIFE 8 } 9 private: 10 int m_var; 11 }; 12 13 int main() 14 { 15 Example e; 16 e.func(); 17 } this pointer can also be captured using [this], [=] or [&]. In any of these cases, class data members(including private) can be accessed as you do in a normal method. If you see the lambda expression line, I have used extra () at the end of the lambda function declaration which used to calls it right thereafter declaration. It is called IIFE (Immediately Invoked Function Expression). C++ Lambda Function Types Generic Lambda C++ xxxxxxxxxx 1 1 const auto l = [](auto a, auto b, auto c) {}; 2 // is equivalent to 3 struct anonymous 4 { 5 template 6 auto operator()(T0 a, T1 b, T2 c) const 7 { 8 } 9 }; Generic lambda introduced in C++14 captures parameters with theauto specifier. Variadic Generic Lambda C++ xxxxxxxxxx 1 15 1 void print() {} 2 template 3 void print(const First &first, Rest &&... args) 4 { 5 std::cout << first << std::endl; 6 print(args...); 7 } 8 9 int main() 10 { 11 auto variadic_generic_lambda = [](auto... param) { 12 print(param...); 13 }; 14 variadic_generic_lambda(1, "lol", 1.1); 15 } Lambda with a variable parameter pack will be useful in many scenarios like debugging, repeated operation with different data input, etc. Mutable Lambda Function Typically, a lambda's function call operator is const-by-value, which means lambda requires the mutable keyword if you are capturing anything by-value. C++ xxxxxxxxxx 1 1 []() mutable {} 2 // is equivalent to 3 struct anonymous 4 { 5 auto operator()() // call operator 6 { 7 } 8 }; We have already seen an example of this above. I hope you noticed it. Lambda as a Function Pointer C++ xxxxxxxxxx 1 1 #include 2 #include 3 int main() 4 { 5 auto funcPtr = +[] {}; 6 static_assert(std::is_same::value); 7 } You can force the compiler to generate lambda as a function pointer rather than closure by adding + in front of it, as shown above. Higher-Order Returning Lambda Functions C++ xxxxxxxxxx 1 13 1 const auto less_than = [](auto x) { 2 return [x](auto y) { 3 return y < x; 4 }; 5 }; 6 7 int main(void) 8 { 9 auto less_than_five = less_than(5); 10 std::cout << less_than_five(3) << std::endl; 11 std::cout << less_than_five(10) << std::endl; 12 return 0; 13 } Going a bit further, lambda functions can also return another lambda function. This will open the doors of endless possibilities for customization, code expressiveness, and compactibility (by the way, there is no word like this) of code. Constexpr Lambda Expression Since C++17, a lambda expression can be declared as constexpr. C++ xxxxxxxxxx 1 13 1 constexpr auto sum = [](const auto &a, const auto &b) { return a + b; }; 2 /* 3 is equivalent to 4 constexpr struct anonymous 5 { 6 template 7 constexpr auto operator()(T1 a, T2 b) const 8 { 9 return a + b; 10 } 11 }; 12 */ 13 constexpr int answer = sum(10, 10); Even if you don't specify constexpr , the function call operator will be constexpr anyway, if it happens to satisfy all constexpr function requirements. Closing Words I hope you enjoyed this article. I have tried to cover most of the intricacies around lambda with a couple of simple and small examples. You should use lambda wherever it strikes in your mind considering code expressiveness and easy maintainability like you can use it in custom deleters for smart pointers and with most of the STL algorithms.
May 8, 2020
by Vishal Chovatiya
· 52,337 Views · 4 Likes
article thumbnail
Test-Case Reviews in Scrum-Teams
In this article, I will share a recent experience where a newly formed Scrum team has evolved and learned through its successes and mistakes.
May 8, 2020
by Stelios Manioudakis DZone Core CORE
· 30,807 Views · 3 Likes
article thumbnail
Mule 4 Integration With Kafka
In this article, we discuss how to go about integrating Apache Kafka with a sample Mule 4 application.
May 7, 2020
by Ankur Parashar
· 12,882 Views · 4 Likes
article thumbnail
Accumulator and Broadcast Variables in Spark
In this article, we discuss basics behind accumulators and broadcast variables in Spark, including how and when to use them in a program.
May 7, 2020
by Samik Bandopadhyay
· 31,444 Views · 4 Likes
article thumbnail
Introduction to Spring Data JPA - Part 6 Bidirectional One to One Relations
The next article in this series introduces you to bidirectional one-to-one relations in Spring Data.
May 7, 2020
by Vinu Sagar
· 13,522 Views · 3 Likes
article thumbnail
Unary Streaming via gRPC
In this article, we discuss how to implement Unary Streaming in gRPC in Spring with a simple messaging service.
May 7, 2020
by Munander Singh
· 12,862 Views · 4 Likes
article thumbnail
Use Of Ngx-Bootstrap Typehead In Angular 8
In this article, we will learn about the Typehead component which is a cool feature of Ngx-bootstrap.
May 7, 2020
by Siddharth Gajbhiye
· 14,426 Views · 3 Likes
article thumbnail
Download Blob to Azure VM Using Custom Script Extension via PowerShell
In this post, we share a simple Powershell script that can be used as a Custom Script Extension to copy and install files in an Azure VM post-creation.
May 7, 2020
by Mourya Chigurupati
· 9,200 Views · 1 Like
article thumbnail
Spring 5 Web Reactive: Flux, Mono, and JUnit Testing
In this article we discuss how to use Flux, Mono, and JUnit within Spring Webflux to sure up our reactive programs.
May 7, 2020
by Imaya Purushothaman
· 103,686 Views · 4 Likes
article thumbnail
The Ultimate Guide to Shift-left Testing
Take a look at this quick primer on the importance of shift-left testing and how you can implement it in your CI/CD pipeline.
May 6, 2020
by Oliver Howard
· 10,938 Views · 3 Likes
article thumbnail
Full-Duplex Scalable Client-Server Communication with WebSockets and Spring Boot (Part I)
In the first part of this tutorial, we take a look at how to configure a Java WebSocket that communicates with a microservices infrastructure.
Updated May 6, 2020
by Kyriakos Mandalas DZone Core CORE
· 35,400 Views · 23 Likes
article thumbnail
3 Ways to Reduce Latency in Multi-Region Deployments
In this article from CockroachDB, learn why global applications should have global deployment and how Follower reads can reduce latency.
May 6, 2020
by Andy Woods
· 14,834 Views · 3 Likes
article thumbnail
Anemic Domain Model in Typical Spring Projects (Part 1)
One developer talks about how design patterns hold up in the real world as they examine layered architecture and domains models.
Updated May 6, 2020
by Valerii Sloboda
· 16,096 Views · 7 Likes
  • Previous
  • ...
  • 952
  • 953
  • 954
  • 955
  • 956
  • 957
  • 958
  • 959
  • 960
  • 961
  • ...
  • Next
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook
×