This file contains 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
#include "stdlib.h" | |
#include "stdio.h" | |
#include "string.h" | |
/* | |
* Returns a string containing each argument separated by a newline. | |
*/ | |
char* parse_args(int argc, char** argv) { | |
int char_count = 0; | |
for (int i = 1; i < argc; i++) { |
This file contains 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
#!/bin/zsh | |
if (($# != 2)); then | |
echo | |
echo "This script breaks the given PDF file into a set of PDf files," | |
echo "each with block_size pages." | |
echo | |
echo "Usage:" | |
echo " extract_pages.sh filename block_size" | |
echo |
This file contains 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
# Computes the value of pi by iteratively evaluating the infinite product: | |
# 4 * (8/9) * (24/25) * (48/49) * (80/81) * (120/121) * (168/169) * ... | |
# Each line of output shows the last denominator considered so far, and | |
# the value of the finite product at that point. | |
# | |
# As featured by Matt Parker: https://www.youtube.com/watch?v=8pj8_zjelDo | |
from decimal import * | |
def converge(): |
This file contains 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
#!/bin/zsh | |
i=0 | |
while { true }; do | |
echo $((++i)): | |
$@ | |
read | |
done |
This file contains 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 traverseTreeInOrder(tree) { | |
// In the real world, we'd need a more robust solution of storing | |
// and looking up object IDs. But JavaScript doesn't make that easy. | |
const visitedNodes = {}; | |
const stack = [ tree ]; | |
while (stack.length > 0) { | |
let node = stack.pop(); | |
if (!node) { |
This file contains 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
#include <stdio.h> | |
#define ARR_SIZE 9 | |
void swap(int* a, int* b) { | |
if (a == b) { | |
return; | |
} | |
*a = *a ^ *b; |
This file contains 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
#!/usr/bin/env node | |
const process = require('process'); | |
const num = parseInt(process.argv[2], 10); | |
const CompositeRegex = /^(..+)\1+$/; | |
function isPrime(num) { | |
const str = new Array(num + 1).join(' '); | |
return !CompositeRegex.test(str); |
This file contains 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 { useEffect, useState } from 'react'; | |
import { StateMachine } from 'xstate'; | |
import { useMachine } from '@xstate/react'; | |
import { inspect } from '@xstate/inspect'; | |
let iFrameElement: HTMLIFrameElement | null = null; | |
function createIFrame() { | |
if (!iFrameElement) { | |
const iframe = document.createElement('iframe'); |
This file contains 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
/** | |
* Higher Order Component that renders the wrapped component with the given key (derived from its | |
* props), which allows us to easily "reset" a component if certain props change. | |
* @param component | |
* @param getKey | |
*/ | |
export function KeyOn<P>(component: React.ComponentType<P>, getKey: (props: P) => string) { | |
const displayName = `KeyOn(${component.displayName})`; | |
const hoc = (props: P) => { | |
const key = getKey(props); |
This file contains 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 * as React from 'react'; | |
export type AsyncFetchState<T, E = unknown> = { | |
data: T | null; | |
loading: boolean; | |
error: E | undefined; | |
}; | |
/** | |
* This hook performs an async operation and stores its result in component state. It protects |
OlderNewer