Commit 63498f8e authored by CHAMONT David's avatar CHAMONT David
Browse files

Update after 3-CollectiveDay

parent 94b4e02f
......@@ -227,7 +227,7 @@
}
},
"source": [
"Below, the implementation of `advance()` is optimized for random-access iterators. Why would the compilation fail without `constexpr` ?"
"Below, the implementation of `my_advance()` is optimized for random-access iterators. Why would the compilation fail without `constexpr`, if I use `advance()` with a `std::list`iterator ?"
]
},
{
......@@ -243,7 +243,7 @@
"#include <iterator>\n",
"\n",
"template <typename Iterator, typename Dist>\n",
"void advance( Iterator & i, Dist n )\n",
"void my_advance( Iterator & i, Dist n )\n",
" {\n",
" using cat = typename std::iterator_traits<Iterator>::iterator_category ;\n",
" if constexpr (std::is_same_v<cat,std::random_access_iterator_tag>)\n",
......
......@@ -67,9 +67,9 @@
],
"source": [
"const int MAX = 5 ;\n",
"double values[MAX] = { 1.1, 2.2, 3.3, 4.4, 5.5 } ;\n",
"double accumulator = 0.0 ;\n",
"for ( int indice = 0 ; indice<MAX ; ++indice )\n",
"double values[MAX] { 1.1, 2.2, 3.3, 4.4, 5.5 } ;\n",
"double accumulator {0.} ;\n",
"for ( int indice {0} ; indice<MAX ; ++indice )\n",
" { accumulator += values[indice] ; }\n",
"std::cout << accumulator << std::endl ;"
]
......@@ -103,8 +103,8 @@
}
],
"source": [
"double values[] = { 1.1, 2.2, 3.3, 4.4, 5.5 } ;\n",
"double accumulator = 0.0 ;\n",
"double values[] { 1.1, 2.2, 3.3, 4.4, 5.5 } ;\n",
"double accumulator {0.} ;\n",
"for ( double value : values )\n",
" { accumulator += value ; }\n",
"std::cout << accumulator << std::endl ;"
......@@ -150,10 +150,10 @@
}
],
"source": [
"double values[] = { 1.1, 2.2, 3.3, 4.4, 5.5 } ;\n",
"double values[] { 1.1, 2.2, 3.3, 4.4, 5.5 } ;\n",
"for ( double & value : values )\n",
" { value *= 2 ; }\n",
"double accumulator = 0.0 ;\n",
"double accumulator {0.} ;\n",
"for ( double value : values )\n",
" { accumulator += value ; }\n",
"std::cout << accumulator << std::endl ;"
......@@ -188,10 +188,10 @@
}
],
"source": [
"double values[] = { 1.1, 2.2, 3.3, 4.4, 5.5 } ;\n",
"double values[] { 1.1, 2.2, 3.3, 4.4, 5.5 } ;\n",
"for ( auto & value : values )\n",
" { value *= 2 ; }\n",
"double accumulator = 0.0 ;\n",
"double accumulator {0.} ;\n",
"for ( auto value : values )\n",
" { accumulator += value ; }\n",
"std::cout << accumulator << std::endl ;"
......@@ -434,7 +434,7 @@
"\n",
"int main()\n",
" {\n",
" IndexedVector values = { 1.1, 2.2, 3.3, 4.4, 5.5 } ;\n",
" IndexedVector values { 1.1, 2.2, 3.3, 4.4, 5.5 } ;\n",
" for ( auto [ index, value ] : values )\n",
" { std::cout<<index<<\" \"<<value<<std::endl ; }\n",
" return 0 ;\n",
......
......@@ -67,9 +67,9 @@
],
"source": [
"const int MAX = 5 ;\n",
"double values[MAX] = { 1.1, 2.2, 3.3, 4.4, 5.5 } ;\n",
"double accumulator = 0.0 ;\n",
"for ( int indice = 0 ; indice<MAX ; ++indice )\n",
"double values[MAX] { 1.1, 2.2, 3.3, 4.4, 5.5 } ;\n",
"double accumulator ={0.} ;\n",
"for ( int indice {0} ; indice<MAX ; ++indice )\n",
" { accumulator += values[indice] ; }\n",
"std::cout << accumulator << std::endl ;"
]
......@@ -95,8 +95,8 @@
},
"outputs": [],
"source": [
"double values[] = { 1.1, 2.2, 3.3, 4.4, 5.5 } ;\n",
"double accumulator = 0.0 ;\n",
"double values[] { 1.1, 2.2, 3.3, 4.4, 5.5 } ;\n",
"double accumulator {0.} ;\n",
"for ( double value : values )\n",
" { accumulator += value ; }\n",
"std::cout << accumulator << std::endl ;"
......@@ -142,10 +142,10 @@
}
],
"source": [
"double values[] = { 1.1, 2.2, 3.3, 4.4, 5.5 } ;\n",
"double values[] { 1.1, 2.2, 3.3, 4.4, 5.5 } ;\n",
"for ( double & value : values )\n",
" { value *= 2 ; }\n",
"double accumulator = 0.0 ;\n",
"double accumulator {0.} ;\n",
"for ( double value : values )\n",
" { accumulator += value ; }\n",
"std::cout << accumulator << std::endl ;"
......@@ -180,10 +180,10 @@
}
],
"source": [
"double values[] = { 1.1, 2.2, 3.3, 4.4, 5.5 } ;\n",
"double values[] { 1.1, 2.2, 3.3, 4.4, 5.5 } ;\n",
"for ( auto & value : values )\n",
" { value *= 2 ; }\n",
"double accumulator = 0.0 ;\n",
"double accumulator {0.} ;\n",
"for ( auto value : values )\n",
" { accumulator += value ; }\n",
"std::cout << accumulator << std::endl ;"
......@@ -385,7 +385,7 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 1,
"metadata": {
"slideshow": {
"slide_type": "fragment"
......@@ -396,7 +396,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Overwriting 1-Sugar.cpp\n"
"Writing tmp.functional-sugar.cpp\n"
]
}
],
......@@ -426,7 +426,7 @@
"\n",
"int main()\n",
" {\n",
" IndexedVector values = { 1.1, 2.2, 3.3, 4.4, 5.5 } ;\n",
" IndexedVector values { 1.1, 2.2, 3.3, 4.4, 5.5 } ;\n",
" for ( auto [ index, value ] : values )\n",
" { std::cout<<index<<\" \"<<value<<std::endl ; }\n",
" return 0 ;\n",
......
......@@ -19,7 +19,7 @@ class my_not_null
public :
my_not_null( T p ) : m_p{p} { assert(p) ; }
my_not_null & operator=( T p ) { assert(p) ; m_p = p ; return *this ; }
T operator->() { returnm_p ; }
T operator->() { return m_p ; }
private :
T m_p ;
} ;
......
......@@ -198,7 +198,7 @@
"\n",
" std::cout << \"5th value = \" << array_double.at(4) << std::endl ;\n",
" array_double.at(10) = 1.1 ; // Exception std::out_of_range\n",
" }"
" } "
]
},
{
......@@ -823,7 +823,7 @@
}
},
"source": [
"* Better than a `const std::vector<T>&`, a `gsl::span<T>` argument can be copied at no extra cost, and can stay on top of any \"continuous memory\" container."
"* Better than a `const std::vector<T>&`, a `gsl::span<T>` argument can be copied at no extra cost, and can stay on top of any \"continuous memory\" container. "
]
},
{
......@@ -902,8 +902,8 @@
"source": [
"## Exercise\n",
"\n",
"How about writing your own `my_span<T>`?\n",
"1. Provide a constructor from a `std::vector<T>`, add function `size()` and `operator[]`, which should make the code compilable.\n",
"How about writing your own `my_span<T>` ?\n",
"1. Provide a `my_span<T>` constructor taking a `std::vector<T>` as input, add function `size()` and `operator[]`, which should make the code compilable.\n",
"2. Try to turn the constructor into a template, assuming that the object passed to the constructor has member functions `size()` and `operator[]`, check that the code also works if you substitute the `std::vector` with a `std::array`."
]
},
......
......@@ -85,7 +85,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"metadata": {
"attributes": {
"classes": [
......@@ -97,7 +97,15 @@
"slide_type": "subslide"
}
},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Writing tmp.vector.h\n"
]
}
],
"source": [
"%%file tmp.vector.h\n",
"\n",
......@@ -112,7 +120,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 2,
"metadata": {
"attributes": {
"classes": [
......@@ -124,7 +132,15 @@
"slide_type": "subslide"
}
},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Writing tmp.vector-basic-operators.h\n"
]
}
],
"source": [
"%%file tmp.vector-basic-operators.h\n",
"\n",
......@@ -137,7 +153,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 3,
"metadata": {
"attributes": {
"classes": [
......@@ -149,7 +165,15 @@
"slide_type": "subslide"
}
},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Writing tmp.expressions-templates.cpp\n"
]
}
],
"source": [
"%%file tmp.expressions-templates.cpp\n",
"\n",
......@@ -167,7 +191,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 4,
"metadata": {
"attributes": {
"classes": [
......@@ -186,13 +210,21 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 5,
"metadata": {
"slideshow": {
"slide_type": "-"
}
},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1.09|2.18|3.27\n"
]
}
],
"source": [
"!./tmp.expressions-templates.exe"
]
......@@ -223,7 +255,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 8,
"metadata": {
"attributes": {
"classes": [
......@@ -235,7 +267,15 @@
"slide_type": "slide"
}
},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Writing tmp.iterators.h\n"
]
}
],
"source": [
"%%file tmp.iterators.h\n",
"\n",
......@@ -271,13 +311,21 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 9,
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Overwriting tmp.expressions-templates.cpp\n"
]
}
],
"source": [
"%%file tmp.expressions-templates.cpp\n",
"\n",
......@@ -300,7 +348,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 10,
"metadata": {
"slideshow": {
"slide_type": "-"
......@@ -313,13 +361,21 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 11,
"metadata": {
"slideshow": {
"slide_type": "-"
}
},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1.09 2.18 3.27 "
]
}
],
"source": [
"!./tmp.expressions-templates.exe"
]
......@@ -339,13 +395,21 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 19,
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Overwriting tmp.vector-add-iterators.h\n"
]
}
],
"source": [
"%%file tmp.vector-add-iterators.h\n",
"\n",
......@@ -367,13 +431,21 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 20,
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Overwriting tmp.vector-sub-iterators.h\n"
]
}
],
"source": [
"%%file tmp.vector-sub-iterators.h\n",
"\n",
......@@ -395,13 +467,21 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 21,
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Overwriting tmp.vector.h\n"
]
}
],
"source": [
"%%file tmp.vector.h\n",
"\n",
......@@ -428,7 +508,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 24,
"metadata": {
"attributes": {
"classes": [
......@@ -440,11 +520,19 @@
"slide_type": "subslide"
}
},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Overwriting tmp.expressions-templates.cpp\n"
]
}
],
"source": [
"%%file tmp.expressions-templates.cpp\n",
"\n",
"#include \"tmp.nodes.h\"\n",
"#include \"tmp.iterators.h\"\n",
"#include \"tmp.vector.h\"\n",
"#include \"tmp.vector-add-iterators.h\"\n",
"#include \"tmp.vector-sub-iterators.h\"\n",
......@@ -460,7 +548,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 25,
"metadata": {
"attributes": {
"classes": [
......@@ -479,13 +567,21 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 26,
"metadata": {
"slideshow": {
"slide_type": "-"
}
},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1.09|2.18|3.297\n"
]
}
],
"source": [
"!./tmp.expressions-templates.exe"
]
......
......@@ -10,7 +10,7 @@ int inc( int i ) { return i+1 ; }
int main()
{
std::array<int,5> table = { 1, 2, 3, 4, 5 } ;
std::array<int,5> table { 1, 2, 3, 4, 5 } ;
auto f = compose(inc,square) ;
for ( auto element : table )
std::cout<<f(element)<<std::endl ;
......
......@@ -2,10 +2,10 @@
#include <iostream>
#include <optional>
template< typename Output, typename Input >
auto raise( Output(&f)(Input) )
template< typename OutputType, typename InputType >
auto raise( OutputType(&f)(InputType) )
{
return [f]( std::optional<Input> input ) -> std::optional<Output>
return [f]( std::optional<InputType> input ) -> std::optional<OutputType>
{
if (input) { return f(input.value()) ; }
else { return std::nullopt ; }
......
......@@ -189,7 +189,7 @@
"\n",
"int main()\n",
" { \n",
" std::array<int,5> table = { 1, 2, 3, 4, 5 } ;\n",
" std::array<int,5> table { 1, 2, 3, 4, 5 } ;\n",
" auto f = compose(inc,square) ;\n",
" for ( auto element : table )\n",
" std::cout<<f(element)<<std::endl ;\n",
......
......@@ -240,7 +240,7 @@
"source": [
"## Exercise 2 (difficult)\n",
"\n",
"Rather than modifying `square`, write a high-order function `raise`, which take as input a function of type `Output (&)( Input )`, and return a function of type `std::optional<Output> (&)( std::optional<Input> )`, which is calling the former one when its input optional has a value, or return an empty optional otherwise. Then, `raise` can help to reuse the original `square` without modifying it, it may be used likewise with any function `double(double)`.\n",
"Rather than modifying `square`, write a high-order function `raise`, which take as input a function of type `OutputType (&)( InputType )`, and return a function of type `std::optional<OutputType> (&)( std::optional<InputType> )`, which is calling the former one when its input optional has a value, or return an empty optional otherwise. Then, `raise` can help to reuse the original `square` without modifying it, it may be used likewise with any function `double(double)`.\n",
"\n",
"<!--\n",
"This requires, at the very minimum, the new ... C++!& stuff ...\n",
......@@ -275,10 +275,10 @@
"double mysqrt( double d ) // TO BE MODIFIED\n",
" { return std::sqrt(d) ; } // TO BE MODIFIED\n",
"\n",
"template< typename Output, typename Input >\n",
"auto raise( Output(&f)(Input) )\n",
"template< typename OutputType, typename InputType >\n",
"auto raise( OutputType(&f)(InputType) )\n",
" {\n",
" return [f]( std::optional<Input> input ) -> std::optional<Output>\n",
" return [f]( std::optional<InputType> input ) -> std::optional<OutputType>\n",
" {\n",
" // TO BE COMPLETED\n",
" } ;\n",
......
......@@ -189,7 +189,7 @@
"\n",
"int main()\n",
" { \n",
" std::array<int,5> tableau = { 1, 2, 3, 4, 5 } ;\n",
" std::array<int,5> tableau { 1, 2, 3, 4, 5 } ;\n",
" auto f = compose(inc,carre) ;\n",
" for ( auto element : tableau )\n",
" std::cout<<f(element)<<std::endl ;\n",
......
......@@ -240,8 +240,7 @@
"source": [
"## Exercice 2 (difficile)\n",
"\n",
"Plutôt que de modifier `square`, écrivez une fonction d'ordre supérieur `raise`, qui recoit une fonction d'entrée de type `Output (&)( Input )` et retourne une fonction de type `std::optional<Output> (&)( std::optional<Input> )`,\n",
"qui retourne l'optionnel vide quand lóptionnel d'entrée est vide.\n",
"Plutôt que de modifier `square`, écrivez une fonction d'ordre supérieur `raise`, qui recoit une fonction d'entrée de type `OutputType (&)( InputTypeType )` et retourne une fonction de type `std::optional<OutputType> (&)( std::optional<InputType> )`, qui retourne l'optionnel vide quand l'optionnel d'entrée est vide.\n",
"\n",
"\n",
"<!--\n",
......@@ -277,10 +276,10 @@
"double mysqrt( double d ) // TO BE MODIFIED\n",
" { return std::sqrt(d) ; } // TO BE MODIFIED\n",
"\n",
"template< typename Output, typename Input >\n",
"auto raise( Output(&f)(Input) )\n",
"template< typename OutputType, typename InputType >\n",
"auto raise( OutputType(&f)(InputType) )\n",
" {\n",
" return [f]( std::optional<Input> input ) -> std::optional<Output>\n",
" return [f]( std::optional<InputType> input ) -> std::optional<OutputType>\n",
" {\n",
" // TO BE COMPLETED\n",
" } ;\n",
......