Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View berkes's full-sized avatar
🌱
open to work

Bèr Kessels berkes

🌱
open to work
View GitHub Profile
@berkes
berkes / main.rs
Created January 27, 2024 19:16
"Benchmarking" some micro optimizations: strange behaviour.
use std::time::Instant;
fn main() {
let iterations = 1_000_000;
let mut odd_count = 0;
// Using modulus
let start_modulus = Instant::now();
for i in 0..iterations {
if i % 2 != 0 {
@berkes
berkes / python_iterators.py
Created February 9, 2024 12:18
One-line to multiline block iterators
from itertools import chain, repeat
# Define the inventory as a dictionary
inventory = {
"banana": 3,
"apple": 2,
"pear": 1,
"berry": 4,
}
<div class="block content has-background-light p-4">
<strong>Discussions around the web:</strong>
<div id="comment" data-discussion-url="https://www.joelonsoftware.com/2000/04/06/things-you-should-never-do-part-i">
<ul></ul>
</div>
<p class="pt-2">Powered by <a href="https://discu.eu">discu.eu</a>.</p>
</div>
@berkes
berkes / final.rs
Last active October 4, 2022 13:30
mod domain {
use std::{fmt::Display, ops::Deref};
#[derive(Debug)]
pub enum DomainError {
InvalidEmailAddress,
InvalidResponseCode,
}
#[derive(Clone, PartialEq)]
@berkes
berkes / main.rs
Created October 4, 2022 09:56
ViewCount With Sum
mod domain {
use std::ops::Deref;
pub struct ViewCount { value: usize }
impl ViewCount {
pub fn new(value: usize) -> Self {
Self { value }
}
}
struct Point {
x: usize,
y: usize
}
@berkes
berkes / main.rs
Created October 4, 2022 09:55
ViewCount Example
mod domain {
pub struct ViewCount {
value: usize,
}
impl ViewCount {
pub fn new(value: usize) -> Self {
Self { value }
}
pub fn value(&self) -> usize {
self.value
impl PageSize {
pub fn new_from_kilobytes(value: usize) -> Result<Self, DomainError> {
Ok(Self { value })
}
pub fn as_bytes(&self) -> usize {
&self.value / 1_000
}
pub fn as_kilobytes(&self) -> usize {
self.value
}
@berkes
berkes / report_to.rs
Created October 4, 2022 09:53
Using Email Eq.
fn make_and_send_report(to: EmailAddress, pages: Vec<PageRequest>) {
if to == EmailAddress::new("john@example.com".to_string()).expect("valid email") {
return;
}
//...
}
#[derive(PartialEq)
pub struct EmailAddress {
//...
}