Skip to content

Instantly share code, notes, and snippets.

Avatar
🏎️
When not coding, I'm simracing

Nicky Meuleman NickyMeuleman

🏎️
When not coding, I'm simracing
View GitHub Profile
@NickyMeuleman
NickyMeuleman / destructure_array.rs
Created Jul 22, 2021
Rust: accessing elements in vectors
View destructure_array.rs
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 Jul 19, 2021
Rust enum impl block
View main.rs
#[derive(Debug)]
pub enum Bucket {
One,
Two,
}
impl Bucket {
fn other(&self) -> Self {
match self {
Bucket::One => Bucket::Two,
View lib.rs
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 Jul 8, 2021
exercism.io Rust nucleotide-count
View lib.rs
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 Jul 8, 2021
Rust .collect() an iterator into a HashMap
View lib.rs
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 Jul 3, 2021
exercism.io isbn-verifier
View lib.rs
/// 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 Jun 25, 2021
Check if a number is an Armstrong number
View lib.rs
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()
}
@NickyMeuleman
NickyMeuleman / index.js
Created Jun 22, 2021
JavaScript URL and URLSearchParams
View index.js
async function getReleaseInfo({ track = "", artist = "" }) {
let url = new URL("https://api.discogs.com/database/search");
let searchParams = new URLSearchParams();
searchParams.append("key", process.env.DISCOGS_CONSUMER_KEY);
searchParams.append("secret", process.env.DISCOGS_CONSUMER_SECRET);
searchParams.append("per_page", 10);
if (artist.length) {
searchParams.append("artist", artist);
}
if (track.length) {
@NickyMeuleman
NickyMeuleman / lib.rs
Created Jun 21, 2021
Exercism.io Rust, proverb
View lib.rs
// adding one item to an iterator by calling .chain() with an argument that implements IntoIterator
pub fn build_proverb(list: &[&str]) -> String {
match list.is_empty() {
true => String::new(),
false => list
.windows(2)
.map(|window| format!("For want of a {} the {} was lost.", window[0], window[1]))
.chain(
// first() returns an Option which implements IntoIterator and can be .chain()ed to an other iterator
list.first()
@NickyMeuleman
NickyMeuleman / lib.rs
Created Jun 20, 2021
Exercism.io Sublist in Rust
View lib.rs
use std::cmp::Ordering;
#[derive(Debug, PartialEq)]
pub enum Comparison {
Equal,
Sublist,
Superlist,
Unequal,
}