reverse-complement Matz's Ruby #4 program
source code
# The Computer Language Benchmarks Game
# http://benchmarksgame.alioth.debian.org
#
# Contributed by Aaron Tavistock
def process_segment(segment)
(header, _, sequence) = segment.partition("\n")
sequence.delete!("\n\r >")
sequence.reverse!
sequence.tr!(
'wsatugcyrkmbdhvnATUGCYRKMBDHVN',
'WSTAACGRYMKVHDBNTAACGRYMKVHDBN'
)
sequence_length = sequence.length
results_size = sequence_length + sequence_length / 60 + header.length + 4
results = String.new(">#{header}\n")
idx = 0
while idx < sequence_length
results << sequence[idx,60]
results << "\n"
idx += 60
end
results.freeze
results
end
def forking_worker(segment)
reader, writer = IO.pipe
pid = Process.fork do
begin
reader.close
results = original_process_segment(segment)
writer.write(results)
ensure
writer.close
end
end
writer.close
begin
results = reader.read
ensure
reader.close
end
Process.waitpid(pid)
results
end
if RUBY_PLATFORM != 'java'
class << self
alias_method :original_process_segment, :process_segment
alias_method :process_segment, :forking_worker
end
end
threads = []
STDIN.each_line('>').lazy.each do |segment|
next if segment.length < 2
threads << Thread.new do
Thread.current[:output] = process_segment(segment)
end
end
threads.each(&:join)
threads.each do |thread|
puts thread[:output]
end
notes, command-line, and program output
NOTES:
64-bit Ubuntu quad core
ruby 1.8.7 (2008-08-11 patchlevel 72) [x86_64-linux]
So old that I haven't been able to get rubygems to work
no backport, no gmp
Sat, 04 Jan 2020 21:37:45 GMT
COMMAND LINE:
/usr/bin/ruby revcomp.mri-4.mri 0 < revcomp-input250000.txt
PROGRAM FAILED
PROGRAM OUTPUT:
revcomp.mri-4.mri:63: undefined method `lazy' for #<Enumerable::Enumerator:0x7f4dddc57308> (NoMethodError)