Skip to content

Instantly share code, notes, and snippets.

View YBogomolov's full-sized avatar

Yuriy Bogomolov YBogomolov

View GitHub Profile
@YBogomolov
YBogomolov / addbinding.bat
Last active December 26, 2015 12:29
Add IIS website binding from command line
C:\Windows\System32\Inetsrv\appcmd.exe set site /site.name: %1 /+bindings.[protocol='http',bindingInformation='*:80:%2']
@YBogomolov
YBogomolov / gist:8093485
Created December 23, 2013 08:32
Embed third-party assembly as a resource
// 1. Set build action 'Embedded Resourse'.
// 2. Run this handler as early as possible:
AppDomain.CurrentDomain.AssemblyResolve += (sender, args) =>
{
string resourceName = "Your.Assembly.Name.Here." + new AssemblyName(args.Name).Name + ".dll";
using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName))
{
if (stream == null)
@YBogomolov
YBogomolov / fix.js
Last active January 23, 2018 21:46
Y-combinator in JavaScript (ES6) with memoization
// `fix` is an Y-combinator implementation (recursive calls in lambda calculus):
// Y = \f -> (\x -> f (x x))(\x -> f (x x))
// fix :: (a -> a) -> a
const fix = f => (x => f(y => (x(x))(y)))(x => f(y => (x(x))(y)));
// Generator function. Inner signature should correspond to actual function interface.
// mapgen :: ((a -> b) -> [a] -> [b]) -> (a -> b) -> [a] -> [b]
const mapgen = map => f => list => list.length === 0 ? [] : [f(list[0]), ...map(f)(list.slice(1))];
@YBogomolov
YBogomolov / free.ts
Created November 3, 2018 16:41
Free monads example with fp-ts
import { foldFree, liftF } from 'fp-ts/lib/Free';
import { identity, Identity } from 'fp-ts/lib/Identity';
// Task: get all users named 'John' and make them admins
interface User {
id: number;
name: string;
age: number;
isAdmin: boolean;
@YBogomolov
YBogomolov / tf.ts
Last active September 9, 2023 08:29
Tagless Final example with fp-ts
import { HKT, Type, URIS } from 'fp-ts/lib/HKT';
import { identity, Identity, URI } from 'fp-ts/lib/Identity';
import { Monad, Monad1 } from 'fp-ts/lib/Monad';
// Task: get all users named 'John' and make them admins
interface User {
id: number;
name: string;
age: number;
@YBogomolov
YBogomolov / tf.sc
Created November 3, 2018 18:39
Tagless Final example in Scala + Cats
import scala.language.higherKinds
import cats._
import cats.implicits._
final case class User(id: Integer, name: String, age: Integer, isAdmin: Boolean)
val fakeUsers: List[User] = List(
User(1, "John Connor", 32, false),
User(2, "John Smith", 18, false),
User(3, "Mark Brian", 28, false)
@YBogomolov
YBogomolov / syntax.test.ts
Last active November 9, 2018 11:53
TypeScript implementation of Haskell's do-notation
import { identity, Identity } from 'fp-ts/lib/Identity';
import { io, IO } from 'fp-ts/lib/IO';
import { doM } from './syntax';
describe('Monad do-syntax', () => {
beforeAll(() => {
jest.spyOn(Identity.prototype, 'chain');
jest.spyOn(IO.prototype, 'chain');
});
@YBogomolov
YBogomolov / fsm_cb.ts
Created March 24, 2019 09:14
FSM using Circuit Breaker
import AbortController from 'abort-controller';
import { circuitBreaker, defaultBreakerOptions } from 'circuit-breaker-monad/lib';
import { BreakerClosed, BreakerOpen, BreakerState } from 'circuit-breaker-monad/lib/types';
import { Either, left } from 'fp-ts/lib/Either';
import { Lazy } from 'fp-ts/lib/function';
import { IORef } from 'fp-ts/lib/IORef';
import fetch from 'node-fetch';
const fetcher = circuitBreaker<User[]>().run(defaultBreakerOptions);
@YBogomolov
YBogomolov / tagless.ts
Created April 30, 2019 06:17
#monadicmonday ep.5: Tagless Final
import { log } from 'fp-ts/lib/Console';
import { Type, URIS } from 'fp-ts/lib/HKT';
import { identity, URI as IdURI } from 'fp-ts/lib/Identity';
import { none, Option, some } from 'fp-ts/lib/Option';
import { randomInt } from 'fp-ts/lib/Random';
import { fromIO, Task, task, URI as TaskURI } from 'fp-ts/lib/Task';
import { createInterface } from 'readline';
export interface ProgramSyntax<F extends URIS, A> {
map: <B>(f: (a: A) => B) => _<F, B>;
@YBogomolov
YBogomolov / never-intersect.ts
Created May 15, 2019 12:17
Type-level laws: "function arguments should never intersect"
export type If<T, U, True, False> = [T] extends [U] ? True : False;
export type IfDef<T, Yep, Nope> = If<T, never, Nope, Yep>;
export type Intersect<A extends {}, B extends {}> =
Pick<A, Exclude<keyof A, Exclude<keyof A, keyof B>>> extends { [x: string]: never } ?
never :
Pick<A, Exclude<keyof A, Exclude<keyof A, keyof B>>>;
const f = <
A extends {},
B extends {},