Skip to content

Instantly share code, notes, and snippets.

@Dierk
Dierk / Either.groovy
Created January 20, 2017 09:41
Either type in Groovy as a special implementation of the generic sum type (works the same in Java)
// Either type in Groovy as a special implementation of the generic sum type
// see: Phil Wadler at LambdaWorld 2016: youtu.be/V10hzjgoklA
import java.util.function.*
import groovy.transform.*
interface Either<A,B> {
public <C> C match (Function <A,C> leftCase, Function <B,C> rightCase)
}
@srdjan
srdjan / 100+ different counter apps...
Last active May 6, 2024 05:13
100+ different js counter apps...
100+ different js counter apps...
@say2joe
say2joe / JS-Quiz-Solutions.js
Last active February 25, 2021 05:59
My solutions to former employer's JS quiz for new employees
// Please save all your work in https://jsfiddle.net/
// Return the link to your work
// Answer five out of the seven questions
// Use test data when provided, however your functions should work with any similiarly structured data
// Use any additional libraries you like
// 1: Refactor this function to make it scalable.
var goto = function(evt, where, project, scenario, item, id){
if (evt && evt.stopPropagation){ evt.stopPropagation(); }
@crazy4groovy
crazy4groovy / promises-try-all-async.js
Last active October 11, 2018 16:10
A strategy to try a list of promises, and then check which ones were resolved (or rejected).
var promises = [];
var cnt = 0;
function makePromises() {
console.log('make', Date.now())
promises.length = 0; // clear array
promises.push(new Promise((resolve, reject) => setTimeout(resolve, 1000, `Everything OK in Promise ${cnt++}`)));
promises.push(new Promise((resolve, reject) => setTimeout(resolve, 2000, `Everything OK in Promise ${cnt++}`)));
promises.push(new Promise((resolve, reject) => reject(new Error(`Something went wrong in Promise ${cnt++}`))));
return promises;
@crazy4groovy
crazy4groovy / ec2_spot_prices.js
Last active March 6, 2017 22:15
Parse EC2 spot prices; store in an elasticsearch DB as a timeseries dataset.
require('http').globalAgent.maxSockets = 10
setInterval(main, (5 * 60 * 1000) - 6) // rinse-and-repeat every 5 minutes
main() // kick start!
function main () {
const req = require('request-promise')
const co = require('co')
const oO = require('eyes').inspector({maxLength: 99999})
@bendc
bendc / easing.css
Created September 23, 2016 04:12
Easing CSS variables
:root {
--ease-in-quad: cubic-bezier(.55, .085, .68, .53);
--ease-in-cubic: cubic-bezier(.550, .055, .675, .19);
--ease-in-quart: cubic-bezier(.895, .03, .685, .22);
--ease-in-quint: cubic-bezier(.755, .05, .855, .06);
--ease-in-expo: cubic-bezier(.95, .05, .795, .035);
--ease-in-circ: cubic-bezier(.6, .04, .98, .335);
--ease-out-quad: cubic-bezier(.25, .46, .45, .94);
--ease-out-cubic: cubic-bezier(.215, .61, .355, 1);
@ericelliott
ericelliott / nstime.js
Created May 14, 2016 09:16
Convert Node's process.hrtime() return values to nanoseconds
const nsTime = (hrtime) => hrtime[0] * 1e9 + hrtime[1];
@timyates
timyates / uncurry.groovy
Created May 5, 2016 20:42
Uncurry(?) in Groovy
// Change a method reference into a closure that takes the delegate as the first parameter
import org.codehaus.groovy.runtime.MethodClosure
def uncurry(MethodClosure c) {
{a, ...b -> a."$c.method"(*b) }
}
// So uncurrying Number.plus gives us a closure that will take {x, y -> x.plus(y)}
def plus = uncurry(Number.&plus)
assert plus(1, 2) == 3
@crazy4groovy
crazy4groovy / Json-to-ImmutableClassInstance.groovy
Last active October 17, 2016 20:33
Create an Immutable Class instance from JSON? Let's see!
import groovy.json.*
import groovy.transform.Immutable
enum Fruit {
Orange,
Apple
}
enum Car {
Ford('slow'),
Closure bind
List.metaClass.rightShiftUnsigned = { return bind(delegate, it) }
List.metaClass.call = { return [delegate, ''] }
Closure squrt = { Double x ->
if (x < 0.0d) return []
if (x == 0.0d) return [0.0]
return [Math.pow(x, 0.5), -Math.pow(x, 0.5)]
}