ModErn Text Analysis
META Enumerates Textual Applications
functional.h
Go to the documentation of this file.
1 
10 #ifndef META_UTIL_FUNCTIONAL_H_
11 #define META_UTIL_FUNCTIONAL_H_
12 
13 #include <algorithm>
14 #include <functional>
15 #include <map>
16 
17 namespace meta
18 {
19 namespace functional
20 {
21 
27 template <class Result, class... Args>
28 std::function<Result(Args...)> memoize(std::function<Result(Args...)> fun)
29 {
30  return [fun](Args... args)
31  {
32  static std::map<std::tuple<Args...>, Result> map_;
33  auto it = map_.find(std::make_tuple(args...));
34  if (it != map_.end())
35  return it->second;
36  return map_[std::make_tuple(args...)] = fun(args...);
37  };
38 }
39 
48 template <class Iter, class Function>
49 Iter argmax(Iter begin, Iter end, Function&& fn)
50 {
51  using T = decltype(*begin);
52  return std::max_element(begin, end, [&](const T& a, const T& b)
53  {
54  return fn(a) < fn(b);
55  });
56 }
57 
58 }
59 }
60 #endif
The ModErn Text Analysis toolkit is a suite of natural language processing, classification, information retreival, data mining, and other applications of text processing.
Definition: analyzer.h:24