Skip to content

Instantly share code, notes, and snippets.

using System;
using System.Collections.Generic;
using System.Linq;
public static class ContainerExtensions
{
public static IEnumerable<(int, T)> Enumerate<T>(this IEnumerable<T> enumerable)
{
int index = 0;
foreach (T v in enumerable)
@mattiasflodin
mattiasflodin / ranges_intersect.py
Created June 19, 2018 11:04
Check if two ranges intersect
# Check overlap of range [a1, a2) and range [b1, b2), where None indicates infinity.
def ranges_intersect(a, b):
# Make sure that a starts before or at b.
if b[0] is None or (a[0] is not None and b[0] < a[0]):
a, b = b, a
# If b starts before a ends, then there is an intersection.
return b[0] is None or a[1] is None or b[0] < a[1]
@mattiasflodin
mattiasflodin / TaskExtensions.cs
Created June 28, 2018 13:26
Extensions for dealing with async tasks
using System;
using System.Threading;
using System.Threading.Tasks;
public static class TaskExtensions
{
// Forward result of task if it completes before given timeout. Otherwise,
// return default(T). The original task will remain running until it completes.
public static async Task<T> TimeoutAfter<T>(this Task<T> task, TimeSpan timeout)
{
@mattiasflodin
mattiasflodin / källare.yaml
Last active October 31, 2020 11:28
Automatiseringar
choose:
- conditions:
- condition: or
conditions:
- condition: state
entity_id: input_select.phase
state: Sova
- condition: state
entity_id: input_select.phase
state: Borta
@mattiasflodin
mattiasflodin / PersonIdentity.cs
Created December 16, 2020 22:05
C# class for representing and validating Swedish social security numbers
using System;
using System.Linq;
using System.Text.RegularExpressions;
// Class for dealing with person identity numbers. There are three
// overall types of identities for a person:
// - Personnummer. This is the standard number assigned to any person
// who is accepted as a Swedish citizen. It's unique and kept for life.
// - Samordningsnummer. This is assigned to someone who is a
// registered visitor/occupant in Sweden and needs an identifier.
@mattiasflodin
mattiasflodin / Exceptions.java
Created July 8, 2021 09:38
Utility to wrap checked exceptions into unchecked exceptions
import java.io.IOException;
import java.io.UncheckedIOException;
public class Exceptions {
private Exceptions()
{
}
private static class WrappedCheckedException extends RuntimeException {
public static final long serialVersionUID = 1L;
use cgmath::prelude::*;
use cgmath::{vec2, vec3, ElementWise, EuclideanSpace, InnerSpace, SquareMatrix};
use crossbeam::thread;
use image::{ImageBuffer, RgbImage};
use reduce::Reduce;
use std::cmp::{PartialOrd};
use std::f64;
use rand::Rng;
use std::sync::Mutex;