Skip to content

Commit

Permalink
externalize lambda examples and simple tests (not unit tests)
Browse files Browse the repository at this point in the history
  • Loading branch information
George K. Thiruvathukal committed Oct 3, 2023
1 parent 9bd4782 commit 4026aee
Show file tree
Hide file tree
Showing 12 changed files with 116 additions and 40 deletions.
66 changes: 28 additions & 38 deletions modern-cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,38 +1,28 @@
add_executable(cli11-mva cli11-mva.cpp)
target_link_libraries(cli11-mva fmt::fmt spdlog::spdlog CLI11::CLI11)

add_executable(auto-mva auto-mva.cpp)
target_link_libraries(auto-mva fmt::fmt spdlog::spdlog CLI11::CLI11)

add_executable(auto-with-stl auto-with-stl.cpp)
target_link_libraries(auto-with-stl fmt::fmt spdlog::spdlog CLI11::CLI11)

#add_executable(const-constexpr const-constexpr.cpp)
#target_link_libraries(const-constexpr fmt::fmt spdlog::spdlog CLI11::CLI11)

add_executable(mc-triangle mc-triangle.cpp)
target_link_libraries(mc-triangle fmt::fmt spdlog::spdlog CLI11::CLI11)

add_executable(mc-triangle-threads mc-triangle-threads.cpp)
target_link_libraries(mc-triangle-threads fmt::fmt spdlog::spdlog CLI11::CLI11)

add_executable(move-mvp move-mvp.cpp)
target_link_libraries(move-mvp fmt::fmt spdlog::spdlog CLI11::CLI11)

add_executable(point_demo point_demo.cpp)
target_link_libraries(point_demo fmt::fmt spdlog::spdlog CLI11::CLI11)

add_executable(random-hist random-hist.cpp)
target_link_libraries(random-hist fmt::fmt spdlog::spdlog CLI11::CLI11)

add_executable(range1 range1.cpp)
target_link_libraries(range1 fmt::fmt spdlog::spdlog CLI11::CLI11)

add_executable(range2 range2.cpp)
target_link_libraries(range2 fmt::fmt spdlog::spdlog CLI11::CLI11)

#add_executable(sleep-coroutine sleep-coroutine.cpp)
#target_link_libraries(sleep-coroutine fmt::fmt spdlog::spdlog CLI11::CLI11)

add_executable(weak_ptr weak_ptr.cpp)
target_link_libraries(weak_ptr fmt::fmt spdlog::spdlog CLI11::CLI11)
function(add_my_executable target_name)
add_executable(${target_name} ${target_name}.cpp)
target_link_libraries(${target_name} fmt::fmt spdlog::spdlog CLI11::CLI11)
endfunction()

set(MY_APP_NAMES cli11-mva
auto-mva
auto-with-stl
mc-triangle
mc-triangle-threads
move-mvp
point-demo
random-hist
range1
range2
weak-ptr
lambda-no-parameters
lambda-with-parameters
lambda-with-closures-byref
lambda-with-closures-byref-all
lambda-with-closures-byval
lambda-with-closures-byval-all
lambda-with-return-type
)

foreach(APP_NAME IN LISTS MY_APP_NAMES)
add_my_executable(${APP_NAME})
endforeach()
1 change: 0 additions & 1 deletion modern-cpp/auto-mva2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ std::string format_container(const Container& container) {
return fmt::to_string(buffer);
}


int main() {
auto i{42};
auto d1{1.23}, d2{4.56};
Expand Down
10 changes: 10 additions & 0 deletions modern-cpp/lambda-no-parameters.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include <iostream>

int main() {
auto sayHello = []() {
std::cout << "Hello from lambda!" << std::endl;
};

sayHello();
return 0;
}
14 changes: 14 additions & 0 deletions modern-cpp/lambda-with-closures-byref-all.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

#include <iostream>

int main() {
int a = 5;
int b = 10;

auto sum = [=]() {
return a + b;
};

std::cout << "Sum" << sum() << std::endl;
return 0;
}
14 changes: 14 additions & 0 deletions modern-cpp/lambda-with-closures-byref.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include <iostream>

int main() {
int a = 5;
int b = 10;

auto incrementA = [&a]() {
a++;
};

incrementA();
std::cout << "a = " << a << std::endl; // 6
return 0;
}
15 changes: 15 additions & 0 deletions modern-cpp/lambda-with-closures-byval-all.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include <iostream>

int main() {
int a = 5;
int b = 10;

auto incrementBoth = [&]() {
a++;
b++;
};

incrementBoth();
std::cout << "Increment Both " << "a = " << a << " b = " << b << std::endl;
return 0;
}
13 changes: 13 additions & 0 deletions modern-cpp/lambda-with-closures-byval.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include <iostream>

int main() {
int a = 5;
int b = 10;

auto sum = [a, b]() {
return a + b;
};

std::cout << "Sum = " << sum() << std::endl; // 15
return 0;
}
11 changes: 11 additions & 0 deletions modern-cpp/lambda-with-parameters.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include <iostream>

int main() {
auto divide = [](double a, double b) -> double {
if (b == 0.0) return 0.0; // handle division by zero
return a / b;
};

std::cout << "8.0 / 2.0 = " << divide(8.0, 2.0) << std::endl;
return 0;
}
11 changes: 11 additions & 0 deletions modern-cpp/lambda-with-return-type.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include <iostream>

int main() {
auto divide = [](double a, double b) -> double {
if (b == 0.0) return 0.0; // handle division by zero
return a / b;
};

std::cout << "8.0 / 2.0 = " << divide(8.0, 2.0) << std::endl;
return 0;
}
File renamed without changes.
1 change: 0 additions & 1 deletion modern-cpp/random-hist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// Created by George K. Thiruvathukal on 3/19/23.
//


// Use modern C++ to randomly generate a million floating point values in the range 0 to 1 and bucket them in 0.1 increments?

#include <iostream>
Expand Down
File renamed without changes.

0 comments on commit 4026aee

Please sign in to comment.