Modern programming languages for HEP pyOrC+UsableCpp perfC++python Lambdas Definition a lambda is a function with no name Syntax [captures](args)->type code; The type specification is optional Usage example int sum =0,offset =1; std::vector<int>data[1,9,3,8,3,7,4,6,5]; for_each(begin(data),end(data), [&sum,offset](int x){ sum +x offset; ); simple hanBadCode 16/65 S.Ponce-CERN
Modern programming languages for HEP 16 / 65 S. Ponce - CERN pyOrC++ UsableCpp perfC++ python3 Conclusion simple banBadCode Lambdas Definition a lambda is a function with no name Syntax [captures] (args) -> type { code; } The type specification is optional Usage example int sum = 0, offset = 1; std::vector<int> data{1,9,3,8,3,7,4,6,5}; for_each(begin(data), end(data), [&sum, offset](int x) { sum += x + offset; });
Modern programming languages for HEP 4y0C十+ Ranges (C++20) Reason of being o provide easy manipulation of sets of data via views o simplify the horrible iterator syntax Syntax Based on Unix like pipes,and used in range based loops Example code-godbolt std::vector<int>numbers{...}; auto results numbers I filter([](int n){return n 2 =0;} I transform([](int n){return n 2;}) for (auto v:results)std:cout <<v<<""; simple banBadCade 17/65 S.Ponce-CERN
Modern programming languages for HEP 17 / 65 S. Ponce - CERN pyOrC++ UsableCpp perfC++ python3 Conclusion simple banBadCode Ranges (C++20) Reason of being provide easy manipulation of sets of data via views simplify the horrible iterator syntax Syntax Based on Unix like pipes, and used in range based loops Example code - godbolt std::vector<int> numbers{...}; auto results = numbers | filter([](int n){ return n % 2 == 0; }) | transform([](int n){ return n * 2; }); for (auto v: results) std::cout << v << " ";
Modern programming languages for HEP So far essentially syntactic sugar Range based loops for (int a v){sum *=a; Translate to iterators for (auto it begin(v);it !end(v);it++){ sum *=*it; simple banBadCade 18/65 S.Ponce-CERN
Modern programming languages for HEP 18 / 65 S. Ponce - CERN pyOrC++ UsableCpp perfC++ python3 Conclusion simple banBadCode So far essentially syntactic sugar Range based loops for (int a : v) { sum *= a; } Translate to iterators for (auto it = begin(v); it != end(v); it++) { sum *= *it; }
Modern programming languages for HEP 4y0C十 So far essentially syntactic sugar Lambdas [&sum,offset](int x){sum +=x offset; Are just functors struct MyFunc int&m_sum; int m_offset; MyFunc(int&s,int o):m_sum(s),m_offset (o){ void operator(int x){m_sum +=x +m_offset; 3; MyFunc(sum,offset) By the way,as lambdas are functors,they can inherit from each other And this can be super useful. simple banBadCade 19/65 S.Ponce-CERN
Modern programming languages for HEP 19 / 65 S. Ponce - CERN pyOrC++ UsableCpp perfC++ python3 Conclusion simple banBadCode So far essentially syntactic sugar Lambdas [&sum, offset](int x) { sum += x + offset; } Are just functors struct MyFunc { int& m_sum; int m_offset; MyFunc(int& s, int o) : m_sum(s), m_offset(o) {} void operator(int x) { m_sum += x + m_offset; } }; MyFunc(sum, offset) By the way, as lambdas are functors, they can inherit from each other ! And this can be super useful
Modern programming languages for HEP 4心P0C十十UsableCpp perfC+十whom 花5 C++getting usable ①Why python and C+ ②C++getting usable o Language "simplifications" o Making bad code harder to write Performant C++ Migrating from Python 2 to python 3 5 Conclusion timpl banBadCode 20/65 S.Ponce-CERN
Modern programming languages for HEP 20 / 65 S. Ponce - CERN pyOrC++ UsableCpp perfC++ python3 Conclusion simple banBadCode C ++getting usable 1 Why python and C++ 2 C ++getting usable Language “simplifications” Making bad code harder to write 3 Performant C++ 4 Migrating from Python 2 to python 3 5 Conclusion