Skip to content

Instantly share code, notes, and snippets.

View tlehman's full-sized avatar

Tobi Lehman tlehman

View GitHub Profile
@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;
@tlehman
tlehman / AtomicIntegerExample.java
Created November 3, 2017 20:28
Atomic integer multithreaded example
import java.util.concurrent.atomic.AtomicInteger;
public class AtomicIntegerExample {
public static void main(String args[]) throws InterruptedException {
AtomicInteger a = new AtomicInteger();
NonAtomicInteger n = new NonAtomicInteger();
Runnable r = () -> {
for(int j = 0; j < 100_000; j++) {
a.getAndIncrement();
@tlehman
tlehman / times.sh
Created September 22, 2017 21:34
times script
#!/bin/sh
# This is used in bitbar to display the time in the two main timezones I work in:
TZ=America/Los_Angeles date +"%Y-%h-%d %r (%Z)" | sed 's/:[0-9][0-9] / /g'
TZ=UTC date +"%Y-%h-%d %r (%Z)" | sed 's/:[0-9][0-9] / /g'