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.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 / 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 {},
@YBogomolov
YBogomolov / profunctors.ts
Last active September 12, 2019 06:19
Profunctor instances for UpStar and DownStar
import { Functor } from 'fp-ts/lib/Functor';
import { HKT, Kind3, URIS3 } from 'fp-ts/lib/HKT';
export type Fn<A, B> = (a: A) => B;
export interface Profunctor<F extends URIS3, G> {
dimap: <A, B>(ab: Fn<A, B>) => <C, D>(cd: Fn<C, D>) => (fbc: Kind3<F, G, B, C>) => Kind3<F, G, A, D>;
}
export type UpStar<F, A, B> = (a: A) => HKT<F, B>;
@YBogomolov
YBogomolov / AppendInjective.idr
Last active May 2, 2020 10:17
Proof that a ++ b = a ++ c -> b = c and a ++ c = b ++ c -> a = b
module AppendInjective
import Data.List.Views
%access export
%default total
appendInjectiveRight : (a, b, c : List x) -> a ++ b = a ++ c -> b = c
appendInjectiveRight [] _ _ prf = prf
appendInjectiveRight (_ :: xs) b c prf = appendInjectiveRight xs b c (cong { f = drop 1 } prf)