Skip to content

Instantly share code, notes, and snippets.

View lagunoff's full-sized avatar

Vladislav Lagunov lagunoff

View GitHub Profile
@lagunoff
lagunoff / Migrations.hs
Last active December 2, 2023 12:35
Simple Forward Database Migrations Written in Haskell
module Migrations
( dbMigrate
, openAndMigrate
) where
import Control.Exception
import Control.Monad
import Control.Monad.Except
import Data.List qualified as List
import Data.Map (Map)
@lagunoff
lagunoff / spy-http.hs
Created July 2, 2021 18:28
Spy after HTTP requests and responses in haskell
module SpyHttp where
import Data.ByteString as BS
import Network.HTTP.Client
import Network.HTTP.Client as Http hiding (Proxy)
import Network.HTTP.Client.Internal
data HttpSpyEnv = HttpSpyEnv
{ manager :: Manager
, incoming_ref :: IORef [Either ByteString ByteString]

1. Problem with sending consequative requests

-- Currently we write
do 
  eA <- reqSuccess <$> apiGetA
  eB <- reqSuccess <$> apiGetB
  dA <- holdDyn Nothing eA
  dB <- holdDyn Nothing eB
  dC <- f <$> dA <*> dB
@lagunoff
lagunoff / effects.ts
Last active September 22, 2019 21:38
Attempt on extensible effects in typescript
// Evidence for type equality
type Eq<A, B> = { __typeEq__: Eq1<A, B>};
type Eq1<A, B> = [A, B] & typeof eqSymbol;
const eqSymbol = Symbol('eqSymbol');
const runEq = <A, B>(eq: Eq<A, B>, a: A): B => a as any;
type Fn<Args extends any[], R> = (...args: Args) => R;
// Component messages, `A` is the result of evaluating the message in `update`
type Msg<A = unknown> =
@lagunoff
lagunoff / jexp.ts
Last active August 26, 2019 21:20
Simple functional language embedded in JSON
type Ident = string;
type Bindings = Record<Ident, unknown>;
type List<T> = null | Cons<T>;
type Cons<T> = { 0: T, 1: List<T> };
type JExp = unknown;
type JVal = unknown;
function Cons<T>(x: T, xs: List<T>): List<T> {
return [x, xs];
}
@lagunoff
lagunoff / optics.ts
Last active August 26, 2019 22:01
Very basic concrete optics is typescript
export type Optic<A, B> =
| Iso<A, B>
| Lens<A, B>
| Prism<A, B>
| Compose<A, B>
;
export type Iso<A, B> = {
apply: (a: A) => B;
unapply: (b: B) => A;
@lagunoff
lagunoff / rest-spread-react.ts
Last active June 20, 2019 09:22
Solving rest-spread problem with props in ReactJS
type Props = DivProps & {
title: number;
isActive: boolean;
};
class Todo extends React.Component<Props> {
render() {
const { title, isActive, ...divProps } = this.props;
return <div {...the<DivProps>(divProps) }>
<input type="checkbox" value={isActive}/>
@lagunoff
lagunoff / variant.ts
Last active September 19, 2019 18:01
typescript variants
// Alternative sum types for typescript. Variants may be more convenient than unions
// because they easier to manipulate (see `Mapped types`)
// - [https://www.typescriptlang.org/docs/handbook/advanced-types.html](Mapped types)
// - [https://github.com/natefaubion/purescript-variant]
const variantSymbol = Symbol('Variant');
type Variant<T> = { 0: keyof T, 1: T[keyof T]; [variantSymbol]: T };
// Variant constructor
function variant<T, K extends keyof T>(key: K, value: T[K]): Variant<T> {