From 7b55cf4eb9fa3cd7e562bc037698050e7c9cd20d Mon Sep 17 00:00:00 2001 From: Magdalena Kalin Date: Wed, 23 Oct 2019 14:20:34 +0200 Subject: [PATCH] Add more maggies --- Functional_Maggie.rb | 32 ++++++++++++++++++++++++++++++ functional_functions.rb | 44 ----------------------------------------- lol.rb | 2 +- 3 files changed, 33 insertions(+), 45 deletions(-) create mode 100644 Functional_Maggie.rb delete mode 100644 functional_functions.rb diff --git a/Functional_Maggie.rb b/Functional_Maggie.rb new file mode 100644 index 0000000..a533cd6 --- /dev/null +++ b/Functional_Maggie.rb @@ -0,0 +1,32 @@ +def annoyed_maggie(container_obj, attribute, filter_by) + filters = filter_by.map do |x| + comparator = x.comparator == ".equals" ? ".equals(#{x.value})" : "#{x.comparator} #{x.value}" + "s.get#{attribute}()#{comparator}" + end + "return #{container_obj}.stream().filter(s -> #{filters.join "&&"})" +end + +def func_find_all(container_obj, attribute, filter_by) + "#{annoyed_maggie(container_obj, attribute, filter_by)}.collect(Collectors.toList());" + +end + +def find_whatever_of_something(container_obj, attribute, filter_by, aggregate_func) + "#{annoyed_maggie(container_obj, attribute, filter_by)}.#{aggregate_func}();" +end + +def find_whatever_of_variable(container_obj, attribute, filter_by, aggregate_func, variable) + "#{annoyed_maggie(container_obj, attribute, filter_by)}.mapToInt(s -> s.get#{variable}()).#{aggregate_func}();" +end + +def find_one(container_obj, attribute, filter_by) + "#{annoyed_maggie(container_obj, attribute, filter_by)}.findAny().orElse(null);" +end + +def find_best_value(container_obj, attribute, filter_by, min_or_max, based_on) + "#{annoyed_maggie(container_obj, attribute, filter_by)}.mapToInt(s -> s.get#{based_on}()).#{min_or_max};" +end + +def find_best_object(container_obj, attribute, filter_by, min_or_max, based_on) + "#{annoyed_maggie(container_obj, attribute, filter_by)}.#{min_or_max}(Comparator.comparing(#{based_on.type}.get#{based_on.variable}()));" +end diff --git a/functional_functions.rb b/functional_functions.rb deleted file mode 100644 index 014cf02..0000000 --- a/functional_functions.rb +++ /dev/null @@ -1,44 +0,0 @@ -def annoyed_maggie(attribute, filter_by) - filters = filter_by.map do |x| - comparator = x.comparator == ".equals" ? ".equals(#{x.value})" : "#{x.comparator} #{x.value}" - "s.get#{attribute}()#{comparator}" - end -end - -def func_find_all(container_obj, attribute, filter_by) - annoyed_maggie(attribute, filter_by) - # TODO: Consider also having an "or" here - "return #{container_obj}.stream().filter(s -> #{filters.join "&&"}).collect(Collectors.toList());" - -end - -def find_whatever_of_something(container_obj, attribute, filter_by, aggregate_func) - annoyed_maggie(attribute, filter_by) - # TODO: Consider also having an "or" here - "return #{container_obj}.stream().filter(s -> #{filters.join "&&"}).#{aggregate_func}();" -end - -def find_whatever_of_variable(container_obj, attribute, filter_by, aggregate_func, variable) - annoyed_maggie(attribute, filter_by) - # TODO: Consider also having an "or" here - "return #{container_obj}.stream().filter(s -> #{filters.join "&&"}).mapToInt(s -> s.get#{variable}()).#{aggregate_func}();" -end - -def find_one(container_obj, attribute, filter_by) - annoyed_maggie(attribute, filter_by) - # TODO: Consider also having an "or" here - "return #{container_obj}.stream().filter(s -> #{filters.join "&&"}).findAny().orElse(null);" -end - -def find_best_value(container_obj, attribute, filter_by, min_or_max, based_on) - annoyed_maggie(attribute, filter_by) - # TODO: Consider also having an "or" here - "return #{container_obj}.stream().filter(s -> #{filters.join "&&"}).mapToInt(s -> s.get#{based_on}()).#{min_or_max};" -end - -def find_best_object(container_obj, attribute, filter_by, min_or_max, based_on) - annoyed_maggie(attribute, filter_by) - # TODO: Consider also having an "or" here - "return #{container_obj}.stream().filter(s -> #{filters.join "&&"}).#{min_or_max}(Comparator.comparing(#{based_on.type}.get#{based_on.variable}()));" -end - diff --git a/lol.rb b/lol.rb index a1cc02c..5306161 100644 --- a/lol.rb +++ b/lol.rb @@ -1,7 +1,7 @@ require_relative 'find_algos' require_relative 'sorting_algos' require_relative 'Type' -require_relative 'functional_functions' +require_relative 'Functional_Maggie' require_relative 'Filter' require_relative 'pdf-parser'