Skip to content

Instantly share code, notes, and snippets.

View Xetera's full-sized avatar

Xetera Xetera

View GitHub Profile
@Xetera
Xetera / chunk.js
Created March 24, 2019 06:00
Method that chunks an array of n elements into sub-arrays of max y elements
const chunk = (array, num) => array.reduce((array, current) => {
const last = array[array.length - 1];
const init = array.slice(0, -1);
if (!last) {
return [...init, [current]]
}
if (last.length >= num) {
return [...array, [current]]
}
return [...init, [...last, current]];
@Xetera
Xetera / game_of_googol.hs
Last active April 1, 2019 07:59
A haskell simulation implementing Optimal Stopping Theory for the math game, game of googol.
{-
Using Optimal Stopping for finding the highest number in a list
1. Generate an integer list of N size
2. Reveal the numbers of the first N/e elements
3. Keep revealing numbers until you find one bigger than the biggest revealed number
4. The chances of the first matching number being the highest number in the list will consistently be 1/e
-}
-- Games played: 1000
-- Games Won: 358
@Xetera
Xetera / PatternGenerator.hs
Created May 31, 2019 04:52
A simple function for generating an infinite list of alternating patterns
makePattern :: [(a -> a)] -> a -> [a]
makePattern funcs seed = scanl (\a f -> f a) seed (cycle funcs)
-- makePattern [(*3), (+1)] 1
-- [1,3,4,12,13,39]
@Xetera
Xetera / keybase.md
Created July 21, 2019 00:36
Keybase

Keybase proof

I hereby claim:

  • I am xetera on github.
  • I am xetera (https://keybase.io/xetera) on keybase.
  • I have a public key whose fingerprint is DE0C C5B3 7058 805B 8DD8 E306 7FB1 9FBF D8E5 BBED

To claim this, I am signing this object:

@Xetera
Xetera / Stack.hs
Created July 26, 2019 02:21
Basic stack implementation in haskell
data Stack a
= Nil
| Stack a (Stack a)
instance Functor Stack where
fmap f s =
case s of
Stack e next ->
Stack (f e) $ fmap f next
Nil -> Nil
@Xetera
Xetera / route_methods.js
Created August 29, 2019 09:03
A helper function that lets you declare multiple methods for the same endpoint in express.js without repeating things
const routeMethods = (router, url) => {
const unwrap = () => router
const makeRoute = method => handler => {
router[method](url, handler);
return routeMethods(router, url)
}
const methods = ['get', 'post', 'put', 'options', 'delete', 'patch', /* as many methods as you need here */]
return methods.reduce((all, method) => ({
...all,
[method]: makeRoute(method)
@Xetera
Xetera / tailwind-colors.css
Created October 27, 2020 18:27
Tailwind 2.0 colors as CSS variables for use outside of tailwind
:root {
--rose-50: #fff1f2;
--rose-100: #ffe4e6;
--rose-200: #fecdd3;
--rose-300: #fda4af;
--rose-400: #fb7185;
--rose-500: #f43f5e;
--rose-600: #e11d48;
--rose-700: #be123c;
--rose-800: #9f1239;
@Xetera
Xetera / restic_backup.sh
Created March 15, 2021 18:34
Backing up mongodb with restic to an S3 bucket
#!/usr/bin/env bash
# This backup file is run daily as a cronjob like:
# 0 0 * * * bash ~/backup.sh >> /var/log/restic_backups.log 2>&1
restic_repository="your-s3-repository-url-here"
# There is probably a way to do this conveniently through stdin but I don't know if
# that's more efficient through a file because mongodb does some questionable stuff
# with piping mongodump to stdout vs saving backups to a file
backup_path="/path/to/emit/backup/file"
@Xetera
Xetera / nested-find.js
Created March 29, 2021 21:31
Return a nested subdocument inside an array field from a mongodb query
// Aggregates are a powerful mongodb tool that allows us to craft complex queries.
// In this case it helps prevent repetition in a situation where we would have to
// query a document with a filter, then use a duplicate `Array.find` logic to
// extract the subdocument we were already expecting to find in the first place.
// Example person document
{
name: "jason",
pets: [{
type: "dog",