LearnFrenzy
Sign in Take a free test

Lambda Expressions · Exercise 4 of 4

Lambda Expressions Lambda Programs

  • 10 questions
  • Page 1 of 1
  • Answers explained
0Attempted
0Correct
0Wrong
Directions

C++ programs that use lambdas for callbacks, algorithms, state machines and functional pipelines. Write each one yourself first, then open the solution – every solution was compiled and run, and the output shown is the real output.

Question 1 Easy

Build a calculator that stores operations (+, -, *, /, %, ^) as lambdas in a std::map<char, std::function<double(double, double)>> and evaluates a list of expressions, reporting unknown operators.

Question page & discussion
Question 2 Hard

Write a recursive Fibonacci lambda (C++23 deducing this) that caches results in a captured std::unordered_map, and count how many real computations it performs for fib(40).

Question page & discussion
Question 3 Medium

Validate sign-up data with a vector of {message, lambda} rules (name length, email has @, age range, password strength) and print every failed rule for each user.

Question page & discussion
Question 4 Medium

Sort a list of employees three ways using lambdas: by department then salary (descending), by name length, and with a lambda chosen at run time from a map of comparators.

Question page & discussion
Question 5 Medium

Write a function template retry(attempts, op, on_fail) that calls a lambda until it succeeds or attempts run out, calling another lambda on each failure; simulate a flaky network call.

Question page & discussion
Question 6 Hard

Write a ScopeGuard class template that stores a lambda and calls it in its destructor unless dismissed; use it to roll back a two-step transfer when the second step fails.

Question page & discussion
Question 7 Hard

Write a variadic pipe(f1, f2, …) that returns a lambda applying the functions left to right, and use it to build a text-cleaning pipeline and a number pipeline.

Question page & discussion
Question 8 Medium

Write make_limiter(max, window) returning a mutable lambda that, given a timestamp, returns true if the call is allowed (at most max calls in any window starting at the first call of that window).

Question page & discussion
Question 9 Easy

Write a Button class that stores an on_click std::function; create several buttons whose lambdas update a shared cart total and item count, then simulate clicks.

Question page & discussion
Question 10 Medium

Write a function template group_by(items, key_fn) that returns a std::map from key to vector of items, and use it with lambdas to group words by first letter and numbers by remainder.

Question page & discussion
All exercises doneBack to Lambda ExpressionsRevise the concepts or pick another topic

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links