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 / functionAsChild.md
Last active December 18, 2018 08:57
Function as Child pattern gaining consensus within the React community.

Function As Child in React JS

The main concept is that, instead of passing a child in the form of a component, we define a function that can receive parameters from the parent. Let's see what it looks like:

const FunctionAsChild = ({ children }) => children()

FunctionAsChild.propTypes = {
@miladvafaeifard
miladvafaeifard / lambda-calculator.cpp
Created December 26, 2018 23:08
my first lambda use in c++
#include "pch.h" // for visual studio usage
#include <iostream>
#include <functional>
class Calculator {
public:
Calculator(int num) : _baseNum(num) {};
int add(int a, int b) {
@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";
}
#include <iostream>
#include <vector>
#include <algorithm>
#include <math.h>
enum class PointType {
cartesian,
polar
};
@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) {
@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 / 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 / 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 / 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 / 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);
}