This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Fixed point combinator. Because reasons. | |
# @param [Block with arity 1] Receives recursion Proc, must return Proc representing function (any arity) | |
# @return [Proc] | |
def Y | |
fail unless block_given? | |
lambda{ |fn| fn.call fn }.call lambda{ |fn| | |
lambda{ |*args| | |
(yield fn.call fn).call *args | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class HashlikeFetchCache | |
include HashlikeOverlay | |
def initialize( context ) | |
@overlay = Hash[] | |
@context = context | |
freeze | |
end | |
def []( key ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class BaseEnum | |
include Enumerable | |
attr_reader :ctx | |
def initialize( *ctx ) | |
@ctx = ctx | |
end | |
def each( *args, &blk ) | |
return enum_for __callee__, *args unless block_given? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Tracing | |
def trace_message( msg = yield ) | |
level = String(__callee__).upcase | |
puts ' ** [%s] %s: %s' % [level, self.class, msg] | |
end | |
%i[debug info warn error].each{ |m| alias_method m, :trace_message } | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::io::{self, Read, BufRead}; | |
use std::cmp; | |
use std::fmt::{self, Write}; | |
const HR_BYTES_PER_LINE: usize = 16; | |
struct HexReader<T> { | |
inner: T, | |
buf: String, | |
buf_pos: usize, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'set' | |
# Invokes the block, passing in all or some of the input. If the block throws, the block will be invoked | |
# repeatedly for a progressively smaller subset of the batch until all elements are either processed or | |
# failed. Elements contained in sub-batches which succeed are never re-tried. Returns two Arrays, the first | |
# contains all items which succeeded and the second contains all elements which failed. | |
# | |
# This can be useful when a batch API will throw when it receies one or more malformed items, but doesn't | |
# tell you which input items were at fault. | |
# |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const lambda_handler = require('lambda-handler'); | |
exports.handler = lambda_handler(( event, ctx, done ) => { | |
// This will log a handled error to CloudWatch and return the error to AWS Lambda | |
throw 'BOOM!'; | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
// Make predicate funtions more convenient. | |
// Works like Ruby's Symbol#to_proc, which is usually called with the unary & operator and a Symbol literal: | |
// (ruby) %w[one two three].map(&:upcase) # returns ["ONE", "TWO", "THREE"] | |
// (js) ['one', 'two', 'three'].map(toFn('toUpperCase')); // returns ["ONE", "TWO", "THREE"] | |
// (js) ['one', 'two', 'three'].map(toFn`toUpperCase`); // same as above, but called via tagged string literal | |
// @param name {String} Name of method | |
// @param ...bound_args Arguments to bind NOTE: Args cannot be bound when calling as a tagged-template | |
// @return {Function} Function to call named method with bound args on any object given at time of invocation | |
module.exports = function toFn( name, ...bound_args ){ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const error_policy = require('./error-policy'); | |
// EventEmitter example | |
const {Readable} = require('stream'); | |
class MyReadable extends Readable { | |
constructor(){ | |
super(...arguments); | |
this.policy = error_policy(e => this.emit('error', e)); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
const {Transform} = require('readable-stream'); | |
const PRIVATE = Symbol('ConcurrentTransform private state'); | |
const NOOP_TRANSFORM = (chunk, _, done) => done(null, chunk); | |
const NOOP_FLUSH = done => done(); | |
// Transform stream which processes chunks with limited (or unlimited) concurrency | |
class ConcurrentTransform extends Transform { | |
// @param opts {Object} Stream options. Set concurrency limit with "concurrent" (default concurrency: 1) | |
constructor( opts = {} ){ |