From f5911f4cb60587938ca30a98e1c3bce1465d5559 Mon Sep 17 00:00:00 2001 From: Alexander Munch-Hansen Date: Thu, 17 Oct 2019 11:48:54 +0200 Subject: [PATCH] More beautify --- find_algos.rb | 7 ++----- functional_functions.rb | 20 +++++++++++-------- lol.rb | 44 ++++++++++++++++++----------------------- 3 files changed, 33 insertions(+), 38 deletions(-) diff --git a/find_algos.rb b/find_algos.rb index a65c553..45900f8 100644 --- a/find_algos.rb +++ b/find_algos.rb @@ -13,11 +13,8 @@ end def find_best_with_extra_parameter(container_obj, based_on, compare, type, extra_based_on, extra_compare, extra_param, based_on_type) - if extra_compare == " > " - result_init = "Integer.MIN_VALUE" - else - result_init = "Integer.MAX_VALUE" - end + + result_init = extra_compare == " > " ? "Integer.MIN_VALUE" : "Integer.MAX_VALUE" if based_on_type == "int" helper_variable = "int result#{based_on} = #{result_init};" diff --git a/functional_functions.rb b/functional_functions.rb index 78bedf0..aec2faa 100644 --- a/functional_functions.rb +++ b/functional_functions.rb @@ -1,6 +1,7 @@ def func_find_all(container_obj, attribute, filter_by) filters = filter_by.map do |x| - "s.get#{attribute}() #{x.comparator} #{x.value}" + comparator = x.comparator == ".equals" ? ".equals(#{x.value})" : "#{x.comparator} #{x.value}" + "s.get#{attribute}()#{comparator}" end # TODO: Consider also having an "or" here "return #{container_obj}.stream().filter(s -> #{filters.join "&&"}).collect(Collectors.toList());" @@ -8,18 +9,18 @@ def func_find_all(container_obj, attribute, filter_by) end def find_whatever_of_something(container_obj, attribute, filter_by, aggregate_func) - # TODO: Fix comparator to work for strings, such that I use .equals instead of == filters = filter_by.map do |x| - "s.get#{attribute}() #{x.comparator} #{x.value}" + comparator = x.comparator == ".equals" ? ".equals(#{x.value})" : "#{x.comparator} #{x.value}" + "s.get#{attribute}()#{comparator}" end # 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) - # TODO: Fix comparator to work for strings, such that I use .equals instead of == filters = filter_by.map do |x| - "s.get#{attribute}() #{x.comparator} #{x.value}" + comparator = x.comparator == ".equals" ? ".equals(#{x.value})" : "#{x.comparator} #{x.value}" + "s.get#{attribute}()#{comparator}" end # TODO: Consider also having an "or" here "return #{container_obj}.stream().filter(s -> #{filters.join "&&"}).mapToInt(s -> s.get#{variable}()).#{aggregate_func}();" @@ -27,7 +28,8 @@ end def find_one(container_obj, attribute, filter_by) filters = filter_by.map do |x| - "s.get#{attribute}() #{x.comparator} #{x.value}" + comparator = x.comparator == ".equals" ? ".equals(#{x.value})" : "#{x.comparator} #{x.value}" + "s.get#{attribute}()#{comparator}" end # TODO: Consider also having an "or" here "return #{container_obj}.stream().filter(s -> #{filters.join "&&"}).findAny().orElse(null);" @@ -35,7 +37,8 @@ end def find_best_value(container_obj, attribute, filter_by, min_or_max, based_on) filters = filter_by.map do |x| - "s.get#{attribute}() #{x.comparator} #{x.value}" + comparator = x.comparator == ".equals" ? ".equals(#{x.value})" : "#{x.comparator} #{x.value}" + "s.get#{attribute}()#{comparator}" end # TODO: Consider also having an "or" here "return #{container_obj}.stream().filter(s -> #{filters.join "&&"}).mapToInt(s -> s.get#{based_on}()).#{min_or_max};" @@ -43,7 +46,8 @@ end def find_best_object(container_obj, attribute, filter_by, min_or_max, based_on) filters = filter_by.map do |x| - "s.get#{attribute}() #{x.comparator} #{x.value}" + comparator = x.comparator == ".equals" ? ".equals(#{x.value})" : "#{x.comparator} #{x.value}" + "s.get#{attribute}()#{comparator}" end # 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}()));" diff --git a/lol.rb b/lol.rb index 0739142..6becdfa 100644 --- a/lol.rb +++ b/lol.rb @@ -14,6 +14,10 @@ to_string_template = "0 1 x 2 meter" sorting_one = Type.new("String", "Name") sorting_two = Type.new("int", "Length") sorting_three = Type.new("int", "Width") + +sorting_two.comparator = "<" +sorting_three.comparator = "<" + sorting_filters = [sorting_one, sorting_two, sorting_three] def construct_class(name_of_class, parameters, to_string_template, sorting_filters) @@ -74,9 +78,6 @@ def construct_driver(class_name, parameters, methods, container_class, container end.join "," end - - - methods_to_print = "" maggie = methods.map do |x| if x[:print] "System.out.println(lol.#{x[:name]}(#{args x[:parameters]}));" @@ -85,8 +86,8 @@ def construct_driver(class_name, parameters, methods, container_class, container end end - driver_string = " -public class TestDriver { + " + public class TestDriver { public TestDriver(){} @@ -150,7 +151,7 @@ def construct_container(name, parameters, given_fields, methods, arraylist_name) "this.#{x[:name]} = #{x[:name]};" end - container_string = " + " import java.util.*; import java.util.stream.Collectors; @@ -164,27 +165,17 @@ def construct_container(name, parameters, given_fields, methods, arraylist_name) #{maggie.join "\n"} - public void print#{name}() { - System.out.println(\"#{name}\"); - Collections.sort(#{arraylist_name}); + public void print#{name}() { + System.out.println(\"#{name}\"); + Collections.sort(#{arraylist_name}); - for (var i : #{arraylist_name}) { - System.out.println(i); + for (var i : #{arraylist_name}) { + System.out.println(i); + } } - } - } - - " - - container_string - + }" end -file = File.open("#{name}.java", "w") -params = [{:type => "String", :name => "Name"}, {:type => "int", :name => "Length"}, {:type => "int", :name => "Width"}] -file.puts(construct_class name, params, to_string_template, sorting_filters) -file.close - container_methods = [ { @@ -234,12 +225,15 @@ container_methods = [ :type => "String", :name => "Name" }], - :method_type => find_whatever_of_variable(container_obj, "Name", [Filter.new("Name", "==")], "sum", "Length") + :method_type => find_whatever_of_variable(container_obj, "Name", [Filter.new("Name", ".equals")], "sum", "Length") } ] - +file = File.open("#{name}.java", "w") +params = [{:type => "String", :name => "Name"}, {:type => "int", :name => "Length"}, {:type => "int", :name => "Width"}] +file.puts(construct_class name, params, to_string_template, sorting_filters) +file.close another_another_file = File.open("#{container_class}.java", "w") another_another_file.puts(construct_container(container_class, [{:type => "String", :name => "Name"}], [ {:type => "ArrayList<#{name}>", :name => container_obj}], container_methods, container_obj))