Skip to content

Instantly share code, notes, and snippets.

@emrul
emrul / heap-sample.py
Created May 28, 2024 10:35 — forked from timvieira/heap-sample.py
Fast sampling from an evolving distribution
import numpy as np
from numpy.random import uniform
def update(S, k, v):
"Update value position `k` in time O(log n)."
d = S.shape[0]
i = d//2 + k
S[i] = v
while i > 0:
@emrul
emrul / Consumer.kt
Created January 31, 2022 23:25 — forked from cyberdelia/Consumer.kt
Kafka + Kotlin + Coroutines
package com.lapanthere.bohemia
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import org.apache.kafka.clients.consumer.ConsumerRecord
import org.apache.kafka.clients.consumer.KafkaConsumer
import java.time.Duration
fun <K, V> KafkaConsumer<K, V>.asFlow(timeout: Duration = Duration.ofMillis(500)): Flow<ConsumerRecord<K, V>> =
@emrul
emrul / ComponentBag.kt
Created February 26, 2021 23:25 — forked from avwie/ComponentBag.kt
Usage of companion objects
package nl.avwie
interface Tag<T>
interface Tagged<T> {
val tag: Tag<T>
}
class ComponentBag {
private val components = mutableMapOf<Tag<*>, Any>()
@emrul
emrul / useful_pandas_snippets.py
Created March 18, 2018 10:29 — forked from bsweger/useful_pandas_snippets.md
Useful Pandas Snippets
# List unique values in a DataFrame column
# h/t @makmanalp for the updated syntax!
df['Column Name'].unique()
# Convert Series datatype to numeric (will error if column has non-numeric values)
# h/t @makmanalp
pd.to_numeric(df['Column Name'])
# Convert Series datatype to numeric, changing non-numeric values to NaN
# h/t @makmanalp for the updated syntax!
@emrul
emrul / enum.sql
Created February 9, 2018 10:41 — forked from d11wtq/enum.sql
Renaming an ENUM label in PostgreSQL
/*
Assuming you have an enum type like this.
You want to rename 'pending' to 'lodged'
*/
CREATE TYPE dispute_status AS ENUM('pending', 'resolved', 'open', 'cancelled');
BEGIN;
ALTER TYPE dispute_status ADD VALUE 'lodged';
UPDATE dispute SET status = 'lodged' WHERE status = 'pending';
@emrul
emrul / dl4jRNNStockTimeSeriesRegressionExample.txt
Created January 22, 2017 15:30 — forked from ddrummond/dl4jRNNStockTimeSeriesRegressionExample.txt
DL4J Example of Time Series Regression with 2 Outputs:
/*
Problem Description:
Time series forecast for a single securities minor reversal points.
A "minor reversal point" is defined as either a period (day) with a high price greater than both the previous and next high prices, or a period with a low value lower than both the previous and next low prices.
This is a time series regression problem with 8 input features and 2 regression output dimensions
Input Features:
- period return r = (p2 - p1) / p1, descr: a linear return value, typically [-0.05,0.05]
- period volume, descr: standardized, z = (x - Mean) / SD
- High-Low spread, descr: standardized, z = (x - Mean) / SD
@emrul
emrul / tardis.sql
Created August 24, 2016 11:44 — forked from iperdomo/tardis.sql
-- psql -U postgres -h localhost -f /path/to/tardis.sql
SET statement_timeout = 0;
SET lock_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SET check_function_bodies = false;
SET client_min_messages = warning;
SET row_security = off;
@emrul
emrul / README.md
Created April 28, 2016 00:18 — forked from vjt/README.md
[POC] Temporal database system on PostgreSQL with Active Record in mind

This has become a Ruby gem: https://github.com/ifad/chronomodel

A temporal database system on PostgreSQL using table inheritance and the rule system.

This is a data structure for a Slowly-Changing Dimension Type 2 temporal database, implemented using only PostgreSQL >= 9.0 features.

Any application code is completely unaware of the temporal features: queries are done against a view that behaves exactly like a plain table (it can be SELECTed, UPDATEd, INSERTed INTO and DELETEd FROM), but behind the scenes the database redirects the queries to backend tables holding actual data, using the PostgreSQL rule system.

@emrul
emrul / IOUtil.java
Created April 20, 2016 00:30 — forked from awilmore/IOUtil.java
Fast nio InputStream to OutputStream Copy
package com.awilmore.ioutils;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.nio.channels.WritableByteChannel;
@emrul
emrul / README.md
Created March 16, 2016 00:04 — forked from lovasoa/README.md
Compute the intersection of large arrays in javascript

Array intersect

Fastest function to intersect a large number of big arrays in javascript, without dependencies

  • The compressed version is only 345 caracters long.
  • Faster than common libraries, even a large number of arrays, or on very big arrays. (See benchmarks)

Usage