Skip to content

Instantly share code, notes, and snippets.

@hitrik
hitrik / slide_window.ts
Created July 25, 2023 11:17
Алгоритм бегущего окошка
// Алгоритм "бегущего окна"
// Сложность - O(k*n), где n - длина массива, k - размер искомой последовательности
// Найти макс сумму из трех последовательных элементов в массиве
const arrInt = [1, 4, 7, 2, 34, 9, 0, 1, 5];
// функция принимает входной массив числел и какое кол-во элементов брать для подмассива
function findMaxSummSubArray(arr: number[], k: number) {
// сделаем две переменных, одну для хранения суммы, вторая запишем длину входного массива
let maxSum: number = 0;
@hitrik
hitrik / machine.js
Last active May 1, 2021 17:12
Generated by XState Viz: https://xstate.js.org/viz
// Available variables:
// - Machine
// - interpret
// - assign
// - send
// - sendParent
// - spawn
// - raise
// - actions
@hitrik
hitrik / machine.js
Created April 27, 2021 20:58
Generated by XState Viz: https://xstate.js.org/viz
// Available variables:
// - Machine
// - interpret
// - assign
// - send
// - sendParent
// - spawn
// - raise
// - actions
@hitrik
hitrik / machine.js
Last active April 25, 2021 21:30
Generated by XState Viz: https://xstate.js.org/viz
const startUserAuth = () => new Promise(res => setTimeout(res('qazzaq'), 1000))
const userMachine = Machine({
id: 'user',
context: {
user: null,
error: false
},
initial: 'idle',
states: {
const useAnimationUnmount = (duration = 300, useNativeDriver = true) => {
const [animation] = useState(() => new Animated.Value(1));
const [unmount, setUnmount] = useState(false);
const start = useCallback(
() =>
Animated.timing(animation, {
toValue: 0,
useNativeDriver,
duration,
import React, { Component } from 'react';
import { render } from 'react-dom';
import {Subject, interval} from "rxjs";
import {takeUntil, tap} from "rxjs/operators";
//hook creates subject that will destroy on unmount component
const useDestroyedSubject = () => {
const subject$ = new Subject();
React.useEffect(() => () => {
console.log("destroy subject");
//wrapper for react tree with multi fetch
import React, { useState, useEffect, useMemo } from "react";
const apiUrl = `https://jsonplaceholder.typicode.com`;
const isValid = (checker, ...args) =>
args.filter(checker).length === args.length;
const fetch = async ({ url, ...options }) => {
try {
//Reactjs custom hook
export const useFetch = ({ url, ...options = {} }) => {
let [data, setData] = useState(null);
const [error, setError] = useState(null);
const [status, setStatus] = useState(null);
const [loading, setLoading] = useState(true);
const ref = useRef();
useEffect(
() => {
@hitrik
hitrik / rxjs-wrapper-socket-io.js
Last active August 14, 2018 19:07
Reactive wrapper for Socket.io powered by rxjs@6.2.0
/*
* Reactive wrapper for Socket.io powered by rxjs@6.2.0
*/
const io = require("socket.io");
const { pipe, of, fromEvent, timer } = require("rxjs");
const {
map,
merge,
takeUntil,
@hitrik
hitrik / fetch-with-compose.js
Last active March 15, 2018 10:28
some experience with compose
//compose
const compose = (...fns) => arg =>
fns.reduce((composed, fn) => fn(composed), arg);
//fetch all urls
const fetchUrls = urls => urls.map(url => fetch(url));
//parse body like json
const parseResponses = responses =>
responses.map(response => response.then(data => data.json()));