Skip to content

Instantly share code, notes, and snippets.

View greyblake's full-sized avatar
🇺🇦
#StandWithUkraine

Serhii Potapov greyblake

🇺🇦
#StandWithUkraine
View GitHub Profile
@greyblake
greyblake / Result.ts
Created December 22, 2021 20:22
Result in TypeScript with normal pattern matching
type ResultInner<T, E> =
| { type: 'OK', value: T }
| { type: 'ERROR', error: E };
interface MatchHandlers<T, E, R> {
ok: (value: T) => R,
err: (error: E) => R,
}
class Result<T, E> {
// USAGE EXAMPLE
render() {
const { currentUserState, nearEnv } = this.state;
return currentUserState.match({
notAsked: () => <HomePage />,
loading: () => <HomePage />,
failure: (err) => <>Error occurred</>,
success: (user) => <AppRouter currentUser={user} nearEnv={nearEnv} />,
@greyblake
greyblake / whatlang_vs_lingua.rs
Created April 22, 2021 16:31
Whatlang VS Lingua Performance
use lingua::LanguageDetectorBuilder;
use std::time::Instant;
const N: usize = 10_000;
fn main() {
let goethe = "Wir sollen eben nicht in Ruhe bleiben! Gleich wird uns, wenn wir zu genießen denken, Zur Übung unsrer Tapferkeit ein Feind, Zur Übung der Geduld ein Freund gegeben";
let pushkin = "Я помню чудное мгновенье: Передо мной явилась ты, Как мимолетное виденье, Как гений чистой красоты.";
let detector = LanguageDetectorBuilder::from_all_languages().build();
@greyblake
greyblake / rust-playground.sh
Last active March 12, 2021 16:56
Creates new rust project and runs `cargo watch` with vim in tmux
#!/bin/sh
PLAYGROUNDS_DIR="/tmp/rust-playgrounds"
TIMESTAMP=$(date +"%Y%m%d%H%M%S")
PROJECT_DIR="${PLAYGROUNDS_DIR}/playground${TIMESTAMP}"
cargo new $PROJECT_DIR
cd $PROJECT_DIR
# Add dependencies
@greyblake
greyblake / http-poll.spec.ts
Last active December 2, 2018 21:23
http-poll
import { throwError, of, interval, Observable } from 'rxjs';
import { take, filter, map, concatMap, catchError } from 'rxjs/operators';
import { httpPoll, isNotReady } from './http-poll';
function buildError(code) {
return {
error: {
errors: [{ code }]
}
@greyblake
greyblake / .travis.yml
Created September 15, 2018 14:40
Minimal rust setup for travis ci
language: rust
rust:
- stable
install:
- rustup component add rustfmt-preview
- rustup component add clippy-preview
script:
- cargo fmt -- --check
- touch ./src/main.rs && cargo clippy -- -D warnings
- cargo test
require "securerandom"
require "digest/md5"
require "benchmark"
data1 = SecureRandom.random_bytes(5 * 1024 * 1024)
data2 = data1.dup
N = 500
Benchmark.bm do |x|
@greyblake
greyblake / log_exec.rs
Created January 17, 2018 21:09
Log exec
use std::time::Duration;
use std::time::Instant;
// Executes `func` and logs warning or error message if it takes longer than specified.
pub fn log_exec<F, T>(label: &str, warn_millis: u64, error_millis: u64, func: F) -> T where F: FnOnce() -> T {
let warn_duration = Duration::from_millis(warn_millis);
let error_duration = Duration::from_millis(error_millis);
let start = Instant::now();
let output = func();
macro_rules! list_enum {
( $type:ident, { $($el:ident),* } ) => {
#[derive(Debug,Clone,Copy)]
enum $type{
$($el),*
}
impl $type {
pub fn list() -> &'static[$type] {
const LIST : &'static[$type] = &[$($type::$el),*];
extern crate hyper;
extern crate tokio_core;
extern crate hyper_tls;
extern crate futures;
extern crate serde_json;
extern crate serde;
#[macro_use]
extern crate serde_derive;
use hyper::header;