Class: Code::Object::Range
Constant Summary collapse
- CLASS_DOCUMENTATION =
{ name: "Range", description: "represents inclusive or exclusive sequences between comparable bounds.", examples: %w[1..3 1...3 (1..3).to_list] }.freeze
- INSTANCE_FUNCTIONS =
{ "all?" => { name: "all?", description: "returns whether every item in the range matches a function.", examples: [ "(1..3).all?((x) => { x > 0 })", "(1..3).all?((x) => { x < 4 })", "(1..3).all?((x) => { x < 3 })" ] }, "any?" => { name: "any?", description: "returns whether any item in the range matches a function.", examples: [ "(1..3).any?((x) => { x == 2 })", "(1..3).any?((x) => { x == 1 })", "(1..3).any?((x) => { x > 3 })" ] }, "none?" => { name: "none?", description: "returns whether no items in the range match a function.", examples: [ "(1..3).none?((x) => { x > 3 })", "(1..3).none?((x) => { x == 2 })", "(1..3).none?((x) => { x < 0 })" ] }, "each" => { name: "each", description: "calls a function for each item in the range and returns the range.", examples: [ "(1..3).each((x) => { x })", "(:a..:c).each((x) => { x })", "(1...3).each((x) => { x })" ] }, "reverse_each" => { name: "reverse_each", description: "calls a function for each item in the range in reverse order.", examples: [ "(1..3).reverse_each((x) => { x })", "(:a..:c).reverse_each((x) => { x })", "(1...3).reverse_each((x) => { x })" ] }, "include?" => { name: "include?", description: "returns whether the range includes a value.", examples: %w[ (1..3).include?(2) (1..3).include?(4) (:a..:c).include?(:b) ] }, "member?" => { name: "member?", description: "returns whether the range includes a value.", examples: %w[(1..3).member?(2) (1..3).member?(4) (:a..:c).member?(:b)] }, "cover?" => { name: "cover?", description: "returns whether a value is between the range bounds.", examples: %w[(1..3).cover?(2) (1..3).cover?(4) (:a..:c).cover?(:b)] }, "overlap?" => { name: "overlap?", description: "returns whether the range overlaps another range.", examples: %w[ (1..3).overlap?(2..4) (1..3).overlap?(4..6) (:a..:c).overlap?(:b..:d) ] }, "empty?" => { name: "empty?", description: "returns whether the range is empty.", examples: %w[(1..3).empty? (1...1).empty? (:a..:c).empty?] }, "begin" => { name: "begin", description: "returns the starting bound of the range.", examples: %w[(1..3).begin (:a..:c).begin (1...3).begin] }, "end" => { name: "end", description: "returns the ending bound of the range.", examples: %w[(1..3).end (:a..:c).end (1...3).end] }, "exclude_end?" => { name: "exclude_end?", description: "returns whether the range excludes its ending bound.", examples: %w[ (1..3).exclude_end? (1...3).exclude_end? (:a...:c).exclude_end? ] }, "first" => { name: "first", description: "returns the first item in the range.", examples: %w[(1..3).first (2..4).first (:a..:c).first] }, "last" => { name: "last", description: "returns the last item in the range.", examples: %w[(1..3).last (2..4).last (:a..:c).last] }, "minimum" => { name: "minimum", description: "returns the minimum item in the range.", examples: %w[(1..3).minimum (:a..:c).minimum (3..1).minimum] }, "maximum" => { name: "maximum", description: "returns the maximum item in the range.", examples: %w[(1..3).maximum (:a..:c).maximum (3..1).maximum] }, "minimum_maximum" => { name: "minimum_maximum", description: "returns the minimum and maximum items as a list.", examples: %w[ (1..3).minimum_maximum (:a..:c).minimum_maximum (3..1).minimum_maximum ] }, "map" => { name: "map", description: "returns a list with each item transformed by a function.", examples: [ "(1..3).map((x) => { x + 1 })", "(1..3).map((x) => { x.to_string })", "(:a..:c).map((x) => { x.to_string })" ] }, "select" => { name: "select", description: "returns a list of items matched by a function.", examples: [ "(1..3).select((x) => { x > 1 })", "(1..3).select((x) => { x < 3 })", "(:a..:c).select((x) => { x > :a })" ] }, "reject" => { name: "reject", description: "returns a list of items not matched by a function.", examples: [ "(1..3).reject((x) => { x > 1 })", "(1..3).reject((x) => { x < 3 })", "(:a..:c).reject((x) => { x > :a })" ] }, "reduce" => { name: "reduce", description: "combines items in the range with a function.", examples: [ "(1..3).reduce((sum, x) => { sum + x })", "(1..3).reduce((product, x) => { product * x })", "(2..4).reduce((sum, x) => { sum + x })" ] }, "step" => { name: "step", description: "returns a list of items separated by a step size.", examples: %w[(1..5).step(2) (1..5).step(1) (1...5).step(2)] }, "binary_search" => { name: "binary_search", description: "returns the first item matched by binary search.", examples: [ "(1..10).binary_search((x) => { x >= 5 })", "(1..10).binary_search((x) => { x > 10 })", "(1..3).binary_search((x) => { x >= 2 })" ] }, "sample" => { name: "sample", description: "returns a random item from the range.", examples: %w[(1..3).sample (2..4).sample (:a..:c).sample] }, "size" => { name: "size", description: "returns the number of items in the range.", examples: %w[(1..3).size (1...3).size (:a..:c).size] }, "count" => { name: "count", description: "returns the number of items, optionally matched by a function.", examples: [ "(1..3).count", "(1...3).count", "(1..5).count((x) => { x.even? })" ] }, "to_list" => { name: "to_list", description: "returns the range items as a list.", examples: %w[(1..3).to_list (1...3).to_list (:a..:c).to_list] }, "entries" => { name: "entries", description: "returns the range items as a list.", examples: %w[(1..3).entries (1...3).entries (:a..:c).entries] }, "to_dictionary" => { name: "to_dictionary", description: "returns a dictionary built from indexed range items.", examples: %w[ (1..3).to_dictionary (1...3).to_dictionary (:a..:c).to_dictionary ] } }.freeze
Instance Attribute Summary collapse
-
#code_exclude_end ⇒ Object
readonly
Returns the value of attribute code_exclude_end.
-
#code_left ⇒ Object
readonly
Returns the value of attribute code_left.
-
#code_options ⇒ Object
readonly
Returns the value of attribute code_options.
-
#code_right ⇒ Object
readonly
Returns the value of attribute code_right.
Class Method Summary collapse
Instance Method Summary collapse
- #call(**args) ⇒ Object
- #code_all?(argument, **globals) ⇒ Boolean
- #code_any?(argument, **globals) ⇒ Boolean
- #code_begin ⇒ Object
- #code_binary_search(argument, **globals) ⇒ Object
- #code_count(argument = nil, **globals) ⇒ Object
- #code_cover?(value) ⇒ Boolean
- #code_each(argument, **globals) ⇒ Object
- #code_empty? ⇒ Boolean
- #code_end ⇒ Object
- #code_entries ⇒ Object
- #code_exclude_end? ⇒ Boolean
- #code_first ⇒ Object
- #code_include?(value) ⇒ Boolean
- #code_last ⇒ Object
- #code_map(argument, **globals) ⇒ Object
- #code_maximum ⇒ Object
- #code_member?(value) ⇒ Boolean
- #code_minimum ⇒ Object
- #code_minimum_maximum ⇒ Object
- #code_none?(argument, **globals) ⇒ Boolean
- #code_overlap?(range) ⇒ Boolean
- #code_reduce(argument, **globals) ⇒ Object
- #code_reject(argument, **globals) ⇒ Object
- #code_reverse_each(argument, **globals) ⇒ Object
- #code_sample ⇒ Object
- #code_select(argument, **globals) ⇒ Object
- #code_size ⇒ Object
- #code_step(argument = nil, function = nil, **globals) ⇒ Object
- #code_to_dictionary ⇒ Object
- #code_to_list ⇒ Object
- #exclude_end? ⇒ Boolean
-
#initialize(*args, **kargs, &_block) ⇒ Range
constructor
A new instance of Range.
Constructor Details
#initialize(*args, **kargs, &_block) ⇒ Range
Returns a new instance of Range.
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 |
# File 'lib/code/object/range.rb', line 244 def initialize(*args, **kargs, &_block) if args.first.is_a?(Range) @code_left = args.first.code_left @code_right = args.first.code_right @code_options = args.first. @code_exclude_end = args.first.code_exclude_end elsif args.first.is_a?(List) @code_left = args.first.code_first @code_right = args.first.code_last @code_options = Dictionary.new(args.second.presence || kargs) @code_exclude_end = Boolean.new(.code_get(:exclude_end)) else @code_left = (args.first.to_code.nothing? ? Integer.new(0) : args.first.to_code) @code_right = if args.second.to_code.nothing? Integer.new(0) else args.second.to_code end @code_options = Dictionary.new(args.third.presence || kargs) @code_exclude_end = Boolean.new(.code_get(:exclude_end)) end self.raw = ::Range.new(code_left, code_right, exclude_end?) end |
Instance Attribute Details
#code_exclude_end ⇒ Object (readonly)
Returns the value of attribute code_exclude_end.
242 243 244 |
# File 'lib/code/object/range.rb', line 242 def code_exclude_end @code_exclude_end end |
#code_left ⇒ Object (readonly)
Returns the value of attribute code_left.
242 243 244 |
# File 'lib/code/object/range.rb', line 242 def code_left @code_left end |
#code_options ⇒ Object (readonly)
Returns the value of attribute code_options.
242 243 244 |
# File 'lib/code/object/range.rb', line 242 def @code_options end |
#code_right ⇒ Object (readonly)
Returns the value of attribute code_right.
242 243 244 |
# File 'lib/code/object/range.rb', line 242 def code_right @code_right end |
Class Method Details
.function_documentation(scope) ⇒ Object
236 237 238 239 240 |
# File 'lib/code/object/range.rb', line 236 def self.function_documentation(scope) return INSTANCE_FUNCTIONS if scope == :instance {} end |
Instance Method Details
#call(**args) ⇒ Object
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 |
# File 'lib/code/object/range.rb', line 273 def call(**args) code_operator = args.fetch(:operator, nil).to_code code_arguments = args.fetch(:arguments, []).to_code globals = multi_fetch(args, *GLOBALS) code_value = code_arguments.code_first case code_operator.to_s when "all?" sig(args) { Function } code_all?(code_value, **globals) when "any?" sig(args) { Function } code_any?(code_value, **globals) when "none?" sig(args) { Function } code_none?(code_value, **globals) when "each" sig(args) { Function } code_each(code_value, **globals) when "reverse_each" sig(args) { Function } code_reverse_each(code_value, **globals) when "include?" sig(args) { Object } code_include?(code_value) when "member?" sig(args) { Object } code_member?(code_value) when "cover?" sig(args) { Object } code_cover?(code_value) when "overlap?" sig(args) { Range } code_overlap?(code_value) when "empty?" sig(args) code_empty? when "begin" sig(args) code_begin when "end" sig(args) code_end when "exclude_end?" sig(args) code_exclude_end? when "first" sig(args) code_first when "last" sig(args) code_last when "minimum" sig(args) code_minimum when "maximum" sig(args) code_maximum when "minimum_maximum" sig(args) code_minimum_maximum when "map" sig(args) { Function } code_map(code_value, **globals) when "select" sig(args) { Function } code_select(code_value, **globals) when "reject" sig(args) { Function } code_reject(code_value, **globals) when "reduce" sig(args) { Function } code_reduce(code_value, **globals) when "step" sig(args) { [(Integer | Decimal).maybe, Function.maybe] } code_step(*code_arguments.raw, **globals) when "binary_search" sig(args) { Function } code_binary_search(code_value, **globals) when "sample" sig(args) code_sample when "size" sig(args) code_size when "count" sig(args) { Function.maybe } code_count(code_value, **globals) when "to_list" sig(args) code_to_list when "entries" sig(args) code_entries when "to_dictionary" sig(args) code_to_dictionary else super end end |
#code_all?(argument, **globals) ⇒ Boolean
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 |
# File 'lib/code/object/range.rb', line 375 def code_all?(argument, **globals) code_argument = argument.to_code index = 0 Boolean.new( raw.all? do |code_element| code_argument .call( arguments: List.new([code_element, Integer.new(index), self]), **globals ) .truthy? .tap { index += 1 } rescue Error::Next => e e.code_value.truthy?.tap { index += 1 } end ) rescue Error::Break => e e.code_value end |
#code_any?(argument, **globals) ⇒ Boolean
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 |
# File 'lib/code/object/range.rb', line 397 def code_any?(argument, **globals) code_argument = argument.to_code index = 0 Boolean.new( raw.any? do |code_element| code_argument .call( arguments: List.new([code_element, Integer.new(index), self]), **globals ) .truthy? .tap { index += 1 } rescue Error::Next => e e.code_value.truthy?.tap { index += 1 } end ) rescue Error::Break => e e.code_value end |
#code_begin ⇒ Object
499 500 501 |
# File 'lib/code/object/range.rb', line 499 def code_begin code_left end |
#code_binary_search(argument, **globals) ⇒ Object
606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 |
# File 'lib/code/object/range.rb', line 606 def code_binary_search(argument, **globals) code_argument = argument.to_code values = raw.to_a lower = 0 upper = values.length - 1 result = nil while lower <= upper index = (lower + upper) / 2 code_element = values[index] matched = begin code_argument.call( arguments: List.new([code_element, Integer.new(index), self]), **globals ).truthy? rescue Error::Next => e e.code_value.truthy? end if matched result = code_element upper = index - 1 else lower = index + 1 end end result || Nothing.new rescue Error::Break => e e.code_value end |
#code_count(argument = nil, **globals) ⇒ Object
709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 |
# File 'lib/code/object/range.rb', line 709 def code_count(argument = nil, **globals) code_argument = argument.to_code return Integer.new(raw.to_a.size) if code_argument.nothing? index = 0 Integer.new( raw.count do |code_element| code_argument .call( arguments: List.new([code_element, Integer.new(index), self]), **globals ) .truthy? .tap { index += 1 } rescue Error::Next => e e.code_value.truthy?.tap { index += 1 } end ) rescue Error::Break => e e.code_value end |
#code_cover?(value) ⇒ Boolean
484 485 486 487 |
# File 'lib/code/object/range.rb', line 484 def code_cover?(value) code_value = value.to_code Boolean.new(raw.cover?(code_value)) end |
#code_each(argument, **globals) ⇒ Object
441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 |
# File 'lib/code/object/range.rb', line 441 def code_each(argument, **globals) code_argument = argument.to_code raw.each.with_index do |code_element, index| code_argument.call( arguments: List.new([code_element, Integer.new(index), self]), **globals ) rescue Error::Next => e e.code_value end self rescue Error::Break => e e.code_value end |
#code_empty? ⇒ Boolean
495 496 497 |
# File 'lib/code/object/range.rb', line 495 def code_empty? Boolean.new(raw.to_a.empty?) end |
#code_end ⇒ Object
503 504 505 |
# File 'lib/code/object/range.rb', line 503 def code_end code_right end |
#code_entries ⇒ Object
693 694 695 |
# File 'lib/code/object/range.rb', line 693 def code_entries code_to_list end |
#code_exclude_end? ⇒ Boolean
507 508 509 |
# File 'lib/code/object/range.rb', line 507 def code_exclude_end? Boolean.new(exclude_end?) end |
#code_first ⇒ Object
511 512 513 |
# File 'lib/code/object/range.rb', line 511 def code_first raw.first || Nothing.new end |
#code_include?(value) ⇒ Boolean
475 476 477 478 |
# File 'lib/code/object/range.rb', line 475 def code_include?(value) code_value = value.to_code Boolean.new(raw.include?(code_value)) end |
#code_last ⇒ Object
515 516 517 |
# File 'lib/code/object/range.rb', line 515 def code_last raw.last || Nothing.new end |
#code_map(argument, **globals) ⇒ Object
531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 |
# File 'lib/code/object/range.rb', line 531 def code_map(argument, **globals) code_argument = argument.to_code List.new( raw.map.with_index do |code_element, index| code_argument.call( arguments: List.new([code_element, Integer.new(index), self]), **globals ) rescue Error::Next => e e.code_value end ) rescue Error::Break => e e.code_value end |
#code_maximum ⇒ Object
523 524 525 |
# File 'lib/code/object/range.rb', line 523 def code_maximum raw.max || Nothing.new end |
#code_member?(value) ⇒ Boolean
480 481 482 |
# File 'lib/code/object/range.rb', line 480 def code_member?(value) code_include?(value) end |
#code_minimum ⇒ Object
519 520 521 |
# File 'lib/code/object/range.rb', line 519 def code_minimum raw.min || Nothing.new end |
#code_minimum_maximum ⇒ Object
527 528 529 |
# File 'lib/code/object/range.rb', line 527 def code_minimum_maximum List.new([code_minimum, code_maximum]) end |
#code_none?(argument, **globals) ⇒ Boolean
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 |
# File 'lib/code/object/range.rb', line 419 def code_none?(argument, **globals) code_argument = argument.to_code index = 0 Boolean.new( raw.none? do |code_element| code_argument .call( arguments: List.new([code_element, Integer.new(index), self]), **globals ) .truthy? .tap { index += 1 } rescue Error::Next => e e.code_value.truthy?.tap { index += 1 } end ) rescue Error::Break => e e.code_value end |
#code_overlap?(range) ⇒ Boolean
489 490 491 492 493 |
# File 'lib/code/object/range.rb', line 489 def code_overlap?(range) code_range = range.to_code Boolean.new(raw.overlap?(code_range.raw)) end |
#code_reduce(argument, **globals) ⇒ Object
582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 |
# File 'lib/code/object/range.rb', line 582 def code_reduce(argument, **globals) code_argument = argument.to_code index = 0 raw.reduce do |code_acc, code_element| code_argument .call( arguments: List.new([code_acc, code_element, Integer.new(index), self]), **globals ) .tap { index += 1 } rescue Error::Next => e e.code_value.tap { index += 1 } end || Nothing.new rescue Error::Break => e e.code_value end |
#code_reject(argument, **globals) ⇒ Object
565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 |
# File 'lib/code/object/range.rb', line 565 def code_reject(argument, **globals) code_argument = argument.to_code List.new( raw.reject.with_index do |code_element, index| code_argument.call( arguments: List.new([code_element, Integer.new(index), self]), **globals ).truthy? rescue Error::Next => e e.code_value.truthy? end ) rescue Error::Break => e e.code_value end |
#code_reverse_each(argument, **globals) ⇒ Object
458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 |
# File 'lib/code/object/range.rb', line 458 def code_reverse_each(argument, **globals) code_argument = argument.to_code raw.reverse_each.with_index do |code_element, index| code_argument.call( arguments: List.new([code_element, Integer.new(index), self]), **globals ) rescue Error::Next => e e.code_value end self rescue Error::Break => e e.code_value end |
#code_sample ⇒ Object
701 702 703 |
# File 'lib/code/object/range.rb', line 701 def code_sample code_to_list.code_sample end |
#code_select(argument, **globals) ⇒ Object
548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 |
# File 'lib/code/object/range.rb', line 548 def code_select(argument, **globals) code_argument = argument.to_code List.new( raw.select.with_index do |code_element, index| code_argument.call( arguments: List.new([code_element, Integer.new(index), self]), **globals ).truthy? rescue Error::Next => e e.code_value.truthy? end ) rescue Error::Break => e e.code_value end |
#code_size ⇒ Object
705 706 707 |
# File 'lib/code/object/range.rb', line 705 def code_size Integer.new(raw.to_a.size) end |
#code_step(argument = nil, function = nil, **globals) ⇒ Object
639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 |
# File 'lib/code/object/range.rb', line 639 def code_step(argument = nil, function = nil, **globals) code_argument = argument.to_code code_function = function.to_code if code_argument.is_a?(Function) code_function = code_argument code_argument = Integer.new(1) elsif code_argument.nothing? code_argument = Integer.new(1) end code_list = List.new code_element = code_left index = 0 step_is_positive = code_argument.code_greater(Integer.new(0)).truthy? loop do comparison = if step_is_positive if exclude_end? code_element.code_less(code_right) else code_element.code_less_or_equal(code_right) end elsif exclude_end? code_element.code_greater(code_right) else code_element.code_greater_or_equal(code_right) end break unless comparison.truthy? if code_function.is_a?(Function) code_function.call( arguments: List.new([code_element, Integer.new(index), self]), **globals ) else code_list.code_append(code_element) end index += 1 code_element = code_element.code_plus(code_argument) end code_function.is_a?(Function) ? self : code_list rescue Error::Break => e e.code_value end |
#code_to_dictionary ⇒ Object
697 698 699 |
# File 'lib/code/object/range.rb', line 697 def code_to_dictionary code_to_list.code_to_dictionary end |
#code_to_list ⇒ Object
689 690 691 |
# File 'lib/code/object/range.rb', line 689 def code_to_list List.new(raw.to_a) end |
#exclude_end? ⇒ Boolean
602 603 604 |
# File 'lib/code/object/range.rb', line 602 def exclude_end? code_exclude_end.truthy? end |