Skip to content

Instantly share code, notes, and snippets.

@tuan
tuan / SmtpClientWrapper.cs
Last active Feb 6, 2016
SendMailAsync with cancellation token
View SmtpClientWrapper.cs
public class SmtpClientWrapper : IDisposable
{
private SmtpClient _smtpClient;
public SmtpClientWrapper(SmtpClient smtpClient)
{
this._smtpClient = smtpClient;
}
public async Task SendMailAsync(MailMessage message, CancellationToken ct)
@tuan
tuan / SendMailAsync.cs
Created Feb 6, 2016
Implementation of SendMailAsync that correctly cancels the operation when CacellationToken is cancelled.
View SendMailAsync.cs
public async Task SendMailAsync(MailMessage message, CancellationToken ct)
{
try
{
var task = _smtpClient.SendMailAsync(message);
using (ct.Register(_smtpClient.SendAsyncCancel))
{
try
{
await task;
View pipe.js
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);
View arabicToRoman.js
const RomanNumerals = {
1000: 'M',
500: 'D',
100: 'C',
50: 'L',
10: 'X',
5: 'V',
1: 'I'
};
@tuan
tuan / onceWithin.js
Last active Jun 14, 2016
Executes a function once within a specified time window
View onceWithin.js
const once = (fun, timeoutInMilliseconds) => {
let inProgress = false;
setTimeout(() => {
inProgress = false;
}, timeoutInMilliseconds);
return () => {
if (inProgress) {
return;
View once.js
function once(fn) {
let done = false;
return function(...args) {
if (done) {
return void 0;
}
done = true;
return fn.apply(this, args);
View mapWith.js
function mapWith (fn) {
return (list) => {
return Array.prototype.map.call(
list,
(value) => fn.call(this, value))
}
}
View unary.js
function unary(fn) {
return (value) => fn.call(this, value);
}
@tuan
tuan / 1.procedureal_feeding_time.rb
Last active Nov 12, 2016
Functional Core Imperative Shell
View 1.procedureal_feeding_time.rb
def feeding_time
walruses.each do |walrus| /* we know how to destructure walruses array */
walrus.stomach << Cheese.new /* we know the deep structure of a walrus object */
end
end
@tuan
tuan / UploadMultipleBlobs.cs
Created Feb 6, 2016
A test console app to upload multiple blobs to Azure Blob Storage in parallel
View UploadMultipleBlobs.cs
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);
}