Skip to content

Instantly share code, notes, and snippets.

@jmcd
jmcd / Simulator Screen Shot - iPhone Xʀ - 2019-08-01 at 18.07.31.png
Last active August 1, 2019 17:12
Make individual UIViews appear rotated in an orientation locked app
Simulator Screen Shot - iPhone Xʀ - 2019-08-01 at 18.07.31.png
@jmcd
jmcd / Startup.cs
Created April 5, 2019 10:46
Get external (eg social) claims while logged in with another identity
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.UI;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using WebApplication6.Data;
using Microsoft.Extensions.Configuration;
func positionsOfZeroNetForce(_ magnetPositions: [Double]) -> [Double] {
let threshold = 0.0000000000001
// Utility function to give 1/fn(m0) + 1/fn(m1) + ... + 1/fn(mn)
// Used for working out the force at x, and its derivitive
func sumReciprocals(_ fn: (Double)->(Double)) -> Double {
return magnetPositions.map(fn).map {
guard $0 != 0 else { return Double.nan }
return 1.0/$0
@jmcd
jmcd / Operator.swift
Last active March 22, 2019 14:56
Shunting Yard
import Foundation
public enum Associativity {
case left, right
}
public struct Operator: Equatable, CustomStringConvertible {
public var description: String { return body }
let body: String
@jmcd
jmcd / Operator.swift
Created March 22, 2019 14:54
Shunting Yard
import Foundation
public enum Associativity {
case left, right
}
public struct Operator: Equatable, CustomStringConvertible {
public var description: String { return body }
let body: String
@jmcd
jmcd / RPN.scala
Created March 10, 2019 09:33
RPN in scala
import scala.collection.mutable.ListBuffer
object Main extends App {
def getOperator(s: String): Option[((Int, Int) => Int)] = {
s match {
case "+" => Some(_+_)
case "-" => Some(_-_)
case "/" => Some(_/_)
case "*" => Some(_*_)
let tokens = "15 7 1 1 + - / 3 * 2 1 1 + + -".split(separator: " ").map { String($0) }
let operators: [String: (Int, Int) -> (Int)] = [
"+" : (+),
"-" : (-),
"*" : (*),
"/" : (/)
]
let outstack = tokens.reduce([Int]()) { (stack, token) in
@jmcd
jmcd / Compressor.cs
Created March 8, 2019 13:03
LZ77Compressor
using System;
using System.Collections.Generic;
using System.Linq;
namespace LZ77
{
public static class Compressor
{
// it's impossible for a pointer to start with this because that would mean a length of 1
private const int Escape = 0x1;
func largestSquare(_ s: String, width: Int) -> Int {
let bits = s.map { $0 == "1" }
let height = bits.count/width
let longestLen = (0..<bits.count).reduce(0) { (curLongestLen, i) in
let maxLen = min(width - i%width, height - i/width)
let firstLenWhereAllBitsNotSet = (1...maxLen).first(where: { len in
!(0..<len).allSatisfy { n in
bits[i + width*(len-1) + n] && bits[i + (len-1) + width*n]
@jmcd
jmcd / TestHttpClientFactory.cs
Last active February 12, 2019 10:49
TestHttpClientFactory for unit tests
public class TestHttpClientFactory : IHttpClientFactory
{
private class MockHttpMessageHandler : HttpMessageHandler
{
public readonly IList<(Predicate<HttpRequestMessage> predicate, Func<HttpRequestMessage, HttpResponseMessage> factory)> Behaviours =
new List<(Predicate<HttpRequestMessage> predicate, Func<HttpRequestMessage, HttpResponseMessage> factory)>();
public readonly IList<(HttpRequestMessage, HttpResponseMessage)> Log = new List<(HttpRequestMessage, HttpResponseMessage)>();
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)