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 / 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()
}
// This file was initially generated by Windows Terminal 1.0.1401.0
// It should still be usable in newer versions, but newer versions might have additional
// settings, help text, or changes that you will not see unless you clear this file
// and let us generate a new one for you.
// To view the default settings, hold "alt" while clicking on the "Settings" button.
// For documentation on these settings, see: https://aka.ms/terminal-documentation
{
"$schema": "https://aka.ms/terminal-profiles-schema",
@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 / drivers.json
Created December 19, 2018 20:45
JSON-schema
{
"$schema": "./drivers.schema.json",
"series": "f1",
"season": 2018,
"driverList": [
{
"raceNumber": 14,
"code": "ALO",
"firstName": "Fernando",
"lastName": "Alonso",
@NickyMeuleman
NickyMeuleman / lib.rs
Created June 20, 2021 18:43
Exercism.io Sublist in Rust
use std::cmp::Ordering;
#[derive(Debug, PartialEq)]
pub enum Comparison {
Equal,
Sublist,
Superlist,
Unequal,
}
@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)
}