Last active
February 6, 2021 14:50
-
-
Save kiote/b83b68892e50dbf89ae8358b3449fc6b to your computer and use it in GitHub Desktop.
stateless vs stateful example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// stateful vs stateless | |
let x = 1; | |
const stateful = () => x = x + 1; | |
stateful(); // x = 2 | |
stateful(); // x = 3 | |
const stateless = (x) => x + 1; | |
stateless(x); // x = 4 | |
stateless(x); // x = 4 | |
// imperative vs declarative | |
// https://dzone.com/articles/imperative-vs-declarative-javascript | |
const isSubarray = (needle=[], haystack=[]) => { | |
for (let i=0; i < needle.length; i++) { | |
if (haystack.indexOf(needle[i]) === -1) { | |
return false; | |
} | |
} | |
return true; | |
} | |
const isSubarray = (needle=[], haystack=[]) => { | |
return needle.every(el => haystack.includes(el)); | |
} | |
// functions as first-class citizens | |
const withLossLimitsCheck = (callback: () => any, errorHandler: () => any) => { | |
const checkLimitResult = Limits.RollingLimits.checkLimit( | |
... | |
); | |
if (!checkLimitResult) return errorHandler(); | |
return callback(); | |
} | |
withLossLimitsCheck(() => | |
Banking.FiatApi.processDeposit({ | |
... | |
}, | |
Banking.FiatApi.handleError({ | |
... | |
}) | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment