Skip to content

Instantly share code, notes, and snippets.

🎯
Focusing

Tony Brown anthonybrown

🎯
Focusing
Block or report user

Report or block anthonybrown

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View getFirstLast.js
function getFirstLast(array) {
const {
0: first,
length: len,
[len - 1]: last
} = array
return {first, last}
}
@anthonybrown
anthonybrown / App.js
Created Sep 3, 2019
React Cheat Sheet: --> Context, Refs, Memo, Lazy, Suspense
View App.js
// createContext
const WeatherContext = React.createContext({ day: 3 })
const App = ({ children }) => {
const [weather, setWeather] = React.useState(null)
const [error, setError] = React.useState(null)
React.useEffect(() => {
api.getWeather(...)
.then(setWeather)
.catch(setError)
}, [])
@anthonybrown
anthonybrown / bigArr.js
Created Sep 2, 2019
Shorter code isn't always better code.
View bigArr.js
const bigArr = new Array(1000000).fill(1).map((_, i) => i);
bigArr[100] = 5;
const allUnique = arr => {
const all = new Set();
for (let el of arr) {
if (all.has(el)) {
return false;
}
@anthonybrown
anthonybrown / learn-curry.js
Last active Aug 30, 2019
example of using curried functions
View learn-curry.js
/** Store Data */
const prices = {
game: 59.95,
nintendo: 399.99,
controller: 49.99,
xbox: 299.99,
ps1: 349.99
}
const basket = [
@anthonybrown
anthonybrown / curried.js
Last active Aug 30, 2019
A simple example of currying in functional JavaScript
View curried.js
function greetCurried(greeting) {
return function(name) {
if (typeof(greeting) !== 'string') {
return ('Greetings!')
} else if (typeof(name) !== 'string') {
return (greeting)
}
return (`${greeting}, ${name}`)
}
@anthonybrown
anthonybrown / add.js
Last active Aug 15, 2019
Using closures, higher order functions to have a stateful function
View add.js
function getAdd() {
let foo = 0;
return function() {
foo = foo + 1;
return foo;
};
}
const add = getAdd()
View symbolToPrimitive.js
class Decimal {
constructor(number) {
this.precision = 8
this.number = number
this.integer = Math.floor(number)
this.fractional = Math.round(number * Math.pow(10, this.precision))
}
[Symbol.toPrimitive](hint) {
// hint can be: string, number, default
@anthonybrown
anthonybrown / array_iteration_thoughts.md
Created Jul 20, 2019 — forked from ljharb/array_iteration_thoughts.md
Array iteration methods summarized
View array_iteration_thoughts.md

While attempting to explain JavaScript's reduce method on arrays, conceptually, I came up with the following - hopefully it's helpful; happy to tweak it if anyone has suggestions.

Intro

JavaScript Arrays have lots of built in methods on their prototype. Some of them mutate - ie, they change the underlying array in-place. Luckily, most of them do not - they instead return an entirely distinct array. Since arrays are conceptually a contiguous list of items, it helps code clarity and maintainability a lot to be able to operate on them in a "functional" way. (I'll also insist on referring to an array as a "list" - although in some languages, List is a native data type, in JS and this post, I'm referring to the concept. Everywhere I use the word "list" you can assume I'm talking about a JS Array) This means, to perform a single operation on the list as a whole ("atomically"), and to return a new list - thus making it much simpler to think about both the old list and the new one, what they contain, and

@anthonybrown
anthonybrown / commands.md
Last active Jun 20, 2019
Mac OS terminal commands
View commands.md

Terminal Cheatsheet for Mac (Basics)

Letters are shown capitalized for readability only. Capslock should be off.

SHORTCUTS

Key/Command Description
Ctrl + A Go to the beginning of the line you are currently typing on. This also works for most text input fields system wide. Netbeans being one exception
Ctrl + E Go to the end of the line you are currently typing on. This also works for most text input fields system wide. Netbeans being one exception
You can’t perform that action at this time.