Skip to content

Instantly share code, notes, and snippets.

/// <summary>
/// Adapted from SO answer https://stackoverflow.com/a/40491640/915147
/// </summary>
/// <typeparam name="TEntity"></typeparam>
public class AsyncQueryProvider<TEntity> : IAsyncQueryProvider
{
private readonly IQueryProvider _inner;
internal AsyncQueryProvider(IQueryProvider inner) => _inner = inner;
public IEnumerable<T> ConcatTwoOrderedArray<T>(IReadOnlyList<T> first, IReadOnlyList<T> second)
{
dynamic dynamicFirst = first, dynamicSecond = second;
for (int i = 0, j = 0; i < first.Count || j < second.Count;)
{
if (i < first.Count && (j >= second.Count || dynamicFirst[i] < dynamicSecond[j]))
yield return first[i++];
else
yield return second[j++];
}
using System;
using System.Collections.Generic;
public static class EnumeratorExtensions
{
public static IEnumerable<T> TakeNextUntil<T>(this IEnumerator<T> enumerator, Predicate<T> predicate)
{
while(enumerator.MoveNext())
{
var item = enumerator.Current;
using System;
using System.Linq.Expressions;
using System.Reflection;
public static class ReflectionUtils
{
public static MethodInfo GetMethod<TInput, TOutput>(Expression<Func<TInput, TOutput>> methodCallExpression) => GetMethodByLambdaExpression(methodCallExpression);
public static MethodInfo GetMethod<TInput>(Expression<Action<TInput>> methodCallExpression) => GetMethodByLambdaExpression(methodCallExpression);
@imgen
imgen / MaxRectangleFinder.js
Created February 26, 2019 06:54
My friend CM's JavaScript version of calculating max rectangle area
const f = a => {
let max = 0;
const getLocalMax = a => {
let localMax = 0, maxJ = a[0].length -1;
const reset = i => {
const size = (i+1)*(maxJ+1);
if(size > localMax)
localMax = size;
}
@imgen
imgen / MaxRectangleFinder.cs
Last active February 26, 2019 05:20
Minimal version of calculating max rectangle area of a matrix of 0 and 1s
using System;
using System.Linq;
public class Program {
public static void Main() {
var matrices = new []{
new [] {
"11011",
"01101",
"11110",
@imgen
imgen / MaxRectangleFinder.cs
Last active February 26, 2019 07:24
Calculate max rectangle area of a matrix of 0 and 1s
using System;
public class Program {
public static void Main() {
var matrices = new []{
new [] {
"00000",
"00000",
"00000",
"00000",
@imgen
imgen / boxstarter.ps1
Created November 30, 2018 09:22
Boxstarter Commands for a new Windows box.
# Description: Boxstarter Script
# Author: Alex Ho
#
# Install boxstarter:
# . { iwr -useb http://boxstarter.org/bootstrapper.ps1 } | iex; get-boxstarter -Force
#
# You might need to set: Set-ExecutionPolicy RemoteSigned
#
# Run this boxstarter by calling the following from an **elevated** command-prompt:
# start http://boxstarter.org/package/nr/url?<URL-TO-RAW-GIST>
@imgen
imgen / QueueByStacks.cs
Last active October 23, 2018 14:36
Queue implemented in stacks
using System.Collections.Generic;
class QueueByStacks<T>
{
private Stack<T> _inStack, _outStack;
public QueueByStacks()
{
_inStack = new Stack<T>();
_outStack = new Stack<T>();
@imgen
imgen / DotNetExtensions.cs
Created October 3, 2017 16:06
Extensions
public static IEnumerable<PropertyInfo> GetPropertiesWithAttributes<T, TAttr>(this object obj)
{
return GetPropertiesWithAttributes(obj, typeof(T), typeof(TAttr));
}
public static IEnumerable<PropertyInfo> GetPropertiesWithAttributes(this object obj, Type type, Type attrType)
{
return type.GetProperties()
.Where(x => x.GetCustomAttributes(true)
.Any(attr => attr.GetType() == attrType)