Run Code  | API  | Code Wall  | Misc  | Feedback  | Login  | Theme  | Privacy  | Patreon 

C++11 Lambda Function Demonstration

Language: Layout:
+ ] Compiler args + ] Show input
Compilation time: 2.65 sec, absolute running time: 0.44 sec, cpu time: 0.3 sec, memory peak: 3 Mb, absolute service time: 3,1 sec 
fork mode |  history
-----------------------------------------------------------------------------------------
>> Example(1) - Testing function lambda1
Lambda1: : I got the value = Hello
Lambda1: : I got the value = World

-----------------------------------------------------------------------------------------
>> Example(2) Testing function lambda2
Lambda 2 :: I got the value  = Japan
Lambda 2 :: I got the value  = Korea

-----------------------------------------------------------------------------------------
>> Example(3) Testing function lambda3
   - Lambda function can be defined and executed at the call-site
Lambda 3 :: I got the value = C++11 is awesome!

-----------------------------------------------------------------------------------------
>> Example(4) Testing function sumLambda
   - Lambda functions  can return values as any function.
sumLambda(10.0, 25.34) = 35.34
sumLambda(-10.23, 4.56) = -5.67

-----------------------------------------------------------------------------------------
>> Example(5) Testing function lamdaCapture
   - Lambda functions  can capture its environment (closures) and have state as "function objects"
  x  = 5  c = 3
lamdaCapture(2, 5)  = 32
  x  = 5  c = 4
  lamdaCapture(1, 2) =  16
  x  = 5  c = 5

-----------------------------------------------------------------------------------------
>> Example(6) Testing function sumLambda
   - Lambda functions  can play well with STL algorithms
Print all vector elements - Version 1
  v[0] = 1
  v[1] = 2
  v[2] = 3
  v[3] = 4
  v[4] = 5
  v[5] = 6
  v[6] = 7
  v[7] = 8
Print all vector element Version 2  - local state with 'static' keyword
  v[0] = 1
  v[1] = 2
  v[2] = 3
  v[3] = 4
  v[4] = 5
  v[5] = 6
  v[6] = 7
  v[7] = 8

-----------------------------------------------------------------------------------------
>> Example(7) 
   Playing with STL transform algorithm.
  Vector transformed =  
v  [0] = 4
v  [1] = 7
v  [2] = 10
v  [3] = 13
v  [4] = 16
v  [5] = 19
v  [6] = 22
v  [7] = 25

-----------------------------------------------------------------------------------------
>> Example(8) 
   Lambda functions can be returned from functions and be generated at run-time.
  mulSumBy2(2, 4) = 12
  mulSumBy2(3, 1) = 8
  mulSumBy5(2, 4) = 30
  mulSumBy5(9, 2) = 55

-----------------------------------------------------------------------------------------
>> Example(9) 
   Lambda functions can be returned from functions and have state.
  Running dummy function
    (*) m = 2, n = -643994744, x = 1, y = 3, z = -643994736
  Running dummy function
    (*) m = 2, n = -643994743, x = 2, y = 5, z = -643994729
  Running dummy function
    (*) m = 3, n = 32764, x = 1, y = 3, z = 32776
  Running dummy function
    (*) m = 3, n = 32765, x = 2, y = 5, z = 32786

-----------------------------------------------------------------------------------------
>> Example(10) 
   Lambda functions can return lambda functions!!
  addTo5(4) = 9
  addTo5(3) = 8
  addTo10(4) = 14
  addTo10(3) = 13

-----------------------------------------------------------------------------------------
>> Example(11) 
   Lambda functions can be passed as function arguments!
foldVector(dataset2, 0, add) = 28
foldVector(dataset2, 1, mul) = 5040

-----------------------------------------------------------------------------------------
>> Example(12) - Observer pattern
(observer 1) Temperature changed to 30.5 C
(observer 2) Sensor temperature changed to 30.5 C
(observer 1) Temperature changed to 20.5 C
(observer 2) Sensor temperature changed to 20.5 C

    
                
λ
.NET NoSQL database for rapid development