Skip to content

Instantly share code, notes, and snippets.

View miladvafaeifard's full-sized avatar
🙌
always looking simple solutions

Milad Vafaeifard miladvafaeifard

🙌
always looking simple solutions
View GitHub Profile
@miladvafaeifard
miladvafaeifard / uranus_compose_functional_programming.js
Last active February 12, 2019 15:53
compose and pipe functionalities
const uranus = {};
const trace = label => value => {
console.log(`${ label }: $ { value }`);
return value;
}
Object.defineProperty(uranus, 'compose', {
value: (...fns) => value => fns.reduceRight((g,f) => f(g), value),
configurable: false
@miladvafaeifard
miladvafaeifard / setState-like.js
Created February 9, 2019 22:46
a part of react-setState-like functionality only for count function
const setState = x => {
let _x = x;
return [
_x,
(newX) => {
_x += newX;
return _x;
}
];
}
@miladvafaeifard
miladvafaeifard / throttle_fn.js
Created February 6, 2019 22:02
Throttle function in javascript with the example
function throttle (func, delay, watch) {
watch && clearTimeout(watch);
return setTimeout(func, delay)
}
function callSearchApi(v) {
console.log(v);
}
@miladvafaeifard
miladvafaeifard / module_function.js
Created February 3, 2019 12:34
modular function in plain js
const ModuleA = (function traceA(){
function log(){
console.log(this.foo);
}
function bar(){
this.foo = 'bar';
log.call(this, null);
}
@miladvafaeifard
miladvafaeifard / requiredInputValue.ts
Created January 15, 2019 19:19
Angular make @input on directive required
export function Required(target: object, propertyKey: string) {
console.log('required ....');
Object.defineProperty(target, propertyKey, {
get () {
throw new Error(`Attribute ${propertyKey} is required`);
},
set (value) {
Object.defineProperty(target, propertyKey, { value, writable: true, configurable: true });
},
});
@miladvafaeifard
miladvafaeifard / iterator-pattern.ts
Last active January 13, 2019 01:29
Iterator pattern
export interface Iterator<T> {
next(): T;
hasNext(): boolean;
}
export interface Aggregator<T> {
createIterator(): Iterator<T>;
}
export class ConcreteIterator<IteratorType> implements Iterator<IteratorType> {
@miladvafaeifard
miladvafaeifard / high-order-func.js
Created January 3, 2019 21:29
js functional programming
const once = (fn) => {
return (...args) => {
fn && fn(...args);
fn = null
}
}
const thisManyTimes = (fn, limit) => {
return (...args) => {
if(limit > 0) {
@miladvafaeifard
miladvafaeifard / functional-composition.js
Last active January 3, 2019 14:47
functional compositions
//*********************** as libraries ***********************
const pipe = (...functions) => v => functions.reduce((y, f) => f(y), v)
const mapFn = f => arr => arr.map(f)
const trace = key => value => console.log(`${key} : ${value}`)
// TODO: need improvements
const expect = actual => ({
toBe: expected => {
if (Array.isArray(expected)) {
if (expected.length > 0 && expected.length === actual.length) {
#include <iostream>
#include <vector>
#include <algorithm>
#include <math.h>
enum class PointType {
cartesian,
polar
};
@miladvafaeifard
miladvafaeifard / abstract-class.cpp
Created December 28, 2018 14:58
Abstract class in c++
#include <iostream>
class Animal {
public:
Animal(std::string name): _name(name){}
~Animal(){
_name = "";
std::cout << "deleted animal\n";
}