Class: SoberSwag::Nodes::List

Inherits:
Base
  • Object
show all
Defined in:
lib/sober_swag/nodes/list.rb

Overview

TODO:

swap the names of this and Array so it's less confusing.

A List of the contained element types.

Unlike Array, this actually models arrays. The other one is a node that is an array in terms of what it contains. Kinda confusing, but oh well.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#<=>, #eql?, #hash

Constructor Details

#initialize(element) ⇒ List

Initialize with a node representing the type of elements in the list.

Parameters:



15
16
17
# File 'lib/sober_swag/nodes/list.rb', line 15

def initialize(element)
  @element = element
end

Instance Attribute Details

#elementSoberSwag::Nodes::Base (readonly)



21
22
23
# File 'lib/sober_swag/nodes/list.rb', line 21

def element
  @element
end

Instance Method Details

#cata(&block) ⇒ Object

Maps over the element type, then this List type.

See Also:



40
41
42
43
44
45
46
# File 'lib/sober_swag/nodes/list.rb', line 40

def cata(&block)
  block.call(
    self.class.new(
      element.cata(&block)
    )
  )
end

#deconstructArray(SoberSwag::Nodes::Base)



25
26
27
# File 'lib/sober_swag/nodes/list.rb', line 25

def deconstruct
  [element]
end

#deconstruct_keys(_) ⇒ Hash{Symbol => SoberSwag::Nodes::Base}

Returns the contained type wrapped in an element: key.

Returns:



32
33
34
# File 'lib/sober_swag/nodes/list.rb', line 32

def deconstruct_keys(_)
  { element: element }
end

#map(&block) ⇒ Object

Maps over the element type.

See Also:



52
53
54
55
56
# File 'lib/sober_swag/nodes/list.rb', line 52

def map(&block)
  self.class.new(
    element.map(&block)
  )
end