Skip to content

Instantly share code, notes, and snippets.

@jmcd
jmcd / Validation.cs
Last active March 27, 2018 10:17
ASP.NET Core Custom validation of an action parameter that is a list
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc(options =>
{
options.ModelValidatorProviders.Insert(0, new ListOfFooValidatorProvider());
});
}
import UIKit
import PlaygroundSupport
class MySlider: UISlider {
// indent the tracking so the thumb does not hang outside the view
private let padding = CGFloat(18)
// the default rect for tracking is indented by a few pixels; keep note of that value here
private var trackIndent = CGFloat(0)
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc(config =>
{
var requireAgent = new AuthorizationPolicyBuilder()
.RequireAuthenticatedUser()
.RequireRole(RoleNames.Agent)
@jmcd
jmcd / Bits.cs
Created September 12, 2017 13:19
Byte array to ints
public static class Bits
{
public static int[] ToInt32s(byte[] bytes)
{
var paddedBytes = new byte[bytes.Length + 3];
Array.Copy(bytes, paddedBytes, bytes.Length);
var numberOfInts = (bytes.Length + 3) / 4;
var ints = new int[numberOfInts];
@jmcd
jmcd / ApplicationServices.cs
Last active June 21, 2017 10:31
Registration of services in .NET Core without a wall of services.AddFoo<>
public class Startup {
public void ConfigureServices(IServiceCollection services)
{
...
services.AddApplicationServices();
}
}
// Defines which stategies to use, and assemblies to scan, as an extension to be called from startup
@jmcd
jmcd / Trendy.cs
Created June 13, 2017 10:35
Create data from a hand drawn bmp line
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
namespace Trendy
{
internal class Program
{
private static void Main(string[] args)
using System;
using System.IO;
using System.Linq;
using System.Xml.Linq;
namespace ConsoleApplication1
{
internal class Program
{
private static void Main(string[] args)
@jmcd
jmcd / gist:f6de090d9b6f57b6e801fabd9252c1f6
Created April 19, 2017 08:04
commented out file regex
([$,\t,” “]*//.*\n){2,}
@jmcd
jmcd / Group.swift
Last active March 2, 2017 10:47
Grouping an array by members in Swift
import Foundation
extension Array {
func group<Discrimininator>(by discriminator: (Element)->(Discrimininator)) -> [Discrimininator: [Element]] where Discrimininator: Hashable {
var result = [Discrimininator: [Element]]()
for element in self {
let key = discriminator(element)
@jmcd
jmcd / AsyncTaskChain.swift
Last active June 6, 2018 05:36
Chaining async calls in Swift
struct AsyncTaskChain<Success, Error> {
typealias TaskClosure = (Success?, Error?) -> ()
typealias Task = (TaskClosure) -> ()
typealias ChainClosure = ([Success], Error?)->()
private let tasks: [Task]
private let completion: ChainClosure
private let previousSuccesses: [Success]