Skip to content

Instantly share code, notes, and snippets.

@captain-yossarian
captain-yossarian / actionFactory.ts
Last active June 13, 2019 16:22
TS Redux action factory
import { Action } from 'redux';
export enum Constants {
ACTION_NAMESPACE = 'ACTION_NAMESPACE',
}
export type ActionPayload<T, P> = Action<T> & { payload: P };
interface IPayload{
id: string;
import { Constants as _, createAction } from './actionFactory';
export const initialAction = createAction<_.ACTION_NAMESPACE>(_.ACTION_NAMESPACE);
import { ActionPayload, AllActionsPayload, Constants, IActionMap } from './actionFactory';
type ReducerCase<P> = (state: IStore, payload: P) => IStore;
type IReducer = { [P in keyof Record<Constants, ReducerCase<AllActionsPayload>>]: ReducerCase<IActionMap[P]> };
export const reducer: IReducer = {
ACTION_NAMESPACE: (state, payload) => {
const { id, mode } = payload;
return state
@captain-yossarian
captain-yossarian / kmeans.rs
Created September 24, 2019 07:29
kmeans algorithm
use rand::{thread_rng, Rng};
pub fn mean(numbers: Vec<f32>) -> f32 {
numbers.iter().fold(0.0, |acc, value| acc + value) / numbers.len() as f32
}
pub fn distance(a: [f32; 2], b: [f32; 2]) -> f64 {
(((b[0] - a[0]).powf(2.0) + (b[1] - a[1]).powf(2.0)) as f64).sqrt()
}
@captain-yossarian
captain-yossarian / log.txt
Created January 19, 2020 18:35
Bandwhich test log
running 41 tests
test tests::cases::raw_mode::bi_directional_traffic ... FAILED
test tests::cases::raw_mode::multiple_connections_from_remote_address ... FAILED
test tests::cases::raw_mode::multiple_packets_of_traffic_from_different_connections ... FAILED
test tests::cases::raw_mode::multiple_packets_of_traffic_from_single_connection ... FAILED
test tests::cases::raw_mode::multiple_processes_with_multiple_connections ... FAILED
test tests::cases::raw_mode::no_resolve_mode ... FAILED
test tests::cases::raw_mode::one_ip_packet_of_traffic ... FAILED
test tests::cases::raw_mode::one_packet_of_traffic ... FAILED
@captain-yossarian
captain-yossarian / index.md
Last active September 6, 2021 17:53
TypeScript presentation

Typing React Props


Component overloading

import { FC } from "react";

type WithName = { name: string };

To create anchor links that jump down to different sections of a README (as in an interactive table of contents), first create a heading:
#Real Cool Heading

The anchor link for that heading is the lowercase heading name with dashes where there are spaces. You can always get the anchor name by visiting the README on Github.com and clicking on the anchor that appears when you hover to the left of the heading. Copy everything starting at the #:
#real-cool-heading

Wherever you want to link to your Real Cool Heading section, put your desired text in brackets, followed by the anchor link in parentheses:
[Go to Real Cool Heading section](#real-cool-heading)

Привіт, мене звати Сергій. Працюю 4 роки як Front End програміст, здебільшого з TypeScript. Ця стаття буде присвячена Union типам. У цій статті, я спробую показати коли краще ужити Union, як правильно і не правильно з ними проацювати. Більшість прикладів, які я описуватиму - взяті із запитань на StackOverflow, тобто скоріш за все будуть корисні з практичної точки зору. Прошу вибачення наперед, що використовуватиму англійські слова, оскільки не завжди можу підібрати правильний переклад.

Unions

В багатьох випадках краще використати Union замість Enum. По-перше тільки для того, що Union не займає місця. По друге Enums мають свої недоліки.

Приклад:

{
const obj = {
user: {
name: 'John'
}
}
const result1 = deepPickFinal(obj, 'user') // { name: string; }
const result2 = deepPickFinal(obj, 'user', 'name') // string
}
/**
* Shamelessly stolen from Rust
*/
type None = null | undefined;
type Some<T> = T
type Option<T> = Some<T> | None