Skip to content

Instantly share code, notes, and snippets.

View typesafedev's full-sized avatar
🏠
Functionally efficient

Andy Lam typesafedev

🏠
Functionally efficient
  • UK
View GitHub Profile
@typesafedev
typesafedev / cloudSettings
Created July 6, 2020 15:21
Visual Studio Code Settings Sync Gist
{"lastUpload":"2020-07-06T15:21:12.704Z","extensionVersion":"v3.4.3"}
@typesafedev
typesafedev / fp-lingo.md
Created July 2, 2020 14:52 — forked from ericelliott/fp-lingo.md
A Guide to Functional Programming Lingo for JavaScripters

A Guide to Functional Programming Lingo for JavaScripters

Functional programming gets a bad wrap about being too hard for mere mortals to comprehend. This is nonsense. The concepts are actually quite simple to grasp.

The jargon is the hardest part. A lot of that vocabulary comes from a specialized field of mathematical study called category theory (with a liberal sprinkling of type theory and abstract algebra). This sounds a lot scarier than it is. You can do this!

All examples using ES6 syntax. wrap (foo) => bar means:

function wrap (foo) {
@typesafedev
typesafedev / Immutability.ts
Last active June 20, 2020 11:28
Immutability: Demos 3 ways to create new object from old without mutating old object. Run with: deno run Immutability.ts
//Immutability via vanilla, lens, and immer
import { Lens, Iso } from "https://cdn.pika.dev/monocle-ts@^2.2.0";
import { produce } from "https://cdn.pika.dev/immer@^7.0.0";
const toJson = (obj: any) => JSON.stringify(obj, null, 2);
const log = (obj: any) => console.log(toJson(obj));
type Street = { num?: number; name: string };
type Address = { city: string; street: Street };
type Company = { name: string; address: Address };
@typesafedev
typesafedev / Monocle-lens.ts
Last active June 17, 2020 17:15
Demo use of monocle-ts lens. Run with deno Monocle-lens.ts. The use of deno is optional
import { Lens } from "https://cdn.pika.dev/monocle-ts@^2.2.0";
const toJson = (obj: any) => JSON.stringify(obj, null, 2);
const log = (obj: any) => console.log(toJson(obj));
type Street = { num?: number; name: string };
type Address = { city: string; street: Street };
type Company = { name: string; address: Address };
const awesome: Company = {
@typesafedev
typesafedev / BCKW.cs
Last active June 16, 2020 13:48
BCKW combinators in C# by Haskell Curry
/// BCKW combinators by Haskell Curry
/// The Bluebird or compose = λxyz.x(zy). Composition of the args x and y applied to arg z.
static Func<Func<A,B>, Func<A,C>> B<A,B,C>(Func<B,C> x) => y => z => x(y(z));
/// The Cardinal or Thrush once removed = λxyz.xzy. Swaps the argumant y and z.
static Func<B, Func<A,C>> C<A,B,C>(Func<A, Func<B,C>> x) => y => z => x(z)(y);
/// The Kestrel = λxy.x. Discards argument y.
static Func<B,A> K<A,B>(A x) => (B y) => x;
namespace LogLongOpsConsole
{
using System;
using System.Threading;
using System.Threading.Tasks;
using static System.Console;
using static LogLongOps;
// Demonstrates auto logging of overdue operation for both Actions and Funcs.
// Still awkward to use. Perhaps wrap this in a decorator?
public class Specs
{
[Fact]
public void TestMultipleServiceRegistration()
{
var services = new ServiceCollection()
.AddScoped<IService<Dog>, DogGenericService>() // Valid
.AddScoped<IService<Cat>, CatGenericService>() // Valid
.AddScoped<IService, DogService>() // Valid
.AddScoped<IService, CatService>(); // Invalid. A client with IService ctor injected will get DogService with Scrutor skip
using System;
using static System.Console;
public class UsingDeclarations
{
public static void Main()
{
UseMyDisposible();
WriteLine("Outside scope");
}
using System.Linq;
using static System.Console;
public class TuplePatternMatch
{
private static string FizzBuzz (int i) =>
(i % 3, i % 5) switch
{
(0, 0) => "FizzBuzz",
(0, _) => "Fizz",
(_, 0) => "Buzz",
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Linq;
using static System.Console;
public class PatternMatching
{
public void TypePattern()
{
int? x = 3;