-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLazyRepeater.cpp
More file actions
45 lines (41 loc) · 1.3 KB
/
LazyRepeater.cpp
File metadata and controls
45 lines (41 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
#include <cmath>
#include <string>
#include <vector>
#include <string_view>
using namespace std;
auto makeLooper(const string& str) {
size_t index = 0;
return [str, index]() mutable -> char {
char currentChar = str[index];
index = (index + 1) % str.length();
return currentChar;
};
}
int main()
{
makeLooper("abc");
}
/*Description:
The makeLooper() function (or make_looper in your language) takes a string (of non-zero length) as an argument. It returns a function.
The function it returns will return successive characters of the string on successive invocations.
It will start back at the beginning of the string once it reaches the end.
For example:
auto abc = makeLooper("abc");
abc(); // should return 'a' on this first call
abc(); // should return 'b' on this second call
abc(); // should return 'c' on this third call
abc(); // should return 'a' again on this fourth call
Different loopers should not affect each other, so be wary of unmanaged global state.
Iterators
Algorithms*/
// /> フ
// | n n 彡
// /`ミ_xノ
// / |
// / ヽ ノ
// │ | | |
// / ̄| | | |
// | ( ̄ヽ__ヽ_)__)
// \二つ
// ITS CAT FOR YOU