diff --git a/notebooks/08_Functors+Lambdas.ipynb b/notebooks/08_Functors+Lambdas.ipynb index 9b38def464e0659c75086e543e7b1f4976315afb..01e92b0c869de2c2d7b0e0633d8f52ec81d76753 100644 --- a/notebooks/08_Functors+Lambdas.ipynb +++ b/notebooks/08_Functors+Lambdas.ipynb @@ -326,7 +326,7 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 1, "id": "21120bbb-03a7-4048-97f4-0426c40c5bac", "metadata": {}, "outputs": [], @@ -337,7 +337,7 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 2, "id": "bab0ab95-e5fd-4368-b3c2-77bcb869cfe0", "metadata": {}, "outputs": [], @@ -353,7 +353,7 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 3, "id": "b4e3f68a-6c70-49e7-a04c-924804c4cb35", "metadata": {}, "outputs": [ @@ -374,7 +374,7 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": 5, "id": "c5b3e4d0-64b4-47c6-848f-f787bf9c0b17", "metadata": {}, "outputs": [ @@ -405,7 +405,7 @@ }, { "cell_type": "code", - "execution_count": 20, + "execution_count": 6, "id": "ff8d558b-da83-46d9-9842-8e0d9d5ba56d", "metadata": {}, "outputs": [], @@ -418,7 +418,7 @@ }, { "cell_type": "code", - "execution_count": 21, + "execution_count": 7, "id": "b5790b96-bf62-4d6e-ad9c-e56839882728", "metadata": {}, "outputs": [ @@ -428,7 +428,7 @@ "6" ] }, - "execution_count": 21, + "execution_count": 7, "metadata": {}, "output_type": "execute_result" } @@ -456,7 +456,7 @@ }, { "cell_type": "code", - "execution_count": 22, + "execution_count": 9, "id": "646b302f-cdf5-40ed-98f8-01d98a9b277a", "metadata": {}, "outputs": [], @@ -473,7 +473,7 @@ }, { "cell_type": "code", - "execution_count": 26, + "execution_count": 10, "id": "b56b370a-654f-4665-a86d-1b4a1497a813", "metadata": {}, "outputs": [], @@ -483,7 +483,7 @@ }, { "cell_type": "code", - "execution_count": 27, + "execution_count": 11, "id": "b267e62f", "metadata": {}, "outputs": [ @@ -523,7 +523,7 @@ }, { "cell_type": "code", - "execution_count": 43, + "execution_count": 13, "id": "24ad0ef1", "metadata": {}, "outputs": [ @@ -552,7 +552,7 @@ "id": "ff3d112c", "metadata": {}, "source": [ - "The expression **`[]( int& n ) { n *= n; }`** is an *anonymous*, so called, lambda, lambda expression, lambda function or closure.\n", + "The expression **`[]( int& n ) { n *= n; }`** is an *anonymous function*, so called, lambda, lambda expression, lambda function or closure.\n", "\n", "- The `(...)` part specifies the interface.\n", "- If a lambda function does not return a value we can omit the return type.\n", @@ -591,7 +591,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 14, "id": "3514f84c", "metadata": {}, "outputs": [], @@ -618,7 +618,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 15, "id": "7bca56ac", "metadata": {}, "outputs": [ @@ -643,7 +643,7 @@ "source": [ "In line #10 we have our lambda expression.\n", "- It does not receive arguments through its interface. Thus, we can omit the `( )` part.\n", - "- When we want to re-use a lambda we can store it in a variable. For this we need to use `auto`, asSmalleSmalle.\n", + "- When we want to re-use a lambda we can store it in a variable. For this we need to use `auto`.\n", "- Note the difference in the output between the two invocations of `lambda()`. As we captured `copy` by copy its value inside `lambda()` is not affected by its change in the surrounding scope, while that of `ref` is. The latter was captured by reference." ] }, @@ -658,7 +658,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 34, "id": "f0fd0543", "metadata": {}, "outputs": [ @@ -668,7 +668,7 @@ "text": [ "5.0 is of type d\n", "type of a is i\n", - "What is lambda? ZN11__cling_N5415__cling_Un1Qu33EPvE3$_1\n" + "What is lambda? ZN12__cling_N52216__cling_Un1Qu318EPvE3$_6\n" ] } ], @@ -692,7 +692,7 @@ }, { "cell_type": "code", - "execution_count": 61, + "execution_count": 18, "id": "a32b1635", "metadata": {}, "outputs": [ @@ -700,7 +700,7 @@ "name": "stdout", "output_type": "stream", "text": [ - "Overwriting lambda.cpp\n" + "Writing lambda.cpp\n" ] } ], @@ -718,7 +718,7 @@ }, { "cell_type": "code", - "execution_count": 62, + "execution_count": 19, "id": "85373b9f", "metadata": {}, "outputs": [], @@ -728,7 +728,7 @@ }, { "cell_type": "code", - "execution_count": 63, + "execution_count": 20, "id": "fa038d35", "metadata": {}, "outputs": [ @@ -766,7 +766,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 21, "id": "03a872a9", "metadata": {}, "outputs": [ @@ -774,7 +774,7 @@ "name": "stdout", "output_type": "stream", "text": [ - "Overwriting join.cpp\n" + "Writing join.cpp\n" ] } ], @@ -791,7 +791,7 @@ " std::string joinStr = \"\";\n", "\n", " // small mod from original\n", - " // if( not str.size()() ) return joinStr;\n", + " // if( not str.size() ) return joinStr;\n", " if( str.size() == 0 ) return joinStr;\n", "\n", " std::for_each( str.begin(), str.end()-1,\n", @@ -827,7 +827,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 22, "id": "933de670", "metadata": {}, "outputs": [], @@ -837,7 +837,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 23, "id": "c485b43f", "metadata": {}, "outputs": [ @@ -869,7 +869,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 35, "id": "dcaec377", "metadata": {}, "outputs": [ @@ -910,7 +910,7 @@ " const int NUM = 60;\n", "\n", " std::random_device seed;\n", - " std::mt19937 engine( seed() ); // \n", + " std::mt19937 engine( seed() ); // a mersenne twister\n", "\n", " // distribution\n", " std::uniform_int_distribution< int > six(1,6);\n", @@ -939,7 +939,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 25, "id": "b45b0e4f", "metadata": {}, "outputs": [], @@ -949,7 +949,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 28, "id": "f85b1bc8", "metadata": {}, "outputs": [ @@ -961,9 +961,9 @@ "4 inRange(5,10): false\n", "5 inRange(5,10): true\n", "\n", - "13 of 60 inRange(6,6)\n", - "38 of 60 inRange(4,6)\n", - "All numbers 5 and 6: 5 5 6 6 5 6 6 5 5 6 6 5 6 6 5 5 5 6 5 6 6 6 6 5 5 \n", + "7 of 60 inRange(6,6)\n", + "30 of 60 inRange(4,6)\n", + "All numbers 5 and 6: 5 5 6 5 6 5 6 5 6 5 5 6 6 6 5 5 \n", "\n" ] } @@ -980,12 +980,14 @@ "***\n", "We had seen that we can store a lambda expression as an object (of some implicit type). However, we can also convert it into a\n", "- function pointer\n", - "- std::function" + "- std::function\n", + "\n", + "The last one is what we implicitely did in the filter (generator function) `inRange()` in the previous example." ] }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 30, "id": "cf33e115", "metadata": {}, "outputs": [ @@ -1036,7 +1038,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 31, "id": "3fbad575", "metadata": {}, "outputs": [], @@ -1046,7 +1048,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 32, "id": "0584e221", "metadata": {}, "outputs": [ @@ -1079,7 +1081,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 33, "id": "ae57e2ba", "metadata": {}, "outputs": [ @@ -1089,7 +1091,7 @@ "10.000000" ] }, - "execution_count": 7, + "execution_count": 33, "metadata": {}, "output_type": "execute_result" } @@ -1105,7 +1107,7 @@ "metadata": {}, "source": [ "***\n", - "In HyTeG we often make use of lambdas. Especially for initialising functions and setting boundary conditions. Like in this snippet: \n", + "In <a href=\"https://i10git.cs.fau.de/hyteg/hyteg\">HyTeG</a> we often make use of lambdas. Especially for initialising functions and setting boundary conditions. Like in this snippet: \n", "```c++\n", "real_t k = 2.0;\n", "real_t m = 5.0;\n", @@ -1121,7 +1123,7 @@ { "cell_type": "code", "execution_count": null, - "id": "328f2a8e", + "id": "f5d8241e", "metadata": {}, "outputs": [], "source": []