Skip to content

Instantly share code, notes, and snippets.

View aykutyaman's full-sized avatar

Aykut Yaman aykutyaman

View GitHub Profile
@aykutyaman
aykutyaman / combineReducers.js
Created November 20, 2018 13:14
redux combineReducer function
const combineReducers = reducers => (state = {}, action) =>
Object.keys(reducers).reduce((acc, key) => ({
...acc,
[key]: reducers[key](state[key], action)
}), {})
const sum = ([x, ...xs]) => (
xs.length === 0
? x
: x + sum(xs)
);
console.log(sum([2, 4, 6])); // 12
const count = ([x, ...xs]) => (
xs.length === 0
? 1
const LinkedList = () => {
let length = 0;
let headNode = null;
let Node = (element) => ({
element,
next: null,
});
let size = () => length;
const express = require('express');
const xl = require('excel4node');
const app = express();
const makeReport = (request, response) => {
const wb = new xl.Workbook();
const ws = wb.addWorksheet('Ödenmiş Mesai Raporu');
// Create a reusable style
// reverse a string using stack data structure
const reverse = s => {
let stack = [];
let reversed = '';
// push all characters of string to stack
for (var i = 0; i < s.length; i++) {
stack.push(s[i]);
}
const isOpening = (s, m) => m[s];
const isClosing = (s, m) => (
Object.values(m).includes(s)
);
const isValid = (last, s, m) => (
m[last] !== s
);
const balancedParens = s => {
const mapping = {
/**
* Complexity Analysis
* push: O(1)
* pop: Amortized O(1), Worst-case O(n)
* empty: O(1)
* peek: O(1)
*/
function Queue() {
var stack1 = [];
var stack2 = [];
class Child extends React.Component {
greeting() {
return 'hello world';
}
}
class Parent extends React.Component {
render() {
return (
<TheChild ref='foo' />
)
@aykutyaman
aykutyaman / cached_computation.js
Created August 23, 2016 13:30
This goes against the "single source of state" principle, but if computations on a prop are expensive you can cache them on the component. Instead of directly using doExpensiveComputation(this.prop.someProp) in the render method, we can wrap the call that caches the value if the prop is unchanged
getCachedExpensiveComputation() {
if (this._cachedSomeProp !== this.prop.someProp) {
this._cachedSomeProp = this.prop.someProp;
this._cachedComputation = doExpensiveComputation(this.prop.someProp);
}
return this._cachedComputation;
}
@aykutyaman
aykutyaman / two_components.jsx
Created August 23, 2016 13:27
refactor out <ul> into its own subcomponent that takes in this.props.items, and only update if this.props.items changes
<div>
<CustomList items={this.props.items} />
<ComplexForm props={this.props.complexFormProps} />
</div>