Skip to content

Instantly share code, notes, and snippets.

View duarten's full-sized avatar
🤷‍♂️

Duarte Nunes duarten

🤷‍♂️
View GitHub Profile
@duarten
duarten / Combiner.java
Created October 25, 2016 19:48
Flat combining
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
public final class Combiner {
public interface Action {
public void apply();
}
private static final class Node {
@duarten
duarten / LICENSE
Last active July 16, 2022 08:31
Baze rule for 99designs/gqlgen
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
@duarten
duarten / bits.c
Created October 25, 2016 19:50
Bit manipulation
#include "stdio.h"
/*
* bitAnd - x&y using only ~ and |
* Example: bitAnd(6, 5) = 4
* Legal ops: ~ |
* Max ops: 8
* Rating: 1
*/
int bitAnd(int x, int y) {
@duarten
duarten / extension.rs
Created November 18, 2020 21:19
Rust AWS Lambda extension
use anyhow::{ensure, Result};
use reqwest::blocking::Client;
use reqwest::StatusCode;
use serde::Deserialize;
use serde_json::Value;
use std::collections::HashMap;
use std::env;
use std::fs;
use std::io::BufReader;
use std::time;
@duarten
duarten / meta-unique.ts
Last active September 19, 2020 05:44
Tuple type containing only unique values
type ElementOf<T> = T extends (infer E)[] ? E : never;
type TupleHead<Tuple extends readonly unknown[]> =
Tuple extends [infer HeadElement, ...readonly unknown[]] ? HeadElement : never;
type TupleTail<Tuple extends readonly unknown[]> =
Tuple extends [unknown, ...infer TailElements] ? TailElements : never;
type Unique<Tuple extends readonly unknown[]>
= Tuple extends []
@duarten
duarten / meta-permutations.ts
Created September 19, 2020 05:37
Compile-time permutations in TypeScript
type Access<Tuple extends readonly unknown[], I extends string> =
I extends keyof Tuple ? Tuple[I] : never
type Swap<Tuple extends readonly unknown[], I extends string, J extends string> = {
[Index in keyof Tuple]: Index extends I ? Access<Tuple, J> : Index extends J ? Access<Tuple, I> : Tuple[Index]
}
type Length<Tuple extends readonly unknown[]> = Tuple["length"] extends number ? `${Tuple["length"]}` : never
type PushFront<Tuple extends readonly unknown[], E> = [E, ...Tuple]
@duarten
duarten / false_sharing.c
Last active July 15, 2020 23:44
Example program to demonstrate false sharing between threads
/*
* This is an example program to demonstrate false sharing between threads.
*
* It can be compiled two ways:
* gcc -g false_sharing.c -Wall -pthread -lnuma -o false_sharing
* gcc -g false_sharing.c -Wall -pthread -lnuma -DNO_FALSE_SHARING -o no_false_sharing
*
* The -DNO_FALSE_SHARING macro reduces the false sharing.
*
* The usage is:
export function useAnimationDelay(
delay: number,
shouldShow: boolean,
onHide: () => void = () => {
return
},
): boolean {
const [display, setDisplay] = useState(shouldShow)
useEffect(() => {
@duarten
duarten / WcfHttpProcessorDsl.cs
Created February 25, 2011 00:15
An WCF HTTP configuration DSL
static IProcessorProvider GetProcessorProvider()
{
return new ProcessorProviderFor<TheService>()
.RemoveDefaultMediaTypeProcessors()
.OnAllOperations(_ => _
.Use(typeof(DataValidationProcessor))
.UseForRequests(typeof(RequestLoggingProcessor)))
.OnGetOperation(_ => _
.UseForResponses(typeof(ImageFromTextMediaProcessor),
typeof(WaveFromTextMediaProcessor)))
@duarten
duarten / HashMapBenchmark.java
Created October 30, 2014 18:33
Concurrent maps benchmarks
import java.util.HashMap;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
import scala.collection.concurrent.TrieMap;
import org.openjdk.jmh.annotations.*;
@BenchmarkMode(Mode.AverageTime)