Skip to content

Instantly share code, notes, and snippets.

@EkremDincel
EkremDincel / profile.py
Created July 11, 2021 10:20
Basic Python profiler
from contextlib import contextmanager
from timeit import default_timer as timer
try:
import matplotlib
except ModuleNotFoundError:
PLOT = False
else:
PLOT = True
class Profiler:
def windows(sequence, step):
i = 0
while i < len(sequence):
yield sequence[i: i + step]
i += step
def filled_windows(sequence, step):
i = 0
while i + step <= len(sequence):
yield sequence[i: i + step]
@EkremDincel
EkremDincel / new_array.rs
Created May 4, 2021 23:10
A safe way of creating and initializing huge arrays on the heap in Rust.
#![feature(allocator_api)]
use core::alloc::Layout;
use std::alloc::Allocator;
use std::alloc::Global;
fn new_array<T, const N: usize>(mut initializer: impl FnMut() -> T) -> Box<[T; N]> {
// allocate the array with default allocator by using low level API
let ptr: *mut T = Global
.allocate(Layout::new::<[T; N]>())
// panic on Out of Memory
@EkremDincel
EkremDincel / writeup.md
Created December 8, 2020 19:25 — forked from edmundsmith/writeup.md
Method for Emulating Higher-Kinded Types in Rust

Method for Emulating Higher-Kinded Types in Rust

Intro

I've been fiddling about with an idea lately, looking at how higher-kinded types can be represented in such a way that we can reason with them in Rust here and now, without having to wait a couple years for what would be a significant change to the language and compiler.

There have been multiple discussions on introducing higher-ranked polymorphism into Rust, using Haskell-style Higher-Kinded Types (HKTs) or Scala-looking Generalised Associated Types (GATs). The benefit of higher-ranked polymorphism is to allow higher-level, richer abstractions and pattern expression than just the rank-1 polymorphism we have today.

As an example, currently we can express this type:

@EkremDincel
EkremDincel / enum_meta.py
Created October 7, 2020 12:06
Implementing enumerations in Python using metaclasses.
class EnumMetaDict(dict):
def __init__(self):
self.n = -1
def __getitem__(self, item):
if item.isupper():
self.n += 1
self[item] = self.n
return self.n
from functools import reduce, partial
import itertools as it
from operator import itemgetter, methodcaller
def reversed_partial(f, *args, **kwargs):
def wrapper(*_args, **_kwargs):
f(*_args, *args, **_kwargs, **kwargs)
return wrapper
def unpack(iterable):
@EkremDincel
EkremDincel / enum.py
Created June 14, 2020 13:52
Pythonic Enum example
class MyEnum():
(ONE,
TWO,
THREE,
FOUR,
FIVE) = range(5)
@EkremDincel
EkremDincel / sayıların_okunuşu.py
Created June 1, 2020 16:51
Python'da Türkçe tam sayıların okunuşunu hesaplayan fonksiyon örneği
digit = ['sıfır', 'bir', 'iki', 'üç', 'dört', 'beş', 'altı', 'yedi', 'sekiz', 'dokuz']
digit2 = ['', 'on', 'yirmi', 'otuz', 'kırk', 'elli', 'altmış', 'yetmiş', 'seksen', 'doksan']
digit3 = ['milyon', 'milyar', 'trilyon', 'katrilyon', 'kentilyon', 'sekstilyon', 'septilyon', 'oktilyon', 'nonilyon', 'desilyon', 'andesilyon', 'dodesilyon', 'tredesilyon', 'katordesilyon', 'kendesilyon', 'seksdesilyon', 'septendesilyon', 'oktodesilyon', 'novemdesilyon', 'vicintilyon', 'anvicintilyon', 'dovicintilyon', 'trevicintilyon', 'katorvicintilyon', 'kenkavicintilyon', 'sesvicintilyon', 'septemvicintilyon', 'oktovicintilyon', 'novemvicintilyon', 'tricintilyon', 'antricintilyon']
onluk = lambda i: (digit[i] if i<10 else digit2[i // 10] + " " + digit[i % 10]) if i%10 else digit2[i // 10]
yüzlük = lambda i: onluk(i) if i < 100 else ("yüz " + onluk(i - 100) if i-100 else "yüz") if i < 200 else digit[i // 100] + " yüz " + onluk(i % 100) if i%100 else digit[i // 100] + " yüz"
binlik = lambda i: ("bin" if i == 1 else yüzlük(i) + "
@EkremDincel
EkremDincel / microstream.py
Last active July 15, 2020 00:45
A little TCP socket interface for Python.
import json
import socket
from struct import Struct, calcsize
from select import select
__all__ = ("Server", "Client", "localhostname", "localhost", "LAN", "WAN")
_int = "H" # 0 <= number <= 65535 == 2 ** 16 -1
_size = calcsize(_int)
_struct = Struct("!" + _int)
@EkremDincel
EkremDincel / analysts.py
Last active April 10, 2020 15:37
An example tool for analyzing python class hierarchy statically
import ast
with open("module.py", "r") as f: # we will analysis this file
data = f.read()
tree = ast.parse(data)
classes = {} # {class: [parents]}
for i in tree.body: