Skip to content

Instantly share code, notes, and snippets.

@CyrusJavan
CyrusJavan / justify_text.go
Created October 23, 2020 21:23
Justify Text - techincal interview question and answer written in Golang with many comments explaing the answer.
package main
import (
"fmt"
"strings"
)
func main() {
text := "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras " +
"venenatis, quam et dapibus porttitor, nisi mauris maximus sapien, a " +
@colmmacc
colmmacc / shardcalc.py
Last active August 7, 2023 11:05
Calculate the blast radius of a shuffle shard
import sys
# choose() is the same as computing the number of combinations. Normally this is
# equal to:
#
# factorial(N) / (factorial(m) * factorial(N - m))
#
# but this is very slow to run and requires a deep stack (without tail
# recursion).
#
@vkhorikov
vkhorikov / CustomerController.cs
Last active May 4, 2024 01:25
Handling failures and input errors in a functional way
[HttpPost]
public HttpResponseMessage CreateCustomer(string name, string billingInfo)
{
Result<BillingInfo> billingInfoResult = BillingInfo.Create(billingInfo);
Result<CustomerName> customerNameResult = CustomerName.Create(name);
return Result.Combine(billingInfoResult, customerNameResult)
.OnSuccess(() => _paymentGateway.ChargeCommission(billingInfoResult.Value))
.OnSuccess(() => new Customer(customerNameResult.Value))
.OnSuccess(
@markrendle
markrendle / README.md
Last active August 5, 2017 13:35
Vagrantfile to run Kafka in boot2docker

Kafka in Docker in Vagrant

I'm using this Vagrantfile to run Kafka on a Windows 8.1 laptop for development purposes.

It runs the ultra-lightweight boot2docker Linux, then uses Vagrant's Docker provisioning support to spin up ZooKeeper and Kafka.

The fun bits to work out were:

  • You need to forward the ports on both Vagrant (lines 13 & 14) and Docker (the -p flag), so you can access the instance from Windows using localhost:9092
@taylorkj
taylorkj / gist:9012616
Last active March 27, 2021 14:16
How to use Dapper's new Table Valued Parameter (TVP) in C#
/*
I wasn't able to find a single example on how to actually use Dapper's new TVP, so I though I'd add one.
First of all, you will need to install the Dapper.TVP package from NuGet.
The main item to note is the need to create and populate a list of SqlDataRecords. This is then used to used as part of the
input parameter for Dapper's TableValueParameter.
The API is thus:
@bradwilson
bradwilson / Cacheability.cs
Created January 23, 2014 20:53
Using chaining to create cached results in ASP.NET Web API v2
public enum Cacheability
{
NoCache,
Private,
Public,
}
@thecodejunkie
thecodejunkie / DynamicModelBinder.cs
Created May 5, 2013 19:41
Binding to a DynamicDictionary
using System;
using System.Collections.Generic;
using System.Linq;
using Nancy.ModelBinding;
public class DynamicModelBinder : IModelBinder
{
public object Bind(NancyContext context, Type modelType, object instance, BindingConfig configuration, params string[] blackList)
{
var data =
@DamianEdwards
DamianEdwards / Program.cs
Created March 19, 2013 05:54
Demonstrates that SqlDependency notifications can occur concurrently with the command that set them up. This might cause issues in certain cases if the application isn't expecting multiple commands to be executed at the same time.
using System;
using System.Data;
using System.Data.SqlClient;
using System.Threading;
namespace SqlDependencyConcurrency
{
class Program
{
private static readonly string _connectionString = "Data Source=(local);Initial Catalog=Sample;Integrated Security=SSPI;";