Skip to content

Instantly share code, notes, and snippets.

View aykutyaman's full-sized avatar

Aykut Yaman aykutyaman

View GitHub Profile
[
{
"dashboard_id":"1",
"items": [
{
"node_id": "x1",
"title": "Pull Requests",
"data": [{
"data": 10,
"date": "2022-01-01"

inline strings used for app states instead of reusable constants/enums (”Idle”, “Loading”, "Act::Fetch" , etc).

strongly typed approach of the application and the usage of dicriminated union makes using constants unnecessary.

I will discuss it with three examples what I mean.

  1. We can't have silly typos and autocomplete of IDEs will predict them correctly too. So for example if we type "Load" the IDE will autocomplete as "Loading". And when we type something that is not part of the union the compiler will alert us in that spot. Screen shot
  2. If a naming change occurs we are also forced to handle the change. So, it means we don't have the possibility to skip renaming where they are used.
const compose = (...args) => n => args.reverse().reduce((acc, f) => f(acc), n);
var mapReducer = fn => combineFn => (acc, v) => combineFn(acc, fn(v));
var filterReducer = fn => combineFn => (acc, v) => fn(v) ? combineFn(acc, v) : acc;
var add1 = n => n + 1;
var isOdd = n => n % 2 === 1;
var sum = (acc, n) => Number(acc) + n;
var list = [1, 3, 4, 6, 9, 12, 13, 16, 21];
/*** FIX ***/
var fix = f => x => f(fix(f))(x);
/*** TRAMPOLINE ***/
const rec = (...args) => ({ rec: true, args });
const ret = x => ({ rec: false, x });
var trampoline = f => (...args) => {
@aykutyaman
aykutyaman / trampoline.js
Created January 24, 2020 15:07
proper tail call with trampoline in javascript
var trampoline = fn => (n) => {
// make sure we call trampoline function only once
var result = fn(n);
while (typeof result === 'function') {
result = result();
}
return result;
@aykutyaman
aykutyaman / server-cors.js
Created January 25, 2019 10:57
Simple node.js server example with cors enabled
const http = require('http')
const Countries = [
'Turkey', 'Italy', 'Germany'
]
const app = http.createServer((req, res) => {
res.setHeader("Access-Control-Allow-Origin", "*");
res.writeHead(200, { 'Content-Type': 'application/json' })
res.end(JSON.stringify(Countries));
[
'Ankara',
'Milano',
'Berlin'
]
@aykutyaman
aykutyaman / short.js
Created January 22, 2019 14:07
short circuit reduce
const sumUntil = (a, target) =>
[...a].reduce((acc, n, _, arr) => n === target ? (arr.length = 0, acc) : acc + n)
sumUntil([1, 2, 3, 36, 4], 36) // 6
@aykutyaman
aykutyaman / palindrome.js
Last active January 21, 2019 13:51
Recursive palindrome
// it works only with strings
const isPalindrome = ([head, ...tail]) => {
if (tail.length === 0) return true
if (head !== tail[tail.length - 1]) return false
return isPalindrome(tail.slice(0, tail.length - 1))
}
@aykutyaman
aykutyaman / isValidDate.js
Created December 20, 2018 12:52
check if a date is valid or not
// isValid -> Date -> Bool
const isValidDate = d => d instanceof Date && !isNaN(d.getTime())