Skip to content

Instantly share code, notes, and snippets.

@pietrom
pietrom / StringToObjectSerializer.cs
Created May 30, 2018 10:09
Custom mongodb serializer, able to wrap/unwrap string values to/from custom class instances, generic for the type of the (de)serialized custom class
namespace Huxley.MongoDb.Serialization {
class StringToObjectSerializer<T> : SerializerBase<T> {
private readonly Func<string, T> valueDeserializer;
private readonly Func<T, string> valueSerializer;
public StringToObjectSerializer(Func<string, T> valueDeserializer, Func<T, string> valueSerializer) {
this.valueDeserializer = valueDeserializer;
this.valueSerializer = valueSerializer;
}
@pietrom
pietrom / MyClassSerializer.cs
Last active May 30, 2018 09:59
Custom mongodb serializer, able to wrap/unwrap string values to/from custom class instances
class MyClassSerializer : SerializerBase<Plate> {
public override MyClass Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args) {
string value = context.Reader.ReadString();
return new MyClass(value);
}
public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, MyClass obj) {
context.Writer.WriteString(obj.Value);
}
}
@pietrom
pietrom / Vagrantfile
Last active May 23, 2018 07:00
Vagrantfile providing SqlServer on localhost:1433
Vagrant.configure("2") do |config|
config.vm.define "linux-sqlserver"do |linux|
linux.vm.box = "ubuntu/xenial64"
linux.vm.network "forwarded_port", guest: 1433, host: 1433
linux.vm.provider "virtualbox" do |vb|
vb.name = "my-sqlserver"
vb.memory = 2048
vb.cpus = 2
@pietrom
pietrom / process-template.js
Created April 20, 2018 09:55
Replace placeholders in string template
function process(tpl, params) {
return Object.keys(params).reduce(function(acc, curr) { return acc.replace("[" + curr + "]", params[curr]);}, tpl)
}
using System;
using System.Collections.Specialized;
using System.Configuration;
public static class ConfigurationExtension {
public static string ReadSetting(this NameValueCollection self, string key) {
return self[$"{Environment.MachineName}.{key}"] ?? self[key];
}
public static string ReadConnectionString(this ConnectionStringSettingsCollection self, string name) {
@pietrom
pietrom / ConcurrentExecutor.cs
Created January 12, 2018 14:21
Concurrent executer, for concurrency-related tests
namespace TestingSupport {
public class ConcurrentExecutor {
public int ThreadCount { get; set; } = 50;
public int RepetitionsCount { get; set; } = 200;
public bool RandomDelayBetweenExecutions { get; set; } = true;
private readonly Random random = new Random();
public void Execute(Action action) {
Barrier startBarrier = new Barrier(ThreadCount);
@pietrom
pietrom / option.js
Last active January 3, 2018 11:54
JS function providing functional 'option' capabilities
function option(x) {
return {
map: function(f) {
return x ? option(f(x)) : option()
},
valueOr: function(d) {
return x || d
}
}
}
@pietrom
pietrom / leading-zeroes-in-factorial-of-n.js
Created December 20, 2017 08:09
Calculate the number of leading zeroes in n!
function range(first, last) {
const length = last - first + 1
return new Array(length).fill(-1, 0, length).map(function(v, i) { return i + first; })
}
function factZeros(n) {
return range(1, Math.floor(Math.log(n) / Math.log(5))).map(x => Math.pow(5, x)).map(x => Math.floor(n / x)).reduce((acc, curr) => acc + curr, 0)
}
@pietrom
pietrom / delayed-event-handler.js
Last active November 22, 2017 07:47
Delayed function execution after the last invocation of an event handler
let timerId = null
const delay = 2000
function delayedHandler() {
console.log('Hello, World!')
}
function onEvent() {
if(timerId) {
clearTimeout(timerId)
@pietrom
pietrom / ConnectionStringExtension.cs
Last active September 12, 2017 13:22
Utility methods for connection string handling
using System;
using System.Data;
using System.Data.SqlClient;
namespace Plastiline.Core.Utils.Sql {
public static class ConnectionStringExtension {
public static void Execute(this string connectionString, Action<IDbConnection> action) {
using (IDbConnection conn = new SqlConnection(connectionString)) {
conn.Open();
action(conn);