From af371a4ae00bad79917e331256315b700f915f0a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jaakko=20J=C3=A4rvi?=
Table of Contents
Table of Contents
-BLL defines several functions to create lambda functors for control expressions. -They all take lambda functors as parameters and return void. -
+for_each(a.begin(), a.end(), + if_then(_1 % 2 == 0, cout << _1)); ++
+The BLL supports the following function templates for control structures: + +
+if_then(condition, then_part) +if_then_else(condition, then_part, else_part) +while_loop(condition, body) +while_loop(condition) // no body case +do_while_loop(condition, body) +do_while_loop(condition) // no body case +for_loop(init, condition, increment, body) +for_loop(init, condition, increment) // no body case +switch_statement(...) ++
+Delayed variables tend to be commonplace in control lambda expressions. +For instance, here we use the var function to turn the arguments of for_loop into lambda expressions. +The effect of the code is to add 1 to each element of a two-dimensional array: + +
+int a[5][10]; int i; +for_each(a, a+5, + for_loop(var(i)=0, var(i)<10, ++var(i), + _1[var(i)] += 1)); ++ +As explained in Section 5.6, we can avoid the repeated use of wrapping of var if we define it beforehand: + +
+int i; +var_type<int>::type vi(var(i)); +for_each(a, a+5, + for_loop(vi=0, vi<10, ++vi, _1[vi] += 6)); ++ +
+The lambda expressions for switch control structures are more complex since the number of cases may vary. +The general form of a switch lambda expression is: + +
+switch_statement(condition, + case_statement<label>(lambda expression), + case_statement<label>(lambda expression), + ... + default_statement(lambda expression) +) ++ +The condition argument must be a lambda expression that creates a lambda functor with an integral return type. +The different cases are created with the case_statement functions, and the optional default case with the default_statement function. +The case labels are given as explicitly specified template arguments to case_statement functions and +break statements are implicitly part of each case. +For example, case_statement<1>(a), where a is some lambda functor, generates the code: + +
case 1: + evaluate lambda functor a; + break; ++We have specialized the switch_statement function for up to 9 case statements. + +