Skip to content

Instantly share code, notes, and snippets.

Avatar

Aurélien aherve

View GitHub Profile
View LRUCache.go
// Go coding exercise: implement LRU Caching service
package main
func main() {}
type QueueNode struct {
next *QueueNode
prev *QueueNode
key string
}
View set-subtract.js
// subtract two sets
const s1 = [ 1, 2, 3, 4, 5 ]
const s2 = [ 2, 4 ]
const subtracted = s1.filter(x => s2.indexOf(x) < 0)
console.log(subtracted)
View fromPairs.js
// fromPairs
const pairs = [['a', 1], ['b', 2], ['c', 3]]
const asObjects = pairs
.reduce((res, [key, value]) => ({ ...res, [key]: value }), {})
// Or event smarter (thanks to @nomaed for pointing this one out)
const asObjects2 = { ...(new Map(pairs)) }
console.log(asObjects) // => { a: 1, b: 2, c: 3 }
View flatten.js
// flatten an array of arrays
const arrayOfArray = [ [1, 2], [3, 4], [[5, 6]] ]
const flattened = arrayOfArray.reduce((res, a) => [...res, ...a], [] )
console.log(flattened) // => [1, 2, 3, 4, [5, 6]]
View merge.js
// merge an array of objects
const data = [ {a: 1}, {b: 2}, {c: 3} ]
const merged = data.reduce((res, obj) => ({...res, ...obj}), {})
console.log(merged) // => { a: 1, b: 2, c: 3 }
// merge an array of objects by property
const toMerge = [
{ id: 1, value: 'a', },
{ id: 2, value: 'b', },
{ id: 3, value: 'c' },
View remove-by-property.js
// remove object from array by property
const removeId = 3
const without3 = initial.filter(x => x.id !== removeId)
console.log(without3) // => [ { id: 1, score: 1 }, { id: 2, score: 2 } ]
View update-by-property.js
// update an object in array by property
const initial = [ {id: 1, score: 1}, {id: 2, score: 2}, {id: 3, score: 4}]
const newValue = {id: 3, score: 3}
const updated = initial.map(x => x.id === newValue.id ? newValue : x)
console.log(updated) // => [ { id: 1, score: 1 }, { id: 2, score: 2 }, { id: 3, score: 3 } ]
View uniq.js
// get uniq from array
const numbers = [1, 2, 1, 1, 2, 1, 3, 4, 1 ]
const uniq = [...new Set(numbers)] // => [ 1, 2, 3, 4 ]
const uniq2 = Array.from(new Set(numbers)) // => [ 1, 2, 3, 4 ]
View pre-commit
#!/bin/bash
# Lunatic pre-commit hook
echo 'pre-commit hook starting'
FLIP=$(($(($RANDOM%10))%2))
if [ $FLIP -eq 1 ]
then
echo 'Okay, I will accept your commit'
exit 0
View docker-compose.yml
githook_installer:
build: .
dockerfile: 'Dockerfile.githooks'
volumes:
- ./.git:/tmp/.git
- ./hooks:/tmp/hooks