Skip to content

Instantly share code, notes, and snippets.

View superbobry's full-sized avatar

Sergei Lebedev superbobry

View GitHub Profile
@superbobry
superbobry / treap.ml
Created December 14, 2011 23:17
basic treap implementation
(** Task X: Treap. *)
module Treap = struct
type ('a, 'b) t =
| Empty
| Node of ('a, 'b) t * 'a * 'b * ('a, 'b) t
let empty = Empty
let rec merge l r = match (l, r) with
@superbobry
superbobry / gist:1617642
Created January 15, 2012 22:03
First class modules and functors
# module M = Map.Make((val (module String : Map.OrderedType) : Map.OrderedType));;
module M :
sig
type key
type +'a t
end
# module M = Map.Make(String);;
module M :
sig
type key = String.t
@superbobry
superbobry / gist:1917188
Created February 26, 2012 15:00
Iterable unpacking in Python
>>> def f((x, y)): return x + y
...
>>> def gen():
... yield 1
... yield 2
...
>>>
>>> f(gen())
3
@superbobry
superbobry / task1.py
Created May 25, 2012 22:33
Bioinf. Algorithms Homework
# -*- coding: utf-8 -*-
# from __future__ import print_function
import csv
import itertools
import errno
import os
import os.path
import sys
@superbobry
superbobry / ChromaSig_seq.pl
Created August 8, 2012 11:43
The Joy of Reading Code Written in Academia
# See http://bioinformatics-renlab.ucsd.edu/rentrac/wiki/ChromaSig
sub init_gibbs_seed {
# ...
# print("score = $score\n");
# my ($seed, $ave_dist) = tournament_sort($id, $profiles_c,
# $clusters_arrayref, $filtered_data);
# my $norm = $sorted_norm[$percentile + $i * $interval]->{norm};
# my $score = $norm / $ave_dist;
def foo():
try:
foo()
finally:
foo()
# >>> foo()
# ???
@superbobry
superbobry / gist:4041489
Created November 8, 2012 20:48
Python and +=
# -*- coding: utf-8 -*-
from collections import MutableSequence
def trace(f):
def inner(*args, **kwargs):
print(f.__name__)
return f(*args, **kwargs)
return inner
@superbobry
superbobry / ImplicitTest.scala
Last active September 25, 2017 20:36
Specializing a method on a value class
import scala.{specialized => spec}
package object foo {
class ImplicitTest(val n: Nothing) extends AnyVal {
def foo[@spec(Double) T](size: Int)(f: Int => T): Array[T] = {
f(42)
???
}
}
}
>>> Error(abc.ABC, Exception): pass
...
>>> EndOfTheWorld(Exception): pass
...
>>> Error.register(EndOfTheWorld)
<class '__main__.EndOfTheWorld'>
>>> e = EndOfTheWorld()
>>> isintance(e, Error)
True
>>> try:
@superbobry
superbobry / mangle.py
Created January 23, 2018 09:42
Python name mangling
>>> class A:
... def __init__(self, __foo):
... print(__foo)
...
>>> dis.dis(A.__init__)
3 0 LOAD_GLOBAL 0 (print)
2 LOAD_FAST 1 (_A__foo)
4 CALL_FUNCTION 1
6 POP_TOP
8 LOAD_CONST 0 (None)