Skip to content

Instantly share code, notes, and snippets.

View Mathspy's full-sized avatar
💭
Working on few big surprises! 🌸

Mathspy

💭
Working on few big surprises! 🌸
View GitHub Profile
@Mathspy
Mathspy / spotify-playlist-saver.js
Created June 24, 2018 22:08
Turns a Spotify playlist into an array for copying
Array.from(document.querySelectorAll(".track-name-wrapper")).map(node => {
let temp = {}
temp.name = node.firstElementChild.textContent
let artistAlbum = node.lastElementChild.querySelectorAll("a")
temp.artist = {name: artistAlbum[0].textContent, link: artistAlbum[0].href}
temp.album = {name: artistAlbum[1].textContent, link: artistAlbum[1].href}
return temp
})
@Mathspy
Mathspy / partial-youtube-playlist-time-calc.js
Last active June 29, 2018 21:27
Calculates the time remaining until one finishes a playlist if they have been watching videos linearly in it
Array.from(document.querySelectorAll("ytd-playlist-video-renderer"))
.slice(121) //Changes this number to the number of the last video you watched
.map(x => x.querySelector(".ytd-thumbnail-overlay-time-status-renderer").textContent.trim())
.reduce((acc, video) => {
let [h, m, s] = video.split(":").map(x => parseInt(x));
if (!s) {s = m; m = h; h = 0;}
acc[0] += h;
acc[1] += m;
acc[2] += s;
return acc;
@Mathspy
Mathspy / all_primes_iter.rs
Last active August 21, 2019 21:48
All the prime numbers your heart desires in Rust!
// Use this if you want [2, 3, 5.....]
use std::collections::HashMap;
/// This is a prime number generator based on [Sieve of Eratosthenes](https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes)
/// that can be _trivially_ improved by switching to [Sieve of Atkin](https://en.wikipedia.org/wiki/Sieve_of_Atkin) for extra speed
struct PrimeIterator {
current: u64,
composite_to_primes: HashMap<u64, Vec<u64>>,
}
@Mathspy
Mathspy / tuple_combinations_while.rs
Last active August 22, 2019 22:57
A Tuple Combination iterator like the one in Itertools but with short-circuiting
// This is the trait that has .tuple_combinations_while()
trait HasTupleCombinationWhile: Iterator {
/// Return an iterator adaptor that iterates over the combinations of the elements from an iterator.
/// With the added beneift of being able to short-circuit
fn tuple_combinations_while<P>(
mut self,
predicate: P,
) -> TupleCombinationsWhile<Self, Self::Item, P>
where
Self: Clone,
@Mathspy
Mathspy / debugging_timer.js
Created December 24, 2019 23:20
A utility for debugging JS timeouts
require('log-timestamp');
const timers = [];
function setTimeout(fn, time) {
const ret = global.setTimeout(() => {
console.log(`Finished timer ${timers.indexOf(ret)} after ${time}`);
fn();
}, time);
console.log(`Started timer ${timers.push(ret) - 1} with ${time}`);
@Mathspy
Mathspy / example_custom_serde_derive.rs
Created April 4, 2020 18:09
Example of how to derive a custom `serde` Deserialize
use serde::{Deserialize, de::{self, Deserializer, Visitor}};
use std::fmt;
#[derive(Debug)]
enum Shape {
Triangle = 3,
Rectangle = 4,
Pentagon = 5,
}
@Mathspy
Mathspy / i_can_write_unsafe_ma.rs
Last active April 24, 2020 11:52
My first sound unsafe code lol
use std::{
pin::Pin,
task::{Context, Poll},
};
use tokio::io::AsyncRead;
enum Either<T, U> {
A(T),
B(U),
}
@Mathspy
Mathspy / is_reader_ready.rs
Created May 10, 2020 12:48
Detect if a reader is ready to be read from
async fn is_reader_ready<R>(reader: &mut R) -> bool where R: AsyncRead + Unpin {
let mut me = &mut *reader;
poll_fn(|cx| {
let mut v = [];
Poll::Ready(Pin::new(&mut me).poll_read(cx, &mut v[..]).is_ready())
}).await
}
@Mathspy
Mathspy / dir_entry_test_util.rs
Created October 19, 2021 15:58
Get a breakdown of a directory
use std::{
collections::HashSet,
ffi::OsString,
fs,
hash::{Hash, Hasher},
path::Path,
};
#[derive(Debug, PartialEq, Eq)]
pub enum DirEntry {
@Mathspy
Mathspy / zero_alloc_html_rendere.rs
Last active November 19, 2021 09:10
Zero allocation HTML renderer with escaping
#![no_std]
extern crate alloc;
use alloc::borrow::Cow;
use core::iter;
enum Tag {
/// A non-HTML tag that renders into nothing for wrapping text
Fragment,