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_cbor, #to_proc, #to_s, #|

Class Method Details

.as_dhallObject



358
359
360
# File 'lib/dhall/ast.rb', line 358

def self.as_dhall
	Builtins[:List]
end

.decode(type, *els) ⇒ Object



82
83
84
85
86
87
88
89
# File 'lib/dhall/binary.rb', line 82

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

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



350
351
352
353
354
355
356
# File 'lib/dhall/ast.rb', line 350

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



393
394
395
# File 'lib/dhall/ast.rb', line 393

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

#as_jsonObject



369
370
371
# File 'lib/dhall/ast.rb', line 369

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

#concat(other) ⇒ Object



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

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

#each(&block) ⇒ Object



380
381
382
383
# File 'lib/dhall/ast.rb', line 380

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

#firstObject



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

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

#join(sep = $,) ⇒ Object



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

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

#lastObject



401
402
403
# File 'lib/dhall/ast.rb', line 401

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

#lengthObject



389
390
391
# File 'lib/dhall/ast.rb', line 389

def length
	elements.length
end

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



373
374
375
376
377
378
# File 'lib/dhall/ast.rb', line 373

def map(type: nil, &block)
	with(
		elements:     elements.each_with_index.map(&block),
		element_type: type&.as_dhall
	)
end

#reduce(*z) ⇒ Object



385
386
387
# File 'lib/dhall/ast.rb', line 385

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

#reverseObject



405
406
407
# File 'lib/dhall/ast.rb', line 405

def reverse
	with(elements: elements.reverse)
end

#typeObject



362
363
364
365
366
367
# File 'lib/dhall/ast.rb', line 362

def type
	Dhall::Application.new(
		function: self.class.as_dhall,
		argument: element_type
	)
end