Skip to content

Instantly share code, notes, and snippets.

View stevewadsworth's full-sized avatar

Steve Wadsworth stevewadsworth

View GitHub Profile
@stevewadsworth
stevewadsworth / QueryTree.ts
Last active December 11, 2021 16:01
Regex to match and|or with minimal match to left and maximal to right
// regex to match and|or with minimal match to left and maximal to right /^\s*(.+?)\s+(or|and)\s+(.+?)\s*$/
// foo = 192.168.0.1 or foo = 192.168.0.110 and bar = baz
// Gives "foo = 192.168.0.1", "or", "foo = 192.168.0.110 and bar = baz"
// The third match is ready to be re-input to the regex
// ...Or should it be the other way around with the maximal match on the left for correct left to right associativity?
// Not sure right now, so keep it in mind removing the ? from (.+?) will do it /^\s*(.+)\s+(or|and)\s+(.+?)\s*$/
//
// regex to match '=' /(\S+)\s*(=)\s*(\S+)/
// foo = 192.168.0.1
@stevewadsworth
stevewadsworth / throttle.java
Created February 28, 2019 17:41
Throttle update calls
@Override
public void update( final @Nullable OnResult<LeoRootObject> resultHandler )
{
// Throttle the update rate.
long currentTime = System.currentTimeMillis();
long timeToNextUpdate = MIN_TIME_BETWEEN_UPDATES - currentTime -_lastTimeUpdated;
if(timeToNextUpdate <= 0)
{
_lastTimeUpdated = currentTime;
super.update(resultHandler);
@stevewadsworth
stevewadsworth / Gradle.properties
Last active October 13, 2020 18:43 — forked from neyguvj/build.gradle
Set default build variant witn variant filter (workaround)
#.
# Add to the end of existing file
#.
#.
# By default this is not a CI environment
# To configure for CI either pass to the gradle command -PisCI=true or
# set ORG_GRADLE_PROJECT_isCI=true in the environment
isCI=false
@stevewadsworth
stevewadsworth / throttle.kt
Last active July 22, 2018 19:29
Throttle the rate at which a function is called to no more than the given rate
import android.os.Handler
/**
* Throttle the rate at which a function is called
*/
fun throttle(function: () -> Unit, rate: Long): () -> Unit {
var throttled = false
var called = false
fun emit() {
@stevewadsworth
stevewadsworth / Ping.kt
Created July 18, 2018 10:00
AsyncTask to ping a given IP Address
package com.naimaudio.discovery.helpers
import android.os.AsyncTask
import java.net.InetAddress
internal class Ping(val ipAddress: String, val timeout: Int = 100, val listener: (Boolean) -> Unit) :
AsyncTask<Unit, Unit, Boolean>() {
override fun doInBackground(vararg params: Unit?): Boolean {
val address: InetAddress = InetAddress.getByName(ipAddress)
@stevewadsworth
stevewadsworth / withEither.js
Last active July 18, 2018 10:04
withEither
/**
* withEither takes three parameters and returns a function.
* When the returned function is called a decision is made to call either the first or second functions.
*
* @param {function} funcTrue The function to call if choice returns true
* @param {function} funcFalse The function to call if choice returns false
* @param {function} choice The function which returns true or false depending on the input params
* @returns {function}
*/
const withEither = (funcTrue, funcFalse, choice) => {
const curryFn = function(fn, ...args) {
return function(...args2) {
return fn.apply(this, args.concat(args2));
};
};
@stevewadsworth
stevewadsworth / gist:d0f6584b5634855373d3a2a0cf0c53db
Created December 12, 2016 15:59
AWS API Gateway Mapping Template
{
"method": "$context.httpMethod",
"body" : $input.json('$'),
"headers": {
#foreach($param in $input.params().header.keySet())
"$param": "$util.escapeJavaScript($input.params().header.get($param))" #if($foreach.hasNext),#end
#end
},
"queryParams": {