Skip to content

Instantly share code, notes, and snippets.

@andrewglind
andrewglind / request.js
Last active November 15, 2018 02:20
Lightweight promise based REST API requests
class Request {
constructor(protocol, host, requestHandler, responseHandler) {
this.protocol = protocol
this.host = host
this.requestHandler = requestHandler
this.responseHandler = responseHandler
}
_request(path, method, requestData, headers) {
return new Promise((resolve, reject) => {
@andrewglind
andrewglind / Extension.kt
Last active October 15, 2017 19:04
Extension function to convert HashMap<String, Any> to JS Object
fun HashMap<String, Any>.toJson(): Json {
val js = json()
this.forEach { (k, v) -> js.set(k, v) }
return js
}
@andrewglind
andrewglind / 2-way-generator.js
Created October 10, 2017 07:16
2-way generator - My explanation of the example at https://davidwalsh.name/es6-generators
/*************************
First call it.next(): has the effect of starting the function, there is no yield waiting for input at this point,
therefore yield (line 13) passes back the value 6, and is now waiting for input
Second call it.next(12): yield (line 13) is paused and waiting for input, input is 12, therefore y is assigned the value 24 (2 * 12).
yield (line 14) passes back 8 (24(y) / 3), and is now waiting for input
Final call it.next(13): yield (line 14) is paused and waiting for input, input is 13, therefore z is assigned the value 13.
function returns x + y + z (5 + 24 + 13) = 42
*************************/
@andrewglind
andrewglind / Main.kt
Last active December 3, 2018 16:02
Pass Node.js command-line args to Kotlin (JavaScript)
package blah
fun Main(args: List<String>) {
// ... your code goes here
}
external val process: dynamic
fun main(args: Array<String>) {
Main((process["argv"] as Array<String>).drop(2))
}
@andrewglind
andrewglind / anonymous.groovy
Created May 17, 2016 21:05
Use a proxy to create an anonymous method pointer
def x = ["y":{z-> println "Hello, ${z ?: 'World'}!"}].&y
x() // Hello, World!
x('Blah') // Hello, Blah!