Skip to content

Instantly share code, notes, and snippets.

View andy-williams's full-sized avatar

Andrew Williams andy-williams

View GitHub Profile
@andy-williams
andy-williams / lavenstein.cs
Created May 2, 2019 21:15
Simple Lavenstein edit distance algorithm
public class Program
{
public void Main()
{
PrintEditDistance("ab", "ab");
PrintEditDistance("bbc", "abc");
PrintEditDistance("bbbc", "bbc");
PrintEditDistance("xyza", "abcx");
}
@andy-williams
andy-williams / Main.cs
Created March 6, 2019 13:56
Get exceptions of all tasks with Task.WhenAll
async Task Main()
{
var task1 = Task.Run(async () => {
await Task.Delay(500);
throw new Exception("Exception from task 1");
});
var task2 = Task.Run(() =>
{
throw new Exception("Exception from task 2");
});
@andy-williams
andy-williams / ApiController.cs
Created March 6, 2019 01:07
React inline error with asp.net Model State errors
[Route("api/v1/account")]
[AllowAnonymous]
public class ApiController : Controller
{
[Route("login")]
public async Task<IActionResult>Login([FromBody] LoginRequest login)
{
if (ModelState.IsValid)
{
// ... logic
@andy-williams
andy-williams / shopify.liquid
Created March 7, 2017 21:12
shopify-modulo
{% for product in collection.products %}
{% capture everyThird %}{{ forloop.index | modulo: 3 }}{% endcapture %}
{% capture everyOnePastThird %}{{ forloop.index0 | modulo: 3 }}{% endcapture %}
{% if forloop.first == true or everyOnePastThird == '0' %}
<div class="row">
{% endif %}
{% include 'product-catalog-item' %}
{% if forloop.last == true or everyThird == '0' %}
@andy-williams
andy-williams / lumen-cheatsheet.md
Last active December 4, 2022 23:07
Lumen cheat sheet

Lumen / Laravel Cheat sheet

Install Lumen / Laravel

install global installer

composer global require "laravel/lumen-installer"

composer global require "laravel/installer"

start new project

class TermFinder:
@staticmethod
def find_terms(to_search, terms):
resultDict = {}
for term in terms:
resultDict[term] = TermFinder._find_term(to_search, term)
return resultDict
@staticmethod
def _find_term(to_search, term):
@andy-williams
andy-williams / sequential-vs-plinq-vs-mapreduce.cs
Created January 14, 2016 15:38
Embarrassingly parallel problem solved with different approaches
void Main()
{
Stopwatch sw;
var ints = Enumerable.Range(0, 25000);
Func<int, double> func = (x) => {
double result = 0;
for(var i = 0; i < x; i++) {
result += Math.Cos(i);
}
(function() {
myVar = 1;
console.log("myVar should be 1: " + myVar); // should be 1
setTimeout(function() {
console.log("myVar should be 1, but isn't: " + myVar); // should be 1, but isn't
}, 100);
})();
(function() {
myVar = 2;
@andy-williams
andy-williams / TaskWaitAny.cs
Last active January 14, 2016 09:27
Task.WaitAny does not care when a task has finished
void Main()
{
var task1 = Task.Factory.StartNew(() => {
Thread.Sleep(1);
return 1;
});
var task2 = Task.Factory.StartNew(() => {
Thread.Sleep(1);
return 2;
@andy-williams
andy-williams / map-reduce.cs
Last active April 16, 2022 18:56
Parallel Programming in C# : Patterns
// Problem:
// We have lots of data that we want to process that can be easily parallelised
// We want to process all our data and combine the results
// "Map is an idiom in parallel computing where a simple operation is applied to all elements of a
// sequence, potentially in parallel.[1] It is used to solve embarrassingly parallel problems: those
// problems that can be decomposed into independent subtasks, requiring no
// communication/synchronization between the subtasks except a join or barrier at the end."
// - https://en.wikipedia.org/wiki/Map_(parallel_pattern)
void Main()