Skip to content

Instantly share code, notes, and snippets.

@Supernats
Created January 25, 2019 02:21
Show Gist options
  • Save Supernats/0e9c0af364204ad7f3d04963ed337cf7 to your computer and use it in GitHub Desktop.
Save Supernats/0e9c0af364204ad7f3d04963ed337cf7 to your computer and use it in GitHub Desktop.
class FindCallable
class << self
def call
new.call
end
end
INPUT = 'index_delicous_fruit_recipes_on_apple_id_and_banana_id_and_citron_id_and_durian_id'
PASSTHROUGH = lambda do |string|
string
end
DROP_VERB = lambda do |string|
head, tail = string.split('_on_')
_verb, *objects = head.split('_')
[objects.join('_'), tail].join('_on_')
end
SHORTEN_PREPOSITION = lambda do |string|
head, tail = string.split('_on_')
prepositional_objects = tail.split('_and_').map do |object|
object.
split('_').
map { |word| word[0] }.
join('_')
end.join('_and_')
[head, prepositional_objects].join('_on_')
end
ALGORITHMS = [
PASSTHROUGH,
DROP_VERB,
SHORTEN_PREPOSITION,
]
def call
[60, 80, 100].map do |max_length|
# In practice one would just want the actually callable returned, but for
# illustrative purposes it may helpful to more directly "see" which
# callable will be used.
process_index = ALGORITHMS.find_index do |a|
a.call(INPUT).length <= max_length
end
{
max_length: max_length,
process_index: process_index,
output: ALGORITHMS[process_index].call(INPUT),
}
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment