1 of 32

CS 368 (C++): Lecture 5

Lambdas

Oct 12th, 2016

2 of 32

Lambdas in C++

3 of 32

std::transform()

std::transform(inV.begin(), inV.end(), outV.begin(), function);

4 of 32

A square function in C++

int square(int x) {

return x * x;

}

5 of 32

A square function in C++ along with main()

int main() {

}

6 of 32

A square function in C++ along with main()

int main() {

int y = square(3);

}

7 of 32

A square function in C++ along with main()

int square(int x);

int main() {

int y = square(3);

}

8 of 32

A square function in C++ along with main()

int square(int x); // function declaration

int main() {

int y = square(3);

}

9 of 32

A square function in C++ along with main()

int square(int x); // function declaration

int main() {

int y = square(3);

}

int square(int x) {

return x * x;

}

10 of 32

A square function in C++ along with main()

int square(int x); // function declaration

int main() {

int y = square(3);

}

int square(int x) { // function definition

return x * x;

}

11 of 32

std::transform()

7

3

2

9

4

inV:

12 of 32

std::transform()

std::transform(inV.begin(), inV.end(), outV.begin(), square);

7

3

2

9

4

inV:

13 of 32

std::transform()

std::transform(inV.begin(), inV.end(), outV.begin(), square);

0

0

0

0

0

7

3

2

9

4

inV:

outV:

14 of 32

std::transform()

std::transform(inV.begin(), inV.end(), outV.begin(), square);

49

0

0

0

0

7

3

2

9

4

inV:

outV:

15 of 32

std::transform()

std::transform(inV.begin(), inV.end(), outV.begin(), square);

49

9

0

0

0

7

3

2

9

4

inV:

outV:

16 of 32

std::transform()

std::transform(inV.begin(), inV.end(), outV.begin(), square);

49

9

4

0

0

7

3

2

9

4

inV:

outV:

17 of 32

std::transform()

std::transform(inV.begin(), inV.end(), outV.begin(), square);

49

9

4

81

0

7

3

2

9

4

inV:

outV:

18 of 32

std::transform()

std::transform(inV.begin(), inV.end(), outV.begin(), square);

49

9

4

81

16

7

3

2

9

4

inV:

outV:

19 of 32

𝛌 Calculus

20 of 32

Lambda Calculus

A function to square a number:

square(x) = x * x

In Lambda Calculus:

x → x * x

21 of 32

A square function in C++

int square(int x) {

return x * x;

}

22 of 32

A square function in C++ using lambdas!

int square(int x) {

return x * x;

}

23 of 32

A square function in C++ using lambdas!

int square(int x) {

return x * x;

}

24 of 32

A square function in C++ using lambdas!

int (int x) {

return x * x;

}

25 of 32

A square function in C++ using lambdas!

int (int x) {

return x * x;

}

26 of 32

A square function in C++ using lambdas!

(int x) {

return x * x;

}

27 of 32

A square function in C++ using lambdas!

[] (int x) {

return x * x;

}

28 of 32

A square function in C++ using lambdas!

[] (int x) { return x * x; }

29 of 32

std::transform() using a lambda function

auto lambda = [] (int x) { return x * x; }

30 of 32

std::transform() using a lambda function

auto lambda = [] (int x) { return x * x; }

std::transform(inV.begin(), inV.end(), outV.begin(), lambda);

31 of 32

std::transform() using a lambda function

std::transform(inV.begin(), inV.end(), outV.begin(),� [] (int x) { return x * x; });

32 of 32

Demo: Lambdas (λ)