Class: Peruby::Op::Map
Overview
map/grep block with $_ aliasing.
Instance Method Summary collapse
-
#initialize(body, list, grep) ⇒ Map
constructor
A new instance of Map.
- #run(env, context) ⇒ Object
Methods inherited from Base
#argument_cells, #local_slot, #lvalue, #run_subroutine, #to_callable, #warning_directive?
Constructor Details
#initialize(body, list, grep) ⇒ Map
Returns a new instance of Map.
136 137 138 139 140 |
# File 'lib/peruby/op/loop.rb', line 136 def initialize(body, list, grep) @body = body @list = list @grep = grep end |
Instance Method Details
#run(env, context) ⇒ Object
142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/peruby/op/loop.rb', line 142 def run(env, context) values = @list.run(env, :list).flat_map do |cell| result = env.with_scope do env.bind(:scalar, '_', cell) @body.run(env, @grep ? :scalar : :list) end if @grep Conv.truthy?(result) ? [cell] : [] else Array(result) end end context == :list ? values : values.length end |