Skip to content

Instantly share code, notes, and snippets.

View ktilcu's full-sized avatar

kyle tilman ktilcu

View GitHub Profile
@ktilcu
ktilcu / !.md
Last active April 17, 2018 20:43
Different async implementations

Question

Which version of the code resonates with how you think about things?

Aside

I'm sure there are problems with this code and there are likely better ways but I chose each of these implementations on purpose.

Answers

Please answer in the gist comments so I can track them. We can carry on the conversation in chat ##javascript or #Node.js.

@ktilcu
ktilcu / optimal.js
Created January 19, 2018 16:37
Optimal Async example
const getAvatars = async (userId) => {
const user = await getUser(userId);
const avatar = await user.avatar();
const friends = await user.friends();
return [avatar, ...await Promise.all(friends.map(friend => friend.avatar())]
}
@ktilcu
ktilcu / !.md
Last active January 24, 2018 23:53

Imagine a function getUser that returns a Promise that resolves to a user. A user has friends and avatar methods that resolve to a list of users and an avatar respectively.

Imagine a user module that provides access to:

  • a user
  • a user's avatar
  • a user's friends
  • a user's friends avatars
  • a user's avatar + friends' avatars
@ktilcu
ktilcu / a.js
Created January 19, 2018 17:47
jo
const Promise = require("bluebird");
function getUserAndFriendsAvatars(user) {
return Promise.try(() => {
return getUser(user);
}).then((user) => {
return Promise.all([
user.avatar(),
user.friends()
]);
@ktilcu
ktilcu / firehose.js
Created February 8, 2018 21:51
Firehose messages to queue
var Promise = require('bluebird');
var sqs = require('./queue');
var R = require('ramda');
var cli = require('cli');
var options = cli.parse({
dest: ['d', 'Destination queue url', 'string', undefined]
});
@ktilcu
ktilcu / apaste
Created April 9, 2018 17:31 — forked from sam-roth/apaste
Aligning `paste` alternative
#!/usr/bin/env python3
import argparse
import itertools
ap = argparse.ArgumentParser()
ap.add_argument('file', nargs='*', type=argparse.FileType('r'))
ap.add_argument('-p', '--pad-char', default=' ')
ap.add_argument('-d', '--delimiter', default=' ')
ap.add_argument('-c', '--pad-color', default=None)
ap.add_argument('-s', '--align-spec', default='')
@ktilcu
ktilcu / maybes.js
Created April 13, 2018 14:17
Maybes with one null check
// type alias DOM = Object // unknown structure, defined by Cheerio
// type alias Selector = String // like a jquery selector (e.g., '#omg')
// selectAll :: Selector -> DOM -> List DOM
const selectAll = R.curry((sel, dom) => {
const res = dom(sel);
return R.map(cheerio, res.toArray());
});
// selectFirst :: Selector -> DOM -> Maybe DOM
@ktilcu
ktilcu / retry-future.js
Created April 13, 2018 14:28
Future URL fetching
const Future = require('fluture');
// (Number -> Number) -> Number -> Future a b -> Future (List a) b
const retry = (time, max, task) => {
const failures = new Array(max);
return (function recurse(i) {
return task.chainRej(function(failure) {
failures[i] = failure;
const total = i + 1;
return total === max
function doubleBarrel (fn) {
return function (a,b) {
var len = a.length > b.length ? b.length : a.length;
if (len == 0) {
return a.length > b.length ? a : b;
}
var idx = 0;
var out = [];
while (idx < len) {
out.push(fn(a[idx], b[idx]));
@ktilcu
ktilcu / app.js
Last active May 10, 2018 16:35
PG Pooling with express
const db = require('./db');
const express = require('express')
const app = express()
app.get('/', (req, res) => res.send('Hello World!'))
db.init()
// after init
.then(() => app.listen(3000, () => console.log('Example app listening on port 3000!')))