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

#&, #*, #+, #annotate, #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.



421
422
423
424
425
426
427
428
# File 'lib/dhall/ast.rb', line 421

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



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

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



430
431
432
433
434
435
436
# File 'lib/dhall/ast.rb', line 430

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



476
477
478
# File 'lib/dhall/ast.rb', line 476

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

#as_jsonObject



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

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

#concat(other) ⇒ Object



496
497
498
499
500
501
502
# File 'lib/dhall/ast.rb', line 496

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

#each(&block) ⇒ Object



463
464
465
466
# File 'lib/dhall/ast.rb', line 463

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

#element_typeObject



442
443
444
445
446
447
448
449
# File 'lib/dhall/ast.rb', line 442

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



480
481
482
# File 'lib/dhall/ast.rb', line 480

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

#join(sep = $,) ⇒ Object



492
493
494
# File 'lib/dhall/ast.rb', line 492

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

#lastObject



484
485
486
# File 'lib/dhall/ast.rb', line 484

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

#lengthObject



472
473
474
# File 'lib/dhall/ast.rb', line 472

def length
	elements.length
end

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



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

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



468
469
470
# File 'lib/dhall/ast.rb', line 468

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

#reverseObject



488
489
490
# File 'lib/dhall/ast.rb', line 488

def reverse
	with(elements: elements.reverse)
end