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
| function gcd(a, b) { | |
| while (b > 0) { | |
| [a, b] = [b, a % b] | |
| } | |
| return a; | |
| } |
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
| const arr = [1, 8, 3, 9]; | |
| for (const [i, element] of arr.entries()) { | |
| // i is the index | |
| // element is the array element | |
| } |
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
| import React from "react"; | |
| import ReactTestUtils from "react-dom/test-utils"; | |
| const PageLink = ({ page, onClick }) => ( | |
| <a href={`?page=${page}`} onClick={onClick}> | |
| Page {page} | |
| </a> | |
| ); | |
| // ReactTestUtils.renderIntoDocument doesn't work with functional components |
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
| function pipe (...funcs) { | |
| if (!funcs.length) { | |
| throw new Error('pipe requires at least 1 argument'); | |
| } | |
| return (...args) => funcs.slice(1).reduce((accum, func) => func(accum), funcs[0](...args)) | |
| } | |
| // Usage | |
| pipe((a, b) => a + b)(1, 2); // => 3 |
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
| const orderCounts = [3, 34, 15, 55, 6, 7]; | |
| const mean = sample => sample.reduce((sum, x) => sum + x, 0) / sample.length; | |
| const square = x => x * x; | |
| const difference = x => y => x - y; | |
| const variance = sample => mean(sample.map(difference(mean(sample))).map(square)); | |
| // Variance | |
| const σ2 = variance(orderCounts); | |
| // Standard deviation |
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
| <?xml version="1.0" encoding="UTF-8"?> | |
| <xsd:schema ...> | |
| <xsd:import | |
| namespace="http://www.google.com/schemas/sitemap-news/0.9" | |
| schemaLocation="./test/schemas/sitemap-news.xsd"/> | |
| ... | |
| </xsd:schema> |
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
| const assert = require('assert'); | |
| const fs = require('fs'); | |
| const libxmljs = require('libxmljs'); | |
| // Read the sitemap and schema from the file system | |
| // Could just as easily get these over HTTP | |
| const sitemap = fs.readFileSync('../sitemap.xml'); | |
| const schema = fs.readFileSync('./schemas/sitemap.xsd'); | |
| // Parse the sitemap and schema |
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
| nvm_global () { | |
| PKG_NAME=$1 | |
| VERSIONS=() | |
| I=0 | |
| for VER in $(nvm_ls) | |
| do | |
| nvm exec --silent ${VER} npm ls -g ${PKG_NAME} --depth=0 >/dev/null 2>&1 | |
| if [ $? -eq 0 ]; then |
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
| const {promisify} = require('util'); | |
| const child = require('child_process'); | |
| const exec = promisify(child.exec); | |
| exec('echo "Hello world"').then( | |
| result => console.log(result), // Prints { stdout: 'Hello world\n', stderr: '' } | |
| error => console.error(error) | |
| ); |
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
| const {promisify} = require('util'); | |
| const fs = require('fs'); | |
| const readFile = promisify(fs.readFile); | |
| // await can only be used within an async function | |
| (async () => { | |
| try { | |
| const fileContents = await readFile('./foo.txt'); | |
| console.log(fileContents.toString()); | |
| } catch (ex) { |
NewerOlder