Skip to content

Instantly share code, notes, and snippets.

@fvilante
fvilante / Meiosis.ts
Last active July 8, 2021 09:54
Meiosis pattern in typescript (a simple example)
// uses 'flyd' stream micro-library
type Stream = (_: number) => Stream // this life is a simplification
interface Model {
value: number
}
interface Actions {
increment: () => void
@fvilante
fvilante / StaticLenses.ts
Created May 20, 2019 13:58
Static Lenses prove of concept in Typescript
// =======================================================================
// Lenses
// =======================================================================
type Setter<A, B extends keyof A> = Func2<A, A[B], A[B]>
type Getter<A, B extends keyof A> = Func<A, A[B]>
type Property<A, B extends keyof A> = B
@fvilante
fvilante / AJSonDescribingTheJson.json
Created May 14, 2019 18:03
A Json format to describe the Json format: Aspects of Recurrency in Descriptive Programming
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "http://json-schema.org/draft-07/schema#",
"title": "Core schema meta-schema",
"definitions": {
"schemaArray": {
"type": "array",
"minItems": 1,
"items": { "$ref": "#" }
},
@fvilante
fvilante / CrazyFunctor.ts
Created May 14, 2019 15:36
Don't know why but this seems very impressive to me
// Don't know why but this seems very impressive to me
type Func<A, B> = (_: A) => B
class Value<A> {
constructor(private _content: A ) { }
static of = <A>(data: A): Value<A> => new Value(data)
interface Entry {
value: string
label: string
}
type Dictionary = Entry[]
const dictionary: Dictionary = [
{ value: 'primeiro', label: '1 - Primeiro' },
{ value: 'segundo', label: '2 - Segundo' },
@fvilante
fvilante / Pipe.ts
Last active August 17, 2019 01:40
Function composition
// A 100% type-safe solution to compose functions in typescript
// tested in TS 3.5.1
type Func<A, B> = (_: A) => B
class ComposedFn<A, B> {
constructor(private f: Func<A, B>) { }
static of = <A,B>(fn: Func<A, B>): ComposedFn<A, B> =>
new ComposedFn(fn)
@fvilante
fvilante / MessageDispatcher.ts
Last active May 9, 2019 07:19
Modeling of parametrized Messages and a Generic Dispatcher
// ============================================================
// Sample code:
// Modeling of parametrized Messages and a Generic Dispatcher
//
// Author: @fvilante
// ============================================================
// Messages (or events, or actions...)
@fvilante
fvilante / Functor.ts
Created April 18, 2019 21:58
An example of Functor in TypeScript. You can run this on https://www.typescriptlang.org/play/
// ==============================================
// PROPOSAL OF SOLUTION
// ==============================================
// ================================
// Emulate nominal type-system
// ================================
type Kind<K> = { kind: K }
type Value<T> = { value: T }
// Demonstration of Core Concepts
// Event Source Reducer functional style and typesafe
// =========================
// Lib Event Source
// =========================
type UserDocument = object
type State = ReadonlyArray<object>
import { mapObjIndexed } from 'ramda'
type PropMorphism
<TKeyValue, TContext extends object, TKeyName, TResult> =
(value: TKeyValue, context: TContext, keyname: TKeyName) =>
TResult
/**
* Use HasExactlySameKeys if you want to assure to procced