Skip to content

Instantly share code, notes, and snippets.

View chemacortes's full-sized avatar

Chema Cortés chemacortes

View GitHub Profile
@chemacortes
chemacortes / benchmarks.scala
Last active February 3, 2024 14:35
Benchmarks in scala
def benchmarks[A](f: => A) =
inline def memory() = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()
val t0 = System.nanoTime()
val m0 = memory()
val ret = f
val t1 = System.nanoTime()
val m1 = memory()
@chemacortes
chemacortes / fibo_matrix.py
Last active January 30, 2024 09:41
fibonacci - matrix exponentiation
import numpy as np
import sys
sys.set_int_max_str_digits(0)
def matrix_power(A: np.matrix, n: int) -> np.matrix:
if n == 0:
return np.matrix( ((1,0),(0,1)), dtype=object )
elif n%2 == 1:
@chemacortes
chemacortes / last_execution_time.py
Created September 28, 2023 16:30
Decorator that calc the last execution time of a function. The result is added as a new function attribute named 'last_execution_time'.
import time
import functools
def timer_decorator(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
start = time.time_ns()
res = func(*args, **kwargs)
end = time.time_ns()
wrapper.last_execution_time = end - start
@chemacortes
chemacortes / fibs.scala
Last active January 27, 2024 01:04
Fibonacci succession with scala3 (oneline versions)
val fibs: LazyList[BigInt] = BigInt(1) #:: BigInt(1) #:: (fibs zip fibs.tail).map(_+_)
val fibs: LazyList[BigInt] = 1 #:: fibs.scan(BigInt(1))(_+_)
val lucas: LazyList[BigInt] = 2 #:: fibs.scan(BigInt(1))(_+_)
@chemacortes
chemacortes / longpath.py
Last active November 14, 2022 15:16
Clase LongPath para usar rutas largas en windows
from pathlib import WindowsPath
class LongPath(WindowsPath):
uncprefix = "\\\\?\\"
def __new__(cls, *pathsegments):
ins = super().__new__(cls, *pathsegments)
abspath = ins.expanduser().absolute()
match abspath.parts:
case (drive, *parts) if not drive.startswith(cls.uncprefix):
from itertools import count, takewhile
def calc_pi(error: float = 1e-6) -> float:
terms = (1 / (2 * k - 1) for k in count(1))
it = takewhile(lambda x: 2 * x >= error, terms)
ts = list(x - y for x, y in zip(it, it))
pi = 4 * sum(ts)
calc_pi.error = error
@chemacortes
chemacortes / primes.py
Last active February 25, 2021 19:48
Primes generator in python
import sys
from bisect import bisect, bisect_left
from collections.abc import Generator, Iterable, Iterator
from functools import singledispatchmethod
from itertools import islice
from math import isqrt
INFINITE = sys.maxsize # una mala aproximación de infinito
Prime = int # un alias para los primos
@chemacortes
chemacortes / Fmt.elm
Last active August 4, 2020 21:06
Padding in elm
module Fmt exposing (..)
import String
type FmtPadding
= Default
| Left Int Char
| Right Int Char
| Center Int Char
@chemacortes
chemacortes / futur.py
Created April 23, 2020 22:29
Simulación de uso de futures masivos
# -*- coding: utf-8 -*-
"""
Created on Fri Jun 12 18:29:33 2015
@author: chema
"""
from concurrent.futures import (
Future, ProcessPoolExecutor, ThreadPoolExecutor, TimeoutError, as_completed)
from itertools import islice
@chemacortes
chemacortes / birthday.scala
Last active February 27, 2020 00:26
Formula for calculating the birthday paradox
def birthday(n: Int) = {
1 - (0 until n).map(1 - BigDecimal(_) / 365).product
}
assert(birthday(23) == BigDecimal("0.5072972343239854072254172283370325"))