Skip to content

Instantly share code, notes, and snippets.

@nouex
nouex / functional-programming-concepts.md
Last active March 24, 2024 18:35
idempotency, determinism, and pure functions

side effect - modifying state outside the local scope e.g. modifying a global variable, modifying a mutable argument passed by reference, performing I/O

idempotent - the effect of calling a function multiple times is the same as calling it once

  • no side effects

deterministic - the effect of calling a function with the same input multiple times aleways produces the same output

  • may or may not have side effects

pure: deterministic and no side effects

@nouex
nouex / hyrums-law.md
Last active February 2, 2026 09:07
Hyrum-Proof APIs

Hyrum's Law in React

I was reading this and it got me thinking about Hyrum's Law. In particular it got me thinking about Hyrum's Law being applied to React.useMemo()

In the docs for useMemo() there is a section at the bottom with caveats, one of them is

React will not throw away the cached value unless there is a specific reason to do that. For example, in development, React throws away the cache when you edit the file of your component. Both in development and in production, React will throw away the cache if your component suspends during the initial mount. In the future, React may add more features that take advantage of throwing away the cache—for example, if React adds built-in support for virtualized lists in the future, it would make sense to throw away the cache for items that scroll out of the virtualized table viewport.

It's interesting that the docs would make a disclaimer about possible future behavioral changes. T

@nouex
nouex / react-optimizations.md
Last active March 22, 2024 21:16
Performance optimizations in react apps

Performance Optimizations in React Apps

Avoid unecessary re-renders

React.memo()

Use it for all components, except when the overhead might outweigh the benefits, e.g. + computation is not expensive + the array dependency changes often

Provide a custom compare if the default compare does not suite your needs - by default React.memo() does a shallow compare

@nouex
nouex / rename-media-using-creation-date.mjs
Last active January 19, 2024 01:58
update file name and "create date" tag using exiftool
/**
MOV - video/quicktime - Creation Date (verified: 1)
MP4 - video/mp4 - Media Create Date [among other candidates] (verified: 1)
PNG - image/png - Date Created [among other candiddate] (verified: 4)
jpeg - image/jpeg - Create Date [among other candiates] (verified: 2)
JPG - image/jpeg - Create Date [among other candidate] (verified: 2)
iterate thru every file and
/**
* @param {number[]} nums
* @return {number[]}
*/
// Time Complexity: O(n log n) best case input, O(n^2) with worst case input
// Space Complexity: O(1) since we sort in-place
var sortArray = function(nums) {
quicksort(0, nums.length)
@nouex
nouex / mergesort.js
Created June 23, 2021 16:11
Implementation of merge sort. Submitted on LeetCode https://leetcode.com/problems/sort-an-array/submissions/
/**
* @param {number[]} nums
* @return {number[]}
*/
// O(n log n) Time Complexity
// O(n) Space Complexity
var sortArray = function(nums) {
mergeSort(0, nums.length)
const db = require('firebase-admin').database()
const normalizePhone = require('phone');
const validator = require("email-validator");
// const offerValueHooks = require("../offer-value-hooks");
const redeemPromo = (req, res) => {
const { formName } = req.body
const code = req.body.code
@nouex
nouex / audiogram.sh
Created August 7, 2020 03:05
Example POST request
curl --location --request POST 'https://callcast-staging.co/audiogram' \
--header 'Accept: application/json' \
--form 'template=square' \
--form 'audio=Mixing/1837ddbc-8269-4d62-9d0d-c1e6cce20136-0FD2C2AB-D513-4CED-9A8A-D485B0C892AA.mp3' \
--form 'backgroundColor=#ffe5b4' \
--form 'artwork=@/Users/amauri/Desktop/callcast/backend/public/assets/audiogram-artwork.jpg' \
--form 'waveformColor=#000000' \
--form 'text=Yeah!' \
--form 'includeWatermark=false' \
--form 'uid=xC9o0sOwYbgHBBtxDLIgoNrrpD73'
/**
* recursion without recursion
* https://web.archive.org/web/20090531163555/http://www.jslab.dk/articles/non.recursive.preorder.traversal.part4
*/
function postorderTraversalNonRecursive(root) {
var n = root;
while(n) {
// Find first left most leaf on a path which have not
// yet been visited
@nouex
nouex / functional.js
Created June 27, 2017 02:47
functional fun
const assert = require('assert');
(function () {
// currying closure style
// add(a)(b)
let add = curry2((l, r) => l + r)
assert.strictEqual(add(2)(3), 5)
function curry2(fn) {