Class: Dhall::List

Inherits:
Expression show all
Includes:
Enumerable
Defined in:
lib/dhall/ast.rb,
lib/dhall/binary.rb

Direct Known Subclasses

EmptyList

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Expression

#&, #*, #+, #as_dhall, #cache_key, #call, #deep_merge, #deep_merge_type, #dhall_eq, #digest, #fetch, #fusion, #merge, #normalize, #resolve, #shift, #slice, #substitute, #to_binary, #to_cbor, #to_proc, #to_s, #|

Constructor Details

#initialize(attrs) ⇒ List

Returns a new instance of List.



380
381
382
383
384
385
386
387
# File 'lib/dhall/ast.rb', line 380

def initialize(attrs)
  if attrs.key?(:element_type)
    et = attrs.delete(:element_type)
    attrs[:type] = self.class.as_dhall.call(et) if et
  end

  super
end

Class Method Details

.as_dhallObject



397
398
399
# File 'lib/dhall/ast.rb', line 397

def self.as_dhall
  Builtins[:List]
end

.decode(type, *els) ⇒ Object



87
88
89
90
91
92
93
94
# File 'lib/dhall/binary.rb', line 87

def self.decode(type, *els)
  type = type.nil? ? nil : Builtins[:List].call(Dhall.decode(type))
  if els.empty?
    EmptyList.new(type: type)
  else
    List.new(elements: els.map(&Dhall.method(:decode)), type: type)
  end
end

.of(*args, type: nil) ⇒ Object



389
390
391
392
393
394
395
# File 'lib/dhall/ast.rb', line 389

def self.of(*args, type: nil)
  if args.empty?
    EmptyList.new(element_type: type)
  else
    List.new(elements: args, element_type: type)
  end
end

Instance Method Details

#[](idx) ⇒ Object



435
436
437
# File 'lib/dhall/ast.rb', line 435

def [](idx)
  Optional.for(elements[idx.to_i], type: element_type)
end

#as_jsonObject



410
411
412
# File 'lib/dhall/ast.rb', line 410

def as_json
  [4, nil, *elements.map(&:as_json)]
end

#concat(other) ⇒ Object



455
456
457
458
459
460
461
# File 'lib/dhall/ast.rb', line 455

def concat(other)
  if other.is_a?(List) && !other.is_a?(EmptyList)
    with(elements: elements + other.elements)
  else
    super
  end
end

#each(&block) ⇒ Object



422
423
424
425
# File 'lib/dhall/ast.rb', line 422

def each(&block)
  elements.each(&block)
  self
end

#element_typeObject



401
402
403
404
405
406
407
408
# File 'lib/dhall/ast.rb', line 401

def element_type
  if type.nil?
  elsif type.is_a?(Application) && type.function == Builtins[:List]
    type.argument
  else
    raise "Cannot get element_type of: #{type.inspect}"
  end
end

#firstObject



439
440
441
# File 'lib/dhall/ast.rb', line 439

def first
  Optional.for(elements.first, type: element_type)
end

#join(sep = $,) ⇒ Object



451
452
453
# File 'lib/dhall/ast.rb', line 451

def join(sep=$,)
  elements.map(&:to_s).join(sep)
end

#lastObject



443
444
445
# File 'lib/dhall/ast.rb', line 443

def last
  Optional.for(elements.last, type: element_type)
end

#lengthObject



431
432
433
# File 'lib/dhall/ast.rb', line 431

def length
  elements.length
end

#map(type: nil, &block) ⇒ Object



414
415
416
417
418
419
420
# File 'lib/dhall/ast.rb', line 414

def map(type: nil, &block)
  type = type.nil? ? nil : Builtins[:List].call(type.as_dhall)
  with(
    elements: elements.each_with_index.map(&block),
    type:     type
  )
end

#reduce(*z) ⇒ Object



427
428
429
# File 'lib/dhall/ast.rb', line 427

def reduce(*z)
  elements.reverse.reduce(*z) { |acc, x| yield x, acc }
end

#reverseObject



447
448
449
# File 'lib/dhall/ast.rb', line 447

def reverse
  with(elements: elements.reverse)
end