Euphoria
functional.h
Go to the documentation of this file.
1 #pragma once
2 
3 
4 #include <utility>
5 #include <map>
6 
7 
8 namespace eu
9 {
10  //
11  // Common polymorphic higher order functions
12  //
13 
14  // todo(Gustav): add tests
15 
16  // combine 2 list into a list of pair
17  template <typename A, typename B>
18  std::vector<std::pair<A, B>>
19  zip(const std::vector<A>& as, const std::vector<B>& bs)
20  {
21  std::vector<std::pair<A, B>> ret;
22  const auto s = std::min(as.size(), bs.size());
23  for(std::size_t index = 0; index < s; index += 1)
24  {
25  ret.emplace_back(std::make_pair<A, B>(as[index], bs[index]));
26  }
27  return ret;
28  }
29 
30 
31  template <typename A, typename B>
32  std::vector<std::pair<A, B>>
34  (
35  const std::vector<A>& as,
36  const std::vector<B>& bs,
37  A da = A(),
38  B db = B()
39  )
40  {
41  std::vector<std::pair<A, B>> ret;
42  const auto s = std::max(as.size(), bs.size());
43  for(std::size_t index = 0; index < s; index += 1)
44  {
45  const A a = index < as.size() ? as[index] : da;
46  const B b = index < bs.size() ? bs[index] : db;
47  ret.emplace_back(a, b);
48  }
49  return ret;
50  }
51 
52 
53  // map: A->B for a list, Select in c#
54  template <typename T, typename F = T, typename C>
55  std::vector<T>
56  map(const std::vector<F>& fs, C convert)
57  {
58  std::vector<T> r;
59  r.reserve(fs.size());
60  for(const auto& f: fs)
61  {
62  r.emplace_back(convert(f));
63  }
64  return r;
65  }
66 
67 
68  template <typename T, typename K, typename V, typename C>
69  std::vector<T>
70  map(const std::map<K, V>& fs, C convert)
71  {
72  std::vector<T> r;
73  r.reserve(fs.size());
74  for(const auto f: fs)
75  {
76  r.emplace_back(convert(f.first, f.second));
77  }
78  return r;
79  }
80 
81  // concat map, SelectMany
82 
83  // filter - all values that satisfies a condition
84  template <typename T, typename C>
85  std::vector<T>
86  filter(const std::vector<T>& ts, C check)
87  {
88  std::vector<T> r;
89  r.reserve(ts.size());
90  for(const auto t: ts)
91  {
92  if(check(t))
93  {
94  r.emplace_back(t);
95  }
96  }
97  return r;
98  }
99 
100  // takewhile
101 
102  // return true if all matches
103  template <typename T, typename C>
104  bool
105  all(const std::vector<T>& ts, C check)
106  {
107  std::vector<T> r;
108  r.reserve(ts.size());
109  for(const auto t: ts)
110  {
111  if(!check(t))
112  {
113  return false;
114  }
115  }
116  return true;
117  }
118 
119  // return true if any mathes
120  template <typename T, typename C>
121  bool
122  any(const std::vector<T>& ts, C check)
123  {
124  std::vector<T> r;
125  r.reserve(ts.size());
126  for(const auto t: ts)
127  {
128  if(check(t))
129  {
130  return true;
131  }
132  }
133  return false;
134  }
135 
136  // zipwith - zip + map - ZipWith in c#
137 
138  // iterate - create infinite iterator - useful?
139 
140  // foldr
141  // foldl - Aggregate in c#
142  // what do I have below?, what's the difference
143 
144  // combine list into single value
145  template <typename T, typename R = T, typename C>
146  R
147  fold(const std::vector<T>& ts, C concat, R zero)
148  {
149  R r = zero;
150  for(const auto& t: ts)
151  {
152  r = concat(r, t);
153  }
154  return r;
155  }
156 }
157 
Definition: assert.h:90
R fold(const std::vector< T > &ts, C concat, R zero)
Definition: functional.h:147
std::vector< std::pair< A, B > > zip_longest(const std::vector< A > &as, const std::vector< B > &bs, A da=A(), B db=B())
Definition: functional.h:34
bool all(const std::vector< T > &ts, C check)
Definition: functional.h:105
std::vector< T > map(const std::vector< F > &fs, C convert)
Definition: functional.h:56
std::vector< std::pair< A, B > > zip(const std::vector< A > &as, const std::vector< B > &bs)
Definition: functional.h:19
size2f min(const size2f lhs, const size2f rhs)
Definition: size2.cc:140
size2f max(const size2f lhs, const size2f rhs)
Definition: size2.cc:149
bool any(const std::vector< T > &ts, C check)
Definition: functional.h:122
std::vector< T > filter(const std::vector< T > &ts, C check)
Definition: functional.h:86