#!/bin/bash
declare -A USE_MAP
declare -a ENABLES
# export_name dependency dependecy dependency...
USE_MAP['HBASE']='USE_HBASE HCATALOG HUE'
USE_MAP['PIG']='USE_PIG'
USE_MAP['HUE']='USE_HUE'
USE_MAP['HCATALOG']='USE_HCATALOG'
USE_MAP['PRESTO']='USE_PRESTO HCATALOG'
ENABLES=()
function use() {
local dep=(${USE_MAP["$1"]})
ENABLES+=(${dep[0]})
dep=("${dep[@]:1}")
if [ ! -z "$dep" ]; then
for item in ${dep[@]}; do
use $item
done
fi
}
function export_enabled() {
sorted_use=($(echo "${ENABLES[@]}" | tr ' ' '\n' | sort -u | tr '\n' ' '))
for item in ${sorted_use[@]}; do
echo "export $item";
done
}
use HBASE
export_enabled
------
$ bash resolve.sh
export USE_HBASE
export USE_HCATALOG
export USE_HUE
Tag: programming
Print simple x*x table in ruby
I’ve needed to print simple table as
1 2 3 5 7 2 4 6 10 14 3 6 9 15 21 5 10 15 25 35 7 14 21 35 49
as you can see, x = y, cell in table = x*y (x*x to be exact)
primes = [2, 3, 5, 7]
primes = [1] + primes # we need that to print x/y axis
puts primes.map{|i| primes.map{|a| a*i}}.map{|i| ['%4i']*primes.length*' '%i}
Calculate prime numbers
Tried simple Sieve of Eratosthenes in Ruby …
class MyPrime
attr_reader :primes # there we can read then
# compute prime numbers up to limit, default is 20
def initialize(limit = 20)
@primes = []
limit = limit.to_i
return if limit.to_i < 2
arr = Hash[*((2..limit).to_a*2).sort] # we can use .zip too
(2..Math.sqrt(limit)).each do |i|
next unless arr[i] # already removed
(i*i..limit).step(i).each{|k| arr.delete(k)}
end
@primes = arr.keys.sort # return just keys
end
end
we can add some tests
require 'test/unit'
require 'my_prime'
class TestMyPrime < Test::Unit::TestCase
def test_20_primes
primes = MyPrime.new(20)
assert_equal [2, 3, 5, 7, 11, 13, 17, 19], primes.primes
end
def test_wo_param
primes = MyPrime.new
assert_equal [2, 3, 5, 7, 11, 13, 17, 19], primes.primes
end
def test_less_than_2
primes = MyPrime.new(1)
assert_equal [], primes.primes
end
def test_limit_eq_2
primes = MyPrime.new(2)
assert_equal [2], primes.primes
end
def test_limit_is_string
primes = MyPrime.new('20')
assert_equal [2, 3, 5, 7, 11, 13, 17, 19], primes.primes
end
def test_limit_is_float
primes = MyPrime.new(20.50)
assert_equal [2, 3, 5, 7, 11, 13, 17, 19], primes.primes
end
end
Sportka v FMS/MSW Logo
NEON: get/post/put requests
iPhone SDK4 Beta 4
Apple has released new version of iPhone OS4 SDK (SDK4) Beta 4…
Build: 10M2252
Download & Availability: iPhone Developer Program/Members only
How to get line number and file of your RoR code
# parse caller to fetch filename, method &amp; line of call
def parse_caller(at)
if /^(.+?):(\d+)(?::in `(.*)')?/ =~ at
file = Regexp.last_match[1]
line = Regexp.last_match[2].to_i
method = Regexp.last_match[3]
[file, line, method]
end
end
file, line, method = parse_caller(caller(1).first)
custom date/time formatting with i18n
in helper
def show_date(date) return '' if date.nil? || date==0 l(Time.at(date), :format => :ddate) end
in config/locale/en.yml (for example)
date: formats: ddate: %d.%m.%Y
add czech pluralize to i18n
module I18n::Backend
class Simple
protected
def pluralize(locale, entry, count)
return entry unless entry.is_a?(Hash) and count
key = :zero if count == 0 && entry.has_key?(:zero)
key ||= count == 1 ? :one : (2..4).include?(count) ? :few : :other
raise InvalidPluralizationData.new(entry, count) unless entry.has_key?(key)
entry[key]
end
end # class
end # module
normalize UTF8 (translit/deaccent)
place this in your application.rb (eg)
class String
def translit
Unicode.normalize_KD(self).unpack('U*').select{ |cp| cp < 127 }.pack('U*').gsub(/ /,'-').downcase
end
end