Skip to content

Instantly share code, notes, and snippets.

@aycabta
Forked from supermomonga/moge.rb
Last active December 21, 2015 20:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aycabta/6362458 to your computer and use it in GitHub Desktop.
Save aycabta/6362458 to your computer and use it in GitHub Desktop.
require 'rspec'
def cloop(*params, &block)
r = ->(*xs){ block.(r, *xs) }
r.(*params)
end
def map(xs, &block)
cloop([], xs){ |r, nxs, x|
if x == []
nxs
else
r.(nxs << block.(x.first), x[1..-1])
end
}
end
describe 'cloop' do
before(:all) do
@xs_orig = [*1..5]
@xs = @xs_orig.dup
end
context 'run map' do
subject { map(@xs) { |n| n + 10 } }
it { should == @xs_orig.map { |n| n + 10 } }
end
context 'check original array' do
subject { @xs }
it { should == @xs_orig }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment