Class: Optics::IxLens

Inherits:
Lens
  • Object
show all
Defined in:
lib/optics.rb

Instance Method Summary collapse

Methods inherited from Lens

#combine, #get, #set

Constructor Details

#initialize(index) ⇒ IxLens

Returns a new instance of IxLens.



38
39
40
41
42
43
44
45
46
47
# File 'lib/optics.rb', line 38

def initialize(index)
  super(
    -> (subject) { subject[index] },
    -> (subject, new_value) {
      subject.each_with_index.map { |old_value, value_index|
        value_index == index ? new_value : old_value
      }
    }
  )
end