@@ -312,8 +312,8 @@ int main() {
312312 std::vector<int> items {1, 2, 3, 4, 5};
313313 auto odd_sq = items | std::views::transform([](int i){return i*i;})
314314 | std::views::filter([](int i){return i%2==1;});
315- sum_sq_odds = std::accumulate(std::begin(odd_sq), std::end(odd_sq), 0, [](int a, int b){return a + b;})
316- std::cout << result << std::endl;
315+ auto sum_sq_odds = std::accumulate(std::begin(odd_sq), std::end(odd_sq), 0, [](int a, int b){return a + b;})
316+ std::cout << sum_sq_odds << std::endl;
317317 return 0;
318318}
319319```
@@ -322,6 +322,23 @@ Not that C++ is slowly gaining support; `std::fold_left` is in C++23, but for
322322C++20, we have to drop back to a classic `std::accumulate` algorithm & begin
323323and end iterators.
324324
325+ ````
326+ ````{tab-item} C++23 Ranges
327+ ```cpp
328+ import std;
329+
330+ int main() {
331+ std::vector items {1, 2, 3, 4, 5};
332+ auto odd_sq = items | std::views::transform([](int i){return i*i;})
333+ | std::views::filter([](int i){return i%2==1;});
334+ auto sum_sq_odds = std::ranges::fold_left(odd_sq, 0, [](int a, int b){return a + b;});
335+ std::println("{}", sum_sq_odds);
336+ return 0;
337+ }
338+ ```
339+
340+ Not that stdlib module support is not available yet.
341+
325342````
326343````{tab-item} Rust
327344```rust
@@ -335,6 +352,16 @@ fn main() {
335352}
336353```
337354````
355+
356+ ````{tab-item} Swift
357+ ```swift
358+ let items = [1, 2, 3, 4, 5]
359+ let sum_sq_odds = items.map { $0 * $0 }
360+ .filter { $0 % 2 == 1 }
361+ .reduce(0, +)
362+ print(sum_sq_odds)
363+ ```
364+ ````
338365`````
339366
340367Notice how many of the languages use similar terms and try to read well when
@@ -516,3 +543,7 @@ Numba library, which is imperative. PyTorch is a great ML-focused library. CuPy
516543is a great GPU NumPy replacement. Etc. JAX here is just intended to be an
517544example of what thinking in a functional mindset can do.
518545```
546+
547+ ```
548+
549+ ```
0 commit comments