Skip to content

Instantly share code, notes, and snippets.

View NickyMeuleman's full-sized avatar
🏎️
When not coding, I'm simracing

Nicky Meuleman NickyMeuleman

🏎️
When not coding, I'm simracing
View GitHub Profile
@NickyMeuleman
NickyMeuleman / fromCSV.js
Created October 5, 2022 21:52
objects to CSV and CSV to objects
async function fromCSV({ ctx }) => {
const { filePaths } = await dialog.showOpenDialog({
filters: [{ name: "Comma seperated values", extensions: ["csv"] }],
});
const csv = fs.readFileSync(filePaths[0], { encoding: "utf-8" });
const lines = csv.split("\n");
const keys = lines.shift()?.split(",");
for (const line of lines) {
@NickyMeuleman
NickyMeuleman / index.jsx
Created August 22, 2022 15:38
Tailwind animate-ping inline in a button, play once
<button className="flex content-center items-center justify-center rounded-full border-2 border-transparent bg-sky-800 py-2 px-3 text-base leading-normal text-sky-100 outline-none hover:bg-opacity-80 focus:ring-1 focus:ring-offset-2 active:ring-0 active:ring-offset-0 disabled:cursor-not-allowed disabled:bg-opacity-50">
<span className="mx-1">Alle</span>
<span className="relative inline-flex">
<span className="mr-1 flex rounded-full bg-sky-500 px-2 py-1 text-xs font-bold uppercase">
{filtercount}
</span>
<span
className="absolute inline-flex h-full w-full animate-ping rounded-full bg-sky-400 opacity-75"
style={{
animationIterationCount: 1,
@NickyMeuleman
NickyMeuleman / index.html
Created June 4, 2022 20:00
File System Access API append to file
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<button id="choose-file">choose a file to write to</button>
@NickyMeuleman
NickyMeuleman / destructure_array.rs
Created July 22, 2021 15:23
Rust: accessing elements in vectors
use std::convert::TryFrom;
fn main() {
let names = vec!["Daniel", "Max", "Lewis"];
let [daniel, max, lewis] = <[&str; 3]>::try_from(names).ok().unwrap();
dbg!(daniel, max, lewis);
// daniel = "Daniel"
// max = "Max"
@NickyMeuleman
NickyMeuleman / main.rs
Created July 19, 2021 14:31
Rust enum impl block
#[derive(Debug)]
pub enum Bucket {
One,
Two,
}
impl Bucket {
fn other(&self) -> Self {
match self {
Bucket::One => Bucket::Two,
@NickyMeuleman
NickyMeuleman / lib.rs
Created July 17, 2021 18:15
alphametics
let permutations = (0u8..10u8).permutations(letters.len());
<!-- 105s -->
let possible_dicts: Vec<BTreeMap<char, u8>> = permutations
.filter_map(|permutation| {
let dict: BTreeMap<char, u8> = letters.iter().copied().zip(permutation).collect();
if dict.iter().any(|(c, &n)| leading.contains(c) && n == 0) {
None
} else {
Some(dict)
}
@NickyMeuleman
NickyMeuleman / lib.rs
Created July 8, 2021 15:34
exercism.io Rust nucleotide-count
use std::collections::HashMap;
const VALID: [char; 4] = ['A', 'C', 'G', 'T'];
pub fn count(nucleotide: char, dna: &str) -> Result<usize, char> {
if !VALID.contains(&nucleotide) {
return Err(nucleotide);
}
match dna.chars().find(|c| !VALID.contains(c)) {
Some(c) => Err(c),
@NickyMeuleman
NickyMeuleman / lib.rs
Created July 8, 2021 15:17
Rust .collect() an iterator into a HashMap
use std::collections::{HashMap, HashSet};
fn count_occurrences(message: &str) -> HashMap<char, usize> {
let unique: HashSet<char> = message.chars().collect();
unique
.iter()
.map(|&c| (c, message.matches(c).count()))
.collect()
}
@NickyMeuleman
NickyMeuleman / lib.rs
Created July 3, 2021 14:46
exercism.io isbn-verifier
/// Determines whether the supplied string is a valid ISBN number
pub fn is_valid_isbn(isbn: &str) -> bool {
let numbers: Vec<u32> = isbn
.chars()
.filter_map(|c| match c {
'X' if Some('X') == isbn.chars().last() => Some(10),
_ => c.to_digit(10),
})
.collect();
if numbers.len() != 10 {
@NickyMeuleman
NickyMeuleman / lib.rs
Created June 25, 2021 15:06
Check if a number is an Armstrong number
pub fn is_armstrong_number(num: u32) -> bool {
let digits: Vec<_> = num
.to_string()
.chars()
.filter_map(|n| n.to_digit(10))
.collect();
let num_digits = digits.len() as u32;
num == digits.iter().map(|n| n.pow(num_digits)).sum()
}