Skip to content

Instantly share code, notes, and snippets.

View berkus's full-sized avatar
🎯
Haptic drift

Berkus Decker berkus

🎯
Haptic drift
View GitHub Profile
@berkus
berkus / datadog.sh
Last active March 19, 2018 19:46
Track connected bluetooth devices Battery status
#!/bin/bash
# Format data suitable for DataDog metric and submit it
data=$(system_profiler -xml SPBluetoothDataType 2>/dev/null \
| plutil -extract 0._items.0.device_title xml1 -o - - \
| plutil -convert json -o - - \
| jq -c --arg hostname $(hostname) --arg timestamp $(date +%s) 'map(to_entries[0])
| map (.key as $k | .value | select(.device_isconnected=="attrib_Yes" and .device_ispaired=="attrib_Yes")
| { battery: .device_batteryPercent, name: $k })
| map({metric: "battery.level", tags: [.name], host: $hostname, points:[[($timestamp|tonumber), (.battery|rtrimstr("%")|tonumber)]]})
| {series: .}')
@berkus
berkus / Real World Specification.md
Created March 2, 2018 10:30 — forked from ForbesLindesay/Real World Specification.md
Functional Programming from the perspective of a JavaScript Programmer.

Real World Specification

(aka "Algebraic JavaScript Specification")

This project specifies the behavior of a number of methods that may optionally be added to any object. The motivation behind this is to encourage greater code reuse. You can create functions that just rely on objects having implementations of the methods below, and in doing so you can make them work with a wide variety of different, but related data structures.

Definitions

For the purposes of this, spec, an "entity" is an object that has an [[equivalent]] operation (see bellow) and may implement some or all of the other methods.

@berkus
berkus / Lifecycle.java
Created January 24, 2018 15:57
Application Backgrounding And Foregrounding Tracking
// Usually these are added as private fields somewhere in Application class
/**
* Handle application foregrounding.
*/
private final class LifecycleListener implements Application.ActivityLifecycleCallbacks {
private Application application;
LifecycleListener(Application app) {
application = app;
@berkus
berkus / spectre.c
Created January 6, 2018 02:26 — forked from ErikAugust/spectre.c
Spectre example code
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#ifdef _MSC_VER
#include <intrin.h> /* for rdtscp and clflush */
#pragma optimize("gt",on)
#else
#include <x86intrin.h> /* for rdtscp and clflush */
#endif
@berkus
berkus / tcp.cpp
Created September 29, 2017 11:31
Coroutines TS plus Networking TS
#include <ctime>
#include <iostream>
#include <string>
#include <experimental/net>
using net = std::experimental::net;
using net::ip::tcp;
std::string make_daytime_string() {
using namespace std; // For time_t, time and ctime;
@berkus
berkus / reverse-list.cpp
Created July 24, 2017 11:43
Single-linked list reverse
#include <iostream>
using namespace std;
struct Node {
Node(int value) : mValue(value), mNext(nullptr) {}
Node* mNext;
int mValue;
};
class List {
@berkus
berkus / vector_monad.hpp
Created July 17, 2017 12:33
vector monad without the ugly stuff
// See https://medium.com/@barryrevzin/the-vector-monad-in-c-really-without-the-ugly-stuff-3112137db5d7
// and https://wandbox.org/permlink/rctzpbPI1NnY0pBT
#include <vector>
#include <iostream>
#include <functional>
template <class F>
struct map_t {
F f;
@berkus
berkus / bugreports.md
Last active December 20, 2016 10:55
Reporting Twilio Android SDK bugs
  1. Enable debug log

    // Before creating a new ChatClient with ChatClient.create() add this line:
    ChatClient.setLogLevel(android.util.Log.DEBUG);
    ...
    
    ChatClient.create(....
    
@berkus
berkus / Jade.php
Last active December 3, 2015 23:31
How to use Talesoft/tale-jade with FlightPHP as a render engine
<?php
Flight::register('view', 'Tale\Jade\Renderer', [[
'paths' => [__DIR__.'/templates/'], // Where you keep your jade templates
'pretty' => true, // For debug only
'adapterOptions' => [
'path' => __DIR__.'/pages-cache/', // Where to cache generated files, writable by webserver
'lifeTime' => 3600
]
]], function($jade) {
// additional init code here, if needed
@berkus
berkus / dig.lua
Last active August 29, 2015 14:22
Minecraft turtle machinery
-- To load to turtle: pastebin get aKard6z3 dig
-- Simple digger --
-- When called, will dig down to bedrock, move one forward and dig back up to original location.
-- Call "dig <N>" - N times dig down and back up.
-- Can refuel from any slot.
-- Will group resources into same slot.
-- Will drop junk items if no free space.
--