Skip to content

Instantly share code, notes, and snippets.

View ddoronin's full-sized avatar
🏠
In The Matrix

Dima Doronin ddoronin

🏠
In The Matrix
View GitHub Profile
@ddoronin
ddoronin / bs.ts
Last active January 25, 2023 18:58
function bs(nums: number[], x: number) {
let left = 0;
let right = nums.length - 1;
while (left <= right) {
const k = Math.floor((left + right) / 2);
if (nums[k] === x) return k;
if (nums[k] < x) left = k + 1;
else right = k - 1;
}
return -1;
@ddoronin
ddoronin / uf.ts
Last active January 25, 2023 00:40
Union Find
class UF {
public vertices: Array<number>;
constructor(n: number) {
this.vertices = Array.from({length: n}, (_, i) => i);
}
union(x: number, y: number){
this.vertices[this.find(x)] = this.find(y);
}
@ddoronin
ddoronin / hook$.ts
Created January 8, 2023 04:14
React hook for RxJS
export function use$<T>(subject$: BehaviorSubject<T>){
const [state, setState] = React.useState<T>(subject$.value);
React.useEffect(() => {
const subscription = subject$.subscribe({
next(data){
setState(data)
}
});
return () => {
subscription.unsubscribe();
{time, your_func_result} = :timer.tc(&your_func/arity, [your_func-args])
If you have discovered this function, please upvote the original answer by Sainik on stackoverflow here:
https://stackoverflow.com/questions/29668635/how-can-we-easily-time-function-calls-in-elixir
@ddoronin
ddoronin / actor.rs
Last active June 1, 2022 03:22
Actor System implementation in Rust
use std::fmt::Debug;
use std::future::Future;
use tokio::sync::mpsc;
use tokio::sync::{oneshot};
use tokio::sync::mpsc::{Sender, Receiver};
pub enum ActorMessage<State, Action> {
Message(Action),
Reply(oneshot::Sender<State>),
}
class DSU {
private p: number[];
constructor(n: number) {
this.p = new Array(n).fill(0).map((_, i) => i);
}
find(x: number) {
while(this.p[x] !== x) {
x = this.p[x];
class BinaryHeap<T> {
private harr: T[] = [];
constructor(private lessThan: (a:T, b:T) => boolean) {
}
size() {
return this.harr.length;
}
@ddoronin
ddoronin / requestor.tsx
Created April 1, 2019 03:18
Requestor Component
import * as React from "react";
import { useRx, useRxTap } from "src/useRx";
import { some, none } from "monas";
import { AppStateContext } from "src/state/AppState";
import Headers from "./headers";
import Response from "./response";
export default function Requestor() {
const appState = React.useContext(AppStateContext);
const [http, httpError] = useRx(appState.http$, {
@ddoronin
ddoronin / http.ts
Created April 1, 2019 02:49
overHttp
import { ajax } from "rxjs/ajax";
import { switchMap, map, merge, catchError } from "rxjs/operators";
import { BehaviorSubject, of } from "rxjs";
import { Option, some, none } from "monas";
import { IRequest } from "src/models/request-composer";
export const overHttp = switchMap((_: Option<IRequest>) =>
_.map(req =>
new BehaviorSubject({ isLoading: true, req: _, resp: none }).pipe(
merge(
@ddoronin
ddoronin / History.tsx
Created April 1, 2019 02:34
History React Component
import * as React from "react";
import { useRx } from "src/useRx";
import { AppStateContext } from "src/state/AppState";
import { some } from "monas";
import styles from "./styles.module.scss";
import { IRequest } from "src/models/request-composer";
export default function History() {
const appState = React.useContext(AppStateContext);
const [requests] = useRx(appState.last5$, []);