Skip to content

Instantly share code, notes, and snippets.

function mapWith (fn) {
return (list) => {
return Array.prototype.map.call(
list,
(value) => fn.call(this, value))
}
}
function once(fn) {
let done = false;
return function(...args) {
if (done) {
return void 0;
}
done = true;
return fn.apply(this, args);
const RomanNumerals = {
1000: 'M',
500: 'D',
100: 'C',
50: 'L',
10: 'X',
5: 'V',
1: 'I'
};
let pipe = (value, ...funs) => {
return funs.reduce((accumulator, currentFun) => currentFun(accumulator), value);
}
let add1 = x => x + 1;
let multiply2 = x => x * 2;
pipe(10, add1, add1, multiply2, console.log);
@tuan
tuan / onceWithin.js
Last active June 14, 2016 13:13
Executes a function once within a specified time window
const once = (fun, timeoutInMilliseconds) => {
let inProgress = false;
setTimeout(() => {
inProgress = false;
}, timeoutInMilliseconds);
return () => {
if (inProgress) {
return;
@tuan
tuan / UploadMultipleBlobs.cs
Created February 6, 2016 22:28
A test console app to upload multiple blobs to Azure Blob Storage in parallel
class Program
{
static void Main(string[] args)
{
var stopwatch = new Stopwatch();
stopwatch.Start();
UploadToBlobAsync().Wait();
stopwatch.Stop();
Debug.WriteLine("Elapsed time in seconds: " + stopwatch.ElapsedMilliseconds / 1000);
}
@tuan
tuan / SendMailAsync.cs
Created February 6, 2016 22:14
Implementation of SendMailAsync that correctly cancels the operation when CacellationToken is cancelled.
public async Task SendMailAsync(MailMessage message, CancellationToken ct)
{
try
{
var task = _smtpClient.SendMailAsync(message);
using (ct.Register(_smtpClient.SendAsyncCancel))
{
try
{
await task;
@tuan
tuan / SmtpClientWrapper.cs
Last active February 6, 2016 22:13
SendMailAsync with cancellation token
public class SmtpClientWrapper : IDisposable
{
private SmtpClient _smtpClient;
public SmtpClientWrapper(SmtpClient smtpClient)
{
this._smtpClient = smtpClient;
}
public async Task SendMailAsync(MailMessage message, CancellationToken ct)
@tuan
tuan / ParallelUploadToBlobStorage.cs
Created February 6, 2016 14:55
Upload multiple files to azure blob storage in parallel
class Program
{
static void Main(string[] args)
{
var stopwatch = new Stopwatch();
stopwatch.Start();
UploadToBlobAsync().Wait();
stopwatch.Stop();
Debug.WriteLine("Elapsed time in seconds: " + stopwatch.ElapsedMilliseconds / 1000);
}