Skip to content

Instantly share code, notes, and snippets.

@halfelf
halfelf / consumer.clj
Created October 2, 2013 06:56
Simple Kafka Consumer in Clojure
(ns oceanus.anduin.clj.consumer
(:gen-class)
(:import [kafka.consumer ConsumerConfig Consumer KafkaStream]
[kafka.javaapi.consumer ConsumerConnector]
[java.util Properties]))
(defn make-props
"convert a clojure map into a Properties object."
[m]
(let [props (Properties.)]
@halfelf
halfelf / read_lines.erl
Created September 10, 2012 07:31
Reading Lines from a File in erlang
for_each_line_in_file(Name, Proc, Mode, Accum0) ->
{ok, Device} = file:open(Name, Mode),
for_each_line(Device, Proc, Accum0).
for_each_line(Device, Proc, Accum) ->
case io:get_line(Device, "") of
eof -> file:close(Device), Accum;
Line -> NewAccum = Proc(Line, Accum),
for_each_line(Device, Proc, NewAccum)
end.
@halfelf
halfelf / concat_binary.erl
Created September 6, 2012 02:49
Concatenate binaries in erlang
% concat, binary
B1= <<1,2>>.
B2= <<3,4>>.
B3= <<B1/binary, B2/binary>>.
% <<1,2,3,4>>
@halfelf
halfelf / is_element_of_tuple.erl
Created September 4, 2012 07:18
Find an element is in a tuple or not in Erlang.
-module(tuple_util).
-export([is_element_of_tuple/2]).
is_element_of_tuple(Tuple, Element) ->
lists:member(Element, tuple_to_list(Tuple)).
% or like this:
is_element_of_tuple(E, Tuple) ->
is_element_of_tuple(E, Tuple, 1, tuple_size(Tuple)).
@halfelf
halfelf / method_hook.rb
Created August 29, 2012 10:12
Example to show how to define a hook before a method called
class Base
def self.method_added(name)
if /hook/.match(name.to_s) or method_defined?("#{name}_without_hook")
return
end
hook = "def #{name}_hook\n p 'Method #{name} has been called'\n #{name}_without_hook\nend"
self.class_eval(hook)
a1 = "alias #{name}_without_hook #{name}"
self.class_eval(a1)