Skip to content

Instantly share code, notes, and snippets.

@KhraksMamtsov
KhraksMamtsov / effect_cli_exercise.ts
Created October 15, 2024 18:52
Effect Cli Exercise
import { Args, Command } from "@effect/cli";
import { NodeContext, NodeRuntime } from "@effect/platform-node";
import { Path, FileSystem as FS } from "@effect/platform";
import { Effect, Stream } from "effect";
import * as Schema from "@effect/schema/Schema";
// Схема статистики файла
const StatsContract = Schema.Struct({
chars: Schema.Number,
lines: Schema.Number,
words: Schema.Number,
export type JsonPrimitive = boolean | number | string | null;
export type JsonArray = ReadonlyArray<Json>;
export type JsonRecord = { readonly [K in string]: Json };
export type Json = JsonPrimitive | JsonArray | JsonRecord;
const from =
<From>() =>
<R, To>(schema: Schema.Schema<To, From, R>) =>
schema;
@KhraksMamtsov
KhraksMamtsov / Do.ts
Created August 29, 2022 09:45
Do notation for rxjs
import { Observable, of } from "rxjs";
import { map, flatMap } from "rxjs/operators";
function bindTo<A, N extends string>(name: N) {
return function (from: Observable<A>) {
return from.pipe(
map((x) => ({ [name]: x } as Record<N, A>))
);
};
}
type Iterableify<T> = Readonly<{ [K in keyof T]: Iterable<T[K]> }>;
type IterableifiedToTuple<X extends Iterableify<unknown>> = Readonly<
{
[K in keyof X]: X[K] extends Iterable<infer Y> ? Readonly<Y> : never;
}
>;
type IterableifiedToArrays<X extends Iterableify<unknown>> = Readonly<
{
[K in keyof X]: X[K] extends Iterable<infer Y> ? ReadonlyArray<Y> : never;
export function* subtractWith<TValue, TSelector = unknown>(
selector: (x: TValue) => TSelector,
minuend: Iterable<TValue>,
subtrahend: Iterable<TValue>
): Generator<TValue> {
for (const minuendEl of minuend) {
const comparableA = selector(minuendEl);
let isInB = false;
for (const subtrahendEl of subtrahend) {
/**
* Split iterable into two arrays with splitter predicate (may be TypeGuard).
*
* Example:
type AB = { a: number; b: number };
type A1 = { a: 1; b: number };
type A2 = { a: 2; b: number };
const isA1 = (ab: AB): ab is A1 => ab.a === 1;
const data: AB[] = [
@KhraksMamtsov
KhraksMamtsov / Vector.ts
Last active June 21, 2021 08:40
homogeneous tuple
type _VectorOf<T, N extends number, R extends readonly T[]> = R['length'] extends N
? R
: _VectorOf<T, N, readonly [T, ...R]>;
type VectorType<V extends Vector<any, number>> = V extends Vector<infer T, number> ? T : never;
type VectorLength<V extends Vector<any, number>> = V['length'];
export type Vector<T, N extends number> = N extends N
? number extends N
? readonly T[]
export type TNonEmptyArray<T> = readonly [T, ...T[]] & { readonly length: number };
export const NonEmptyArray = {
convert<T>(array: readonly T[]): TNonEmptyArray<T> | undefined {
if (array.length > 0) {
return NonEmptyArray.create(...(array as [T, ...T[]]));
}
},
create<T>(...elements: readonly [T, ...T[]]): TNonEmptyArray<T> {
return [...elements];
@KhraksMamtsov
KhraksMamtsov / StringWithSubstring.ts
Created June 21, 2021 05:54
String with substring
import { InitReadonly, Tag } from '../internal';
import { TNonEmptyArray } from './NonEmptyArray';
import { TNonEmptyArray2 } from './NonEmptyArray2';
type _Split<SS extends string, S extends string> = S extends `${infer L}${SS}${infer R}`
? readonly [L, ..._Split<SS, R>]
: readonly [S];
export type Split<SS extends string, S extends string> = SS extends ''
? S extends ''