Stroika Library 3.0d16
 
Loading...
Searching...
No Matches
FunctionalApplication.inl
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2025. All rights reserved
3 */
4#include "Stroika/Foundation/Containers/Sequence.h"
6
7namespace Stroika::Foundation::Traversal {
8
9 /*
10 ********************************************************************************
11 ******************** Traversal::DirectPushMapEngine ****************************
12 ********************************************************************************
13 */
14 template <typename IN_T, typename OUT_T>
15 Iterable<OUT_T> DirectPushMapEngine::Map (const Iterable<IN_T>& from, const function<OUT_T (IN_T)>& do2Each)
16 {
17 Containers::Sequence<OUT_T> result;
18 for (const IN_T& i : from) {
19 // unsure if we update in place, or create a new container? May need traits param to define how todo this!
20 result.Append (do2Each (i));
21 }
22 return move (result);
23 }
24 template <typename IN_T, typename OUT_T>
25 OUT_T DirectPushMapEngine::Reduce (const Iterable<IN_T>& from, const function<OUT_T (IN_T, OUT_T)>& do2Each, OUT_T memo)
26 {
27 OUT_T result = memo;
28 for (const IN_T& i : from) {
29 result = do2Each (i, result);
30 }
31 return result;
32 }
33 template <typename IN_OUT_T>
34 Iterable<IN_OUT_T> DirectPushMapEngine::Filter (const Iterable<IN_OUT_T>& from, const function<bool (IN_OUT_T)>& includeTest)
35 {
36 Containers::Sequence<IN_OUT_T> result;
37 for (const IN_OUT_T& i : from) {
38 if (includeTest (i)) {
39 result.Append (i);
40 }
41 }
42 return move (result);
43 }
44 template <typename IN_OUT_T>
45 optional<IN_OUT_T> DirectPushMapEngine::Find (const Iterable<IN_OUT_T>& from, const function<bool (IN_OUT_T)>& thatPassesThisTest)
46 {
47 for (const IN_OUT_T& i : from) {
48 if (thatPassesThisTest (i)) {
49 return i;
50 }
51 }
52 return nullopt;
53 }
54
55 /*
56 ********************************************************************************
57 ******************** Traversal::FunctionalApplicationContext *******************
58 ********************************************************************************
59 */
60 template <typename T, typename MAPPER_ENGINE>
61 inline FunctionalApplicationContext<T, MAPPER_ENGINE>::FunctionalApplicationContext (Iterable<T> i, MAPPER_ENGINE m)
62 : inherited{i}
63 , fMappingEngine_{m}
64 {
65 }
66 template <typename T, typename MAPPER_ENGINE>
67 template <typename OUT_T>
69 {
70 return FunctionalApplicationContext<OUT_T, MAPPER_ENGINE>{fMappingEngine_.Map (inherited{*this}, do2Each), fMappingEngine_};
71 }
72 template <typename T, typename MAPPER_ENGINE>
73 template <typename OUT_T>
74 inline OUT_T FunctionalApplicationContext<T, MAPPER_ENGINE>::Reduce (const function<OUT_T (T, OUT_T)>& do2Each, OUT_T memo)
75 {
76 return fMappingEngine_.Reduce (inherited{*this}, do2Each, memo);
77 }
78 template <typename T, typename MAPPER_ENGINE>
79 template <typename INOUT_T>
81 FunctionalApplicationContext<T, MAPPER_ENGINE>::Filter (const function<bool (INOUT_T)>& includeTest)
82 {
83 return FunctionalApplicationContext<INOUT_T, MAPPER_ENGINE>{fMappingEngine_.Filter (inherited{*this}, includeTest), fMappingEngine_};
84 }
85 template <typename T, typename MAPPER_ENGINE>
86 template <typename INOUT_T>
87 optional<INOUT_T> FunctionalApplicationContext<T, MAPPER_ENGINE>::Find (const function<bool (INOUT_T)>& that)
88 {
89 return fMappingEngine_.Find (inherited{*this}, that);
90 }
91
92}
OUT_T Reduce(const function< OUT_T(T, OUT_T)> &do2Each, OUT_T memo=OUT_T())
FunctionalApplicationContext< OUT_T, MAPPER_ENGINE > Map(const function< OUT_T(T)> &do2Each)
Iterable<T> is a base class for containers which easily produce an Iterator<T> to traverse them.
Definition Iterable.h:237