Last active
December 19, 2015 11:09
-
-
Save tsnow/5945568 to your computer and use it in GitHub Desktop.
Transformation Priority example http://blog.8thlight.com/uncle-bob/2013/05/27/TransformationPriorityAndSorting.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# http://blog.8thlight.com/uncle-bob/2013/05/27/TransformationPriorityAndSorting.html | |
# The Transformations | |
# | |
# So what are these transformations? Perhaps we can make a list of them: | |
# | |
# ({}–>nil) no code at all->code that employs nil | |
# (nil->constant) | |
# (constant->constant+) a simple constant to a more complex constant | |
# (constant->scalar) replacing a constant with a variable or an argument | |
# (statement->statements) adding more unconditional statements. | |
# (unconditional->if) splitting the execution path | |
# (scalar->array) | |
# (array->container) | |
# (statement->recursion) | |
# (if->while) | |
# (expression->function) replacing an expression with a function or algorithm | |
# (variable->assignment) replacing the value of a variable. | |
class Array | |
def my_sort | |
return self if self.size <= 1 | |
less, greater = partition{|i| i <= self.first} | |
return (less - [self.first] ).my_sort + [self.first] + greater.my_sort | |
end | |
end | |
def test_order(ary, order) | |
str = ary.inspect + '.my_sort ' | |
other_str = ary.inspect + '.sort ' | |
puts " sort:#{'% 10s' % eval(other_str).to_s } my_sort:#{'% 10s' % eval(str).inspect} #{ '% 20s' % str} #{eval(other_str + ' == ' + str)}" | |
end | |
class Array | |
def my_sort | |
[] | |
end | |
end | |
test_order [], [] | |
class Array | |
def my_sort | |
self | |
end | |
end | |
test_order [], [] | |
test_order [1], [1] | |
test_order [1,2], [1,2] | |
test_order [1,2,3], [1,2,3] | |
class Array | |
def my_sort | |
return [last,first] if self.size > 1 && last < first | |
self | |
end | |
end | |
test_order [], [] | |
test_order [1], [1] | |
test_order [1,2], [1,2] | |
test_order [1,2,3], [1,2,3] | |
test_order [2,1], [1,2] | |
class Array | |
def my_sort | |
#return self if self.size <= 1 # If I'd not undone the tests I'd successfully matched, I wouldn't have to go back and add this later. | |
less, greater = partition{|i| i < self.first} | |
return less + greater | |
end | |
end | |
test_order [], [] | |
test_order [1], [1] | |
test_order [1,2], [1,2] | |
test_order [1,2,3], [1,2,3] | |
test_order [2,1], [1,2] | |
test_order [2,1,3], [1,2,3] | |
class Array | |
def my_sort | |
less, greater = partition{|i| i < self.first} | |
return less.my_sort + greater.my_sort # Recurses infinitely | |
end | |
end | |
class Array | |
def my_sort | |
return self if self.size <= 1 # Had to add this to halt the recursion | |
less, greater = partition{|i| i <= self.first} | |
return (less - [self.first] ).my_sort + [self.first] + greater.my_sort #and the .first ops here | |
end | |
end | |
test_order [], [] | |
test_order [1], [1] | |
test_order [1,2], [1,2] | |
test_order [1,2,3], [1,2,3] | |
test_order [2,1], [1,2] | |
test_order [2,1,3], [1,2,3] | |
test_order [1,3,2], [1,2,3] | |
test_order [3,2,1], [1,2,3] | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment