lib/code/object/list.rb in template-ruby-0.3.1 vs lib/code/object/list.rb in template-ruby-0.4.0

- old
+ new

@@ -8,56 +8,64 @@ end def call(**args) operator = args.fetch(:operator, nil) arguments = args.fetch(:arguments, []) - context = args.fetch(:context) - io = args.fetch(:io) + globals = args.multi_fetch(*::Code::GLOBALS) if operator == "any?" - any?(arguments, context: context, io: io) + sig(arguments, ::Code::Object::Function) + any?(arguments.first.value, **globals) elsif operator == "none?" - none?(arguments, context: context, io: io) + sig(arguments, ::Code::Object::Function) + none?(arguments.first.value, **globals) elsif operator == "detect" - detect(arguments, context: context, io: io) + sig(arguments, ::Code::Object::Function) + detect(arguments.first.value, **globals) elsif operator == "reduce" - reduce(arguments, context: context, io: io) + sig(arguments, ::Code::Object::Function) + reduce(arguments.first.value, **globals) elsif operator == "each" - each(arguments, context: context, io: io) + sig(arguments, ::Code::Object::Function) + each(arguments.first.value, **globals) elsif operator == "select" - select(arguments, context: context, io: io) + sig(arguments, ::Code::Object::Function) + select(arguments.first.value, **globals) elsif operator == "map" - map(arguments, context: context, io: io) + sig(arguments, ::Code::Object::Function) + map(arguments.first.value, **globals) + elsif operator == "max_by" + sig(arguments, ::Code::Object::Function) + max_by(arguments.first.value, **globals) elsif operator == "max" - max(arguments) + sig(arguments) + max elsif operator == "flatten" - flatten(arguments) + sig(arguments) + flatten elsif operator == "reverse" - reverse(arguments) + sig(arguments) + reverse elsif operator == "first" - first(arguments) + sig(arguments) + first elsif operator == "last" - last(arguments) - elsif operator == "max_by" - max_by(arguments, context: context, io: io) + sig(arguments) + last elsif operator == "<<" - append(arguments) + sig(arguments, ::Code::Object) + append(arguments.first.value) else super end end - def <<(element) - raw << element - end - - def flatten(arguments) - sig(arguments) + def flatten ::Code::Object::List.new( raw.reduce([]) do |acc, element| if element.is_a?(::Code::Object::List) - acc + element.flatten(arguments).raw + acc + element.flatten.raw else acc + [element] end end, ) @@ -75,147 +83,112 @@ to_s end private - def any?(arguments, context:, io:) - sig(arguments, ::Code::Object::Function) - argument = arguments.first + def any?(argument, **globals) ::Code::Object::Boolean.new( raw.any? do |element| - argument - .value - .call( - arguments: [::Code::Object::Argument.new(element)], - context: context, - io: io, - ) - .truthy? + argument.call( + arguments: [::Code::Object::Argument.new(element)], + **globals, + ).truthy? end, ) end - def none?(arguments, context:, io:) - sig(arguments, ::Code::Object::Function) - argument = arguments.first + def none?(argument, **globals) ::Code::Object::Boolean.new( raw.none? do |element| - argument - .value - .call( - arguments: [::Code::Object::Argument.new(element)], - context: context, - io: io, - ) - .truthy? + argument.call( + arguments: [::Code::Object::Argument.new(element)], + **globals, + ).truthy? end, ) end - def max_by(arguments, context:, io:) - sig(arguments, ::Code::Object::Function) - argument = arguments.first.value + def max_by(argument, **globals) raw.max_by do |element| argument.call( arguments: [::Code::Object::Argument.new(element)], - context: context, - io: io, + **globals, ) end || ::Code::Object::Nothing.new end - def detect(arguments, context:, io:) - sig(arguments, ::Code::Object::Function) - argument = arguments.first.value + def detect(argument, **globals) raw.detect do |element| argument.call( arguments: [::Code::Object::Argument.new(element)], - context: context, - io: io, + **globals, ).truthy? end || ::Code::Object::Nothing.new end - def reduce(arguments, context:, io:) - sig(arguments, ::Code::Object::Function) - argument = arguments.first.value + def reduce(argument, **globals) raw.reduce do |acc, element| argument.call( arguments: [ ::Code::Object::Argument.new(acc), ::Code::Object::Argument.new(element), ], - context: context, - io: io, + **globals, ) end || ::Code::Object::Nothing.new end - def each(arguments, context:, io:) - sig(arguments, ::Code::Object::Function) - argument = arguments.first.value + def each(argument, **globals) raw.each do |element| argument.call( arguments: [::Code::Object::Argument.new(element)], - context: context, - io: io, + **globals, ) end self end - def select(arguments, context:, io:) - sig(arguments, ::Code::Object::Function) - argument = arguments.first.value + def select(argument, **globals) ::Code::Object::List.new( raw.select do |element| argument.call( arguments: [::Code::Object::Argument.new(element)], - context: context, - io: io, + **globals, ).truthy? end, ) end - def map(arguments, context:, io:) - sig(arguments, ::Code::Object::Function) - argument = arguments.first.value + def map(argument, context:, io:) ::Code::Object::List.new( raw.map do |element| argument.call( arguments: [::Code::Object::Argument.new(element)], - context: context, - io: io, + **globals, ) end, ) end - def append(arguments) - sig(arguments, ::Code::Object) - raw << arguments.first.value + def append(other) + raw << other self end - def first(arguments) - sig(arguments) - raw.first + def first + raw.first || ::Code::Object::Nothing.new end - def max(arguments) - sig(arguments) + def max raw.max || ::Code::Object::Nothing.new end - def reverse(arguments) - sig(arguments) + def reverse ::Code::Object::List.new(raw.reverse) end - def last(arguments) - sig(arguments) - raw.last + def last + raw.last || ::Code::Object::Nothing.new end end end end