Skip to content

Instantly share code, notes, and snippets.

@eungju
eungju / thrifthelper.py
Last active January 19, 2016 05:22
Thrift HA
class ThriftConnection(object):
def __init__(self, service, host, port):
socket = TSocket.TSocket(host, port)
self.transport = TTransport.TFramedTransport(socket)
protocol = TBinaryProtocolAccelerated(self.transport)
self.client = service.Client(protocol)
self.transport.open()
self.open_time = time.time()
self.access_time = self.open_time
self.str = "%s#%d(%s:%d/%s)" % (self.__class__.__name__, id(self), host, port, service.__name__.rsplit(".", 1)[-1])
@eungju
eungju / elpa-obsolete.hs
Last active August 29, 2015 13:58
ELPA utility
#!/usr/bin/env runghc
import Control.Monad
import Data.Function
import Data.Functor
import Data.List
import System.Directory
import System.FilePath
data ELPA = ELPA FilePath
@eungju
eungju / otp.hs
Last active December 21, 2022 11:09
Google Authenticator in Haskell
#!/usr/bin/env runghc
import qualified Codec.Binary.Base32 as Base32
import Codec.Utils (i2osp, fromTwosComp)
import qualified Control.Arrow as Arrow
import Data.Bits
import Data.Char
import Data.Functor
import Data.HMAC
import Data.List.Split
@eungju
eungju / Cleave.hs
Created April 11, 2014 11:32
Factor cleave words in Haskell
bi :: (a -> b) -> (a -> c) -> a -> (b, c)
bi f g (x) = (f x, g x)
bi2 :: (a -> b -> c) -> (a -> b -> d) -> (a, b) -> (c, d)
bi2 f g (x, y) = (f x y, g x y)
tri :: (a -> b) -> (a -> c) -> (a -> d) -> a -> (b, c, d)
tri f g h (x) = (f x, g x, h x)
tri2 :: (a -> b -> c) -> (a -> b -> d) -> (a -> b -> e) -> (a, b) -> (c, d, e)
@eungju
eungju / download-chapters.hs
Last active April 9, 2019 08:31
Download The Art of Science and Engineering
import Control.Applicative
import Control.Monad
import System.Directory
import System.FilePath
import System.Exit
import System.Process
import Text.Printf
type URL = String
@eungju
eungju / LookAndSay.exs
Created October 31, 2014 02:24
Look and Say
[1] |> Stream.iterate(fn(x) -> x |> Enum.chunk_by(&+/1) |> Enum.flat_map(fn([x|_]=c) -> [x, Enum.count(c)] end) end) |> Enum.take(10)
@eungju
eungju / min_sum.factor
Last active August 29, 2015 14:10
Minimum sum of two numbers
natural-sort [ zero? ] partition dup length 2 < [ 2drop -1 ] [ 2 cut swapd 3append zip-index [ second even? ] partition [ keys 0 [ swap 10 * + ] reduce ] bi@ + ] if
natural-sort [ zero? ] partition dup length 2 < [ 2drop -1 ] [ 2 cut surround zip-index [ second even? ] partition [ keys 0 [ swap 10 * + ] reduce ] bi@ + ] if
: >column-name ( n -- s ) "" [ over zero? ] [ [ 1 - 26 /mod 65 + ] dip swap prefix ] until nip ;
dup 1 tail zip [ [ second ] [ first ] bi - ] sort-with first
@eungju
eungju / ss.py
Created September 20, 2015 05:59
Cancelling accept by signal.
import socket
import signal
server = None
def sigusr1_handler(signum, frame):
server.close()
signal.signal(signal.SIGUSR1, sigusr1_handler)