Commit b2f3e1ce authored by CHAMONT David's avatar CHAMONT David
Browse files

Cours complet

parent 9368f3ab
...@@ -78,7 +78,7 @@ ...@@ -78,7 +78,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 1, "execution_count": 4,
"metadata": { "metadata": {
"slideshow": { "slideshow": {
"slide_type": "skip" "slide_type": "skip"
...@@ -86,40 +86,62 @@ ...@@ -86,40 +86,62 @@
}, },
"outputs": [], "outputs": [],
"source": [ "source": [
"#include <fstream>\n", "#include <iostream>\n",
"#include <vector>\n", "#include <vector>\n",
"#include <string>" "#include <string>"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 2, "execution_count": 11,
"metadata": { "metadata": {
"slideshow": { "slideshow": {
"slide_type": "-" "slide_type": "subslide"
} }
}, },
"outputs": [], "outputs": [],
"source": [ "source": [
"void count_lines_in_files\n", "void spaces_in_sentences\n",
" ( std::vector<std::string> & fnames, std::vector<int> & nb_lines )\n", " ( std::vector<std::string> & sentences, std::vector<unsigned> & spaces )\n",
" {\n", " {\n",
" std::vector<std::string>::iterator fileitr ;\n", " unsigned i, imax = sentences.size() ;\n",
" for ( fileitr = fnames.begin() ; fileitr != fnames.end() ; ++fileitr )\n", " for ( i = 0 ; i<imax ; ++i )\n",
" {\n", " {\n",
" int line_count = 0 ;\n", " spaces[i] = 0 ;\n",
" char c = 0 ;\n", " unsigned j, jmax = sentences[i].size() ;\n",
" std::ifstream in(fileitr->c_str()) ;\n", " for ( j = 0 ; j<jmax ; ++j )\n",
" while (in.get(c))\n",
" {\n", " {\n",
" if (c == '\\n')\n", " if (sentences[i][j]==' ')\n",
" { line_count++ ; }\n", " { ++spaces[i] ; }\n",
" }\n", " }\n",
" nb_lines.push_back(line_count) ;\n",
" }\n", " }\n",
" }" " }"
] ]
}, },
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [],
"source": [
"int main()\n",
" {\n",
" unsigned SIZE = 2 ;\n",
" std::vector<std::string> sentences(SIZE) ;\n",
" sentences[0] = \"Hello world !\" ;\n",
" sentences[1] = \"Happy birthday\" ;\n",
" \n",
" std::vector<unsigned> spaces(sentences.size()) ;\n",
" spaces_in_sentences(sentences,spaces) ;\n",
" \n",
" std::cout<<spaces[0]<<' '<<spaces[1]<<std::endl ;\n",
" }"
]
},
{ {
"cell_type": "markdown", "cell_type": "markdown",
"metadata": { "metadata": {
...@@ -161,7 +183,7 @@ ...@@ -161,7 +183,7 @@
}, },
"outputs": [], "outputs": [],
"source": [ "source": [
"#include <fstream>\n", "#include <iostream>\n",
"#include <vector>\n", "#include <vector>\n",
"#include <string>\n", "#include <string>\n",
"#include <algorithm>" "#include <algorithm>"
...@@ -169,7 +191,22 @@ ...@@ -169,7 +191,22 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 5, "execution_count": 7,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"unsigned spaces_in_sentence\n",
" ( std::string const & sentence )\n",
" { return std::count(sentence.begin(),sentence.end(),' ') ; }"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": { "metadata": {
"slideshow": { "slideshow": {
"slide_type": "fragment" "slide_type": "fragment"
...@@ -177,29 +214,34 @@ ...@@ -177,29 +214,34 @@
}, },
"outputs": [], "outputs": [],
"source": [ "source": [
"int count_lines( std::string const & filename )\n", "std::vector<unsigned> spaces_in_sentences\n",
" ( std::vector<std::string> const & sentences )\n",
" {\n", " {\n",
" std::ifstream in(filename.c_str()) ;\n", " std::vector<unsigned> spaces(sentences.size()) ;\n",
" typedef std::istreambuf_iterator<char> ifiterator ;\n", " std::transform(sentences.begin(),sentences.end(),spaces.begin(),spaces_in_sentence) ;\n",
" return std::count(ifiterator(in), ifiterator(), '\\n') ;\n", " return spaces ;\n",
" }" " }"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 6, "execution_count": 9,
"metadata": { "metadata": {
"slideshow": { "slideshow": {
"slide_type": "fragment" "slide_type": "skip"
} }
}, },
"outputs": [], "outputs": [],
"source": [ "source": [
"std::vector<int> count_lines_in_files( std::vector<std::string> const & files )\n", "int main()\n",
" {\n", " {\n",
" std::vector<int> nblines(files.size()) ;\n", " unsigned SIZE = 2 ;\n",
" std::transform(files.begin(), files.end(), nblines.begin(), count_lines) ;\n", " std::vector<std::string> sentences(SIZE) ;\n",
" return nblines ;\n", " sentences[0] = \"Hello world !\" ;\n",
" sentences[1] = \"Happy birthday\" ;\n",
" \n",
" std::vector<unsigned> spaces = spaces_in_sentences(words) ;\n",
" std::cout<<spaces[0]<<' '<<spaces[1]<<std::endl ;\n",
" }" " }"
] ]
}, },
...@@ -222,7 +264,7 @@ ...@@ -222,7 +264,7 @@
} }
}, },
"source": [ "source": [
"© *CNRS 2019* \n", "© *CNRS 2022* \n",
"*This document was created by David Chamont, proofread and improved by Hadrien Grasland, and translated by Olga Abramkina. It is available under the [License Creative Commons - Attribution - No commercial use - Shared under the conditions 4.0 International](http://creativecommons.org/licenses/by-nc-sa/4.0/)*" "*This document was created by David Chamont, proofread and improved by Hadrien Grasland, and translated by Olga Abramkina. It is available under the [License Creative Commons - Attribution - No commercial use - Shared under the conditions 4.0 International](http://creativecommons.org/licenses/by-nc-sa/4.0/)*"
] ]
} }
......
...@@ -179,20 +179,19 @@ ...@@ -179,20 +179,19 @@
"\n", "\n",
"#include <iostream>\n", "#include <iostream>\n",
"#include <array>\n", "#include <array>\n",
"#include <algorithm>\n",
"\n", "\n",
"template< typename Function1, typename Function2 >\n", "template< typename Function1, typename Function2 >\n",
"... compose( Function1 f1, Function2 f2 )\n", "... compose( Function1 f1, Function2 f2 )\n",
" { return ... ; }\n", " { return ... ; }\n",
"\n", "\n",
"int square( int i ) { return i*i ; }\n", "int square( int i ) { return i*i ; }\n",
"int inc( int i ) { return i+1 ; }\n", "void display( int i ) { std::cout<<i<<std::endl ; }\n",
"\n", "\n",
"int main()\n", "int main()\n",
" { \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", " std::for_each(table.begin(),table.end(),compose(display,square)) ;\n",
" for ( auto element : table )\n",
" std::cout<<f(element)<<std::endl ;\n",
" }" " }"
] ]
}, },
...@@ -230,7 +229,7 @@ ...@@ -230,7 +229,7 @@
} }
}, },
"source": [ "source": [
"© *CNRS 2021* \n", "© *CNRS 2022* \n",
"*This document was created by David Chamont, proofread and improved by Hadrien Grasland and translated by Olga Abramkina. It is available under the [License Creative Commons - Attribution - No commercial use - Shared under the conditions 4.0 International](http://creativecommons.org/licenses/by-nc-sa/4.0/)*" "*This document was created by David Chamont, proofread and improved by Hadrien Grasland and translated by Olga Abramkina. It is available under the [License Creative Commons - Attribution - No commercial use - Shared under the conditions 4.0 International](http://creativecommons.org/licenses/by-nc-sa/4.0/)*"
] ]
} }
......
...@@ -344,7 +344,53 @@ ...@@ -344,7 +344,53 @@
} }
}, },
"source": [ "source": [
"© *CNRS 2020* \n", "## Exercise 3 (difficult)\n",
"\n",
"What happens if you want a `square` of `square` ? Can you add another flavor of `raise` which would solve the issue ?"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Writing tmp.monadic-types.cpp\n"
]
}
],
"source": [
"%%file tmp.monadic-types.cpp\n",
"\n",
"#include <cmath>\n",
"#include <iostream>\n",
"#include <optional>\n",
"\n",
"// TO BE COMPLETED\n",
"\n",
"int main()\n",
" {\n",
" std::cout<<mysqrt(mysqrt(10))<<std::endl ;\n",
" std::cout<<mysqrt(mysqrt(-10))<<std::endl ;\n",
" }"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
"© *CNRS 2022* \n",
"*This document was created by David Chamont, proofread and improved by Hadrien Grasland and translated by Olga Abramkina. It is available under the [License Creative Commons - Attribution - No commercial use - Shared under the conditions 4.0 International](http://creativecommons.org/licenses/by-nc-sa/4.0/)*" "*This document was created by David Chamont, proofread and improved by Hadrien Grasland and translated by Olga Abramkina. It is available under the [License Creative Commons - Attribution - No commercial use - Shared under the conditions 4.0 International](http://creativecommons.org/licenses/by-nc-sa/4.0/)*"
] ]
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment