Skip to content

Instantly share code, notes, and snippets.

@Luchanso
Last active January 17, 2020 16:42
Show Gist options
  • Save Luchanso/a3802696bf4d75a7f2fb6c145710feb6 to your computer and use it in GitHub Desktop.
Save Luchanso/a3802696bf4d75a7f2fb6c145710feb6 to your computer and use it in GitHub Desktop.
class Logger {
constructor(Database database, String username) {
this.database = database;
this.username = username;
}
String* format(String* str, String* type) {
String result = "[" + new Date().toString() + "][" + type + "][" + this.username + "] " + str;
return result;
}
void log(String *str) {
this.database.writeLog(str);
cout << str;
}
void info(String* str) {
this.log(this.format(str, "инфо"));
}
void error(String *str) {
this.log(this.format(str, "ошибка"));
}
}
// Cat file
class Cat {
String* name;
constructor(String* name) {
this.name = name;
}
meow() {
return "meow";
}
}
// CatLogger file
#include "Cat.cpp"
class LoggedCat : Cat {
String* name;
Logger* logger;
constructor(String* name, Logger logger)
: cat_(name) {
this.logger = logger;
}
virtual void meow() {
logger.info('meow call with name: ' + this.name);
String result = cat_.meow();
logger.info('meow return: ' + result);
return result;
}
}
// Main file
#include "LoggedCat.cpp"
#include "Logger.cpp"
#include "Database.cpp"
class Main {
void main() {
Database database = new Database("пароли", "явки");
Logger logger = new Logger(database);
LoggedCat cat = new LoggedCat("Муся");
cat.meow();
}
}
import { createDbConnection, Database } from "database";
type Cat = {
name: String;
};
const withCurrentDate = () => (str: String) =>
str + "[" + new Date().toString() + "]";
const withType = (type: String) => (str: String) => str + "[" + type + "]";
const withUsername = (username: String) => (str: String) =>
str + "[" + username + "]";
const createLog = (database: Database) => (str: String) => {
database.log(str);
console.log(str);
};
const withLogger = (fn: Function) => (log: Function) => (...args) => {
log("arguments: " + [...args]);
const result = fn(...args);
log("return: " + result);
return result;
};
const compose = (...allFunctions) => data => {
allFunctions.reduce((result, fn) => fn(result), data);
};
const createInfo = (username: string) => (str: String) =>
compose(
withCurrentDate(),
withType("info"),
withUsername(username)
)(str);
const meow = (cat: Cat) => "meow" + cat.name;
const createCat = (name: String): Cat => ({
name
});
function main() {
const database = createDbConnection("пароли", "явки");
const log = createLog(database);
const cat = createCat("Муся");
const username = "Вася";
const info = createInfo(username);
const meowWithLog = withLogger(meow)(str => log(info(str)));
meowWithLog(cat);
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment