Skip to content

Instantly share code, notes, and snippets.

View tlehman's full-sized avatar

Tobi Lehman tlehman

View GitHub Profile
@tlehman
tlehman / generics.go
Last active February 21, 2022 05:24
package main
import (
"fmt"
)
func double[T int | float64](x T) T {
return T(2) * x
}
const items = [4,8,0,3,1,5,7,2,6,9];
console.log(items);
quicksort<number>(items);
console.log(items);
export function quicksort<T>(items: T[]): void {
quicksortSub(items, 0, items.length-1);
}
function quicksortSub<T>(items: T[], lo: number, hi: number) {
@tlehman
tlehman / threading.c
Created January 6, 2020 17:59
Minimal working example of using threads in C
#include <pthread.h>
#include <stddef.h>
#include <stdio.h>
void *entry_point_0(void * input) {
printf("Thread 0 started\n");
sleep(10);
printf("Thread 0 finished\n");
}
#!/usr/bin/env ruby
# display tree of json key names
# like tree(1) for JSON
# by @tlehman
require 'json'
input = STDIN.read || File.open(ARGV.first).read
parsed_input = JSON.parse(input)
@tlehman
tlehman / units.cpp
Created October 30, 2018 21:49
Units in C++ using user-defined literals
// Units in C++ with user-defined literals
// g++ units.cpp -std=c++11 && ./a.out
#include <assert.h>
#include <iostream>
using namespace std;
long double operator "" _km(long double x) {
return 0.6213725 * x;
}
@tlehman
tlehman / transformations.cpp
Created September 11, 2018 04:27
from ch2 of Elements of Programming
/** Chapter 2 of "Elements of Programming": Transformations
*/
#define DistanceType(F) int
#define Domain(F) int
#define pointer(T) *T
template<typename T>
class Transformation {
public:
@tlehman
tlehman / Address.java
Last active June 27, 2018 17:02
Build a SQL string from a Java object using reflection, and handling nulls gracefully
import lombok.Builder;
import lombok.Value;
@Builder
@Value
public class Address {
private String street1;
private String street2;
private String city;
private String cityCode;
@tlehman
tlehman / AddTwo.java
Created March 30, 2018 21:11
Java bytecode example
public class AddTwo {
public static void main(String args[]) {
int c = add(2,3);
}
public static int add(int a, int b) {
return a + b;
}
}
@tlehman
tlehman / StreamMax.java
Created November 21, 2017 19:45
Comparing run times of computing a max int using a loop, Java 8 streams and parallel streams
package com.tobilehman.benchmarks;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
public class StreamMax {
public static void main(String args[]) {
StreamMax sm = new StreamMax(10_000_000);
Long then,now;
@tlehman
tlehman / StreamMax.java
Created November 21, 2017 19:45
Comparing run times of computing a max int using a loop, Java 8 streams and parallel streams
package com.tobilehman.benchmarks;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
public class StreamMax {
public static void main(String args[]) {
StreamMax sm = new StreamMax(10_000_000);
Long then,now;