Last active
December 12, 2015 07:49
-
-
Save MiSawa/4739762 to your computer and use it in GitHub Desktop.
samples for sniplate.vim
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <vector> | |
#include <cassert> | |
using namespace std; | |
//BEGIN SNIPLATE sum | |
//{{abbr: sum for vector<double>}} | |
//{{class: util, statistics}} | |
//{{pattern: SNIPLATE_SUM}} | |
double sum(vector<double> in) { //SNIPLATE_SUM | |
double res = 0; | |
for(int i = 0; i < in.size(); ++i) { | |
res += in[i]; | |
} | |
return res; | |
} | |
//END SNIPLATE | |
//BEGIN SNIPLATE average | |
//{{abbr: average for vector<double>}} | |
//{{require: sum}} | |
//{{class: statistics}} | |
//{{pattern: \S*\s*average(vector<.*>.*)}} | |
double average(vector<double> in) { | |
return sum(in) / in.size(); | |
} | |
//END SNIPLATE | |
//BEGIN SNIPLATE variance_p | |
//{{abbr: population variance for vector<double>}} | |
//{{require: average}} | |
//{{class: statistics}} | |
//{{pattern: \S*\s*variance_p(vector<.*>.*)}} | |
double variance_p(vector<double> in) { | |
double avg = average(in); | |
for(int i = 0; i < in.size(); ++i) { | |
in[i] = (in[i] - avg) * (in[i] - avg); | |
} | |
return average(in); | |
} | |
//END SNIPLATE | |
//BEGIN SNIPLATE variance_s | |
//{{abbr: sample variance for vector<double>}} | |
//{{require: sum}} | |
//{{class: statistics}} | |
//{{pattern: \S*\s*variance_s(vector<.*>.*)}} | |
double variance_s(vector<double> in) { | |
double avg = average(in); | |
for(int i = 0; i < in.size(); ++i) { | |
in[i] = (in[i] - avg) * (in[i] - avg); | |
} | |
return sum(in)/(in.size()-1); | |
} | |
//END SNIPLATE | |
int main(void){ | |
vector<double> data; | |
for(int i = 0; i < 11; ++i) | |
data.push_back(i); | |
assert(sum(data) == 55); | |
assert(average(data) == 5); | |
assert(variance_p(data) == 10); | |
assert(variance_s(data) == 11); | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//BEGIN SNIPLATE includes | |
//{{invisible}} | |
#include <iostream> | |
// : | |
#include <vector> | |
//END SNIPLATE | |
//BEGIN SNIPLATE typedefs | |
//{{require: includes}} | |
//{{invisible}} | |
using namespace std; | |
typedef long long ll; | |
typedef vector<int> vi; | |
typedef pair<int, int> pii; | |
//END SNIPLATE | |
//BEGIN SNIPLATE macros | |
//{{invisible}} | |
//{{require: typedefs}} | |
#define rep(i, n) for(int i=0; i<(n); ++i) | |
#define foreach(it, v) for(typeof(v.begin()) it = (v).begin(); it != (v).end(); ++it) | |
#define pb push_back | |
//END SNIPLATE | |
//BEGIN SNIPLATE header | |
//{{require: includes, typedefs, macros}} | |
//{{invisible}} | |
//END SNIPLATE | |
//BEGIN SNIPLATE main_void | |
//{{require: header}} | |
//{{class: main}} | |
int main(void){ | |
//{{cursor}} | |
return 0; | |
} | |
//END SNIPLATE | |
/* | |
//BEGIN SNIPLATE main_arg | |
//{{require: header}} | |
//{{class: main}} | |
int main(int argc, char const* argv[]) { | |
//{{cursor}} | |
return 0; | |
} | |
//END SNIPLATE | |
*/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//BEGIN SNIPLATE sum_T | |
//{{abbr: sum for vector<T>}} | |
//{{class: statistics}} | |
//{{pattern: \S*\s*sum(vector<.*>.*)}} | |
//{{input:T:'input T:', 'double'}} | |
{{var:T}} sum(vector<{{var:T}}> in) { | |
int res = 0; | |
for(int i = 0; i < in.size(); ++i) { | |
res += in[i]; | |
} | |
return res; | |
} | |
//END SNIPLATE | |
//BEGIN SNIPLATE average_T | |
//{{abbr: average for vector<double>}} | |
//{{require: sum_T}} | |
//{{class: statistics}} | |
//{{pattern: \S*\s*average(vector<.*>.*)}} | |
{{var:T}} average(vector<{{var:T}}> in) { | |
return sum(in) / in.size(); | |
} | |
//END SNIPLATE |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment