| 1 | package Sidef::Types::Range::Range; | ||
| 2 | |||
| 3 | use utf8; | ||
| 4 | use 5.016; | ||
| 5 | |||
| 6 | use List::Util qw(); | ||
| 7 | use Sidef::Types::Bool::Bool; | ||
| 8 | use Sidef::Types::Number::Number; | ||
| 9 | use Sidef::Types::Block::Block; | ||
| 10 | |||
| 11 | use overload '@{}' => sub { | ||
| 12 | $_[0]->{_cached_array} //= do { | ||
| 13 | my @array; | ||
| 14 | my $iter = $_[0]->iter; | ||
| 15 | for (; ;) { | ||
| 16 | push @array, ($iter->run() // last); | ||
| 17 | } | ||
| 18 | \@array; | ||
| 19 | }; | ||
| 20 | }; | ||
| 21 | |||
| 22 | use parent qw(Sidef::Object::Object); | ||
| 23 | |||
| 24 | sub new { | ||
| 25 | shift(); | ||
| 26 | my $from = shift; | ||
| 27 | $from->range(@_); | ||
| 28 | } | ||
| 29 | |||
| 30 | *call = \&new; | ||
| 31 | |||
| 32 | sub by { | ||
| 33 | my ($self, $step) = @_; | ||
| 34 | defined($step) | ||
| 35 | ? $self->new( | ||
| 36 | $self->{from}, $self->{to}, $self->{step}->is_neg | ||
| 37 | ? $step->neg | ||
| 38 | : $step | ||
| 39 | ) | ||
| 40 | : $self->{step}; | ||
| 41 | } | ||
| 42 | |||
| 43 | sub from { | ||
| 44 | my ($self, $from) = @_; | ||
| 45 | defined($from) | ||
| 46 | ? $self->new($from, $self->{to}, $self->{step}) | ||
| 47 | : $self->{from}; | ||
| 48 | } | ||
| 49 | |||
| 50 | sub to { | ||
| 51 | my ($self, $to) = @_; | ||
| 52 | defined($to) | ||
| 53 | ? $self->new($self->{from}, $to, $self->{step}) | ||
| 54 | : $self->{to}; | ||
| 55 | } | ||
| 56 | |||
| 57 | sub min_by { | ||
| 58 | my ($self, $block) = @_; | ||
| 59 | |||
| 60 | $block //= Sidef::Types::Block::Block::IDENTITY; | ||
| 61 | |||
| 62 | my $iter = $self->iter; | ||
| 63 | |||
| 64 | my $min = $iter->run() // return undef; | ||
| 65 | my $min_value = $block->run($min); | ||
| 66 | |||
| 67 | while (1) { | ||
| 68 | my $curr = $iter->run() // last; | ||
| 69 | my $curr_value = $block->run($curr); | ||
| 70 | |||
| 71 | if (CORE::int($curr_value cmp $min_value) < 0) { | ||
| 72 | $min = $curr; | ||
| 73 | $min_value = $curr_value; | ||
| 74 | } | ||
| 75 | } | ||
| 76 | |||
| 77 | $min; | ||
| 78 | } | ||
| 79 | |||
| 80 | sub min { | ||
| 81 | my ($self, $block) = @_; | ||
| 82 | |||
| 83 | if (defined($block)) { | ||
| 84 | goto &min_by; | ||
| 85 | } | ||
| 86 | |||
| 87 | ($self->{_asc} //= !!$self->{step}->is_pos) | ||
| 88 | ? $self->{from} | ||
| 89 | : $self->{to}; | ||
| 90 | } | ||
| 91 | |||
| 92 | sub max_by { | ||
| 93 | my ($self, $block) = @_; | ||
| 94 | |||
| 95 | $block //= Sidef::Types::Block::Block::IDENTITY; | ||
| 96 | |||
| 97 | my $iter = $self->iter; | ||
| 98 | |||
| 99 | my $max = $iter->run() // return undef; | ||
| 100 | my $max_value = $block->run($max); | ||
| 101 | |||
| 102 | while (1) { | ||
| 103 | my $curr = $iter->run() // last; | ||
| 104 | my $curr_value = $block->run($curr); | ||
| 105 | |||
| 106 | if (CORE::int($curr_value cmp $max_value) > 0) { | ||
| 107 | $max = $curr; | ||
| 108 | $max_value = $curr_value; | ||
| 109 | } | ||
| 110 | } | ||
| 111 | |||
| 112 | $max; | ||
| 113 | } | ||
| 114 | |||
| 115 | sub max { | ||
| 116 | my ($self, $block) = @_; | ||
| 117 | |||
| 118 | if (defined($block)) { | ||
| 119 | goto &max_by; | ||
| 120 | } | ||
| 121 | |||
| 122 | ($self->{_asc} //= !!$self->{step}->is_pos) | ||
| 123 | ? $self->{to} | ||
| 124 | : $self->{from}; | ||
| 125 | } | ||
| 126 | |||
| 127 | sub min_max { | ||
| 128 | my ($self, $block) = @_; | ||
| 129 | $block //= Sidef::Types::Block::Block::IDENTITY; | ||
| 130 | |||
| 131 | my $iter = $self->iter; | ||
| 132 | my $first = $iter->run() // return (undef, undef); | ||
| 133 | |||
| 134 | my ($min, $max) = ($first, $first); | ||
| 135 | my ($min_v, $max_v) = ($block->run($first), $block->run($first)); | ||
| 136 | |||
| 137 | while (1) { | ||
| 138 | my $curr = $iter->run() // last; | ||
| 139 | my $curr_v = $block->run($curr); | ||
| 140 | if (CORE::int($curr_v cmp $min_v) < 0) { $min = $curr; $min_v = $curr_v; } | ||
| 141 | if (CORE::int($curr_v cmp $max_v) > 0) { $max = $curr; $max_v = $curr_v; } | ||
| 142 | } | ||
| 143 | |||
| 144 | ($min, $max); | ||
| 145 | } | ||
| 146 | |||
| 147 | sub partition { | ||
| 148 | my ($self, $block) = @_; | ||
| 149 | $block //= Sidef::Types::Block::Block::IDENTITY; | ||
| 150 | |||
| 151 | my (@yes, @no); | ||
| 152 | my $iter = $self->iter; | ||
| 153 | for (; ;) { | ||
| 154 | my $obj = $iter->run() // last; | ||
| 155 | $block->run($obj) ? push(@yes, $obj) : push(@no, $obj); | ||
| 156 | } | ||
| 157 | |||
| 158 | (Sidef::Types::Array::Array->new(\@yes), Sidef::Types::Array::Array->new(\@no)); | ||
| 159 | } | ||
| 160 | |||
| 161 | sub step { | ||
| 162 | $_[0]->{step}; | ||
| 163 | } | ||
| 164 | |||
| 165 | sub bounds { | ||
| 166 | my ($self) = @_; | ||
| 167 | ($self->min, $self->max); | ||
| 168 | } | ||
| 169 | |||
| 170 | sub reverse { | ||
| 171 | my ($self) = @_; | ||
| 172 | $self->new($self->{to}, $self->{from}, $self->{step}->neg); | ||
| 173 | } | ||
| 174 | |||
| 175 | *flip = \&reverse; | ||
| 176 | |||
| 177 | sub first_by { | ||
| 178 | my ($self, $block) = @_; | ||
| 179 | |||
| 180 | $block //= Sidef::Types::Block::Block::IDENTITY; | ||
| 181 | |||
| 182 | my $iter = $self->iter; | ||
| 183 | |||
| 184 | for (; ;) { | ||
| 185 | my $obj = $iter->run() // last; | ||
| 186 | return $obj if $block->run($obj); | ||
| 187 | } | ||
| 188 | |||
| 189 | undef; | ||
| 190 | } | ||
| 191 | |||
| 192 | sub first { | ||
| 193 | my ($self, $num) = @_; | ||
| 194 | |||
| 195 | if (ref($num) eq 'Sidef::Types::Block::Block') { | ||
| 196 | goto &first_by; | ||
| 197 | } | ||
| 198 | |||
| 199 | my $iter = $self->iter; | ||
| 200 | |||
| 201 | if (defined $num) { | ||
| 202 | |||
| 203 | my @array; | ||
| 204 | foreach my $i (1 .. CORE::int($num)) { | ||
| 205 | my $item = $iter->run() // last; | ||
| 206 | push @array, $item; | ||
| 207 | } | ||
| 208 | |||
| 209 | return Sidef::Types::Array::Array->new(\@array); | ||
| 210 | } | ||
| 211 | |||
| 212 | $iter->run() // undef; | ||
| 213 | } | ||
| 214 | |||
| 215 | *head = \&first; | ||
| 216 | |||
| 217 | sub last { | ||
| 218 | my ($self, $num) = @_; | ||
| 219 | |||
| 220 | if (ref($num) eq 'Sidef::Types::Block::Block') { | ||
| 221 | return $self->reverse->first_by($num); | ||
| 222 | } | ||
| 223 | |||
| 224 | defined($num) | ||
| 225 | ? $self->reverse->first($num)->reverse | ||
| 226 | : $self->reverse->first; | ||
| 227 | } | ||
| 228 | |||
| 229 | *tail = \&last; | ||
| 230 | |||
| 231 | sub last_by { | ||
| 232 | my ($self, $block) = @_; | ||
| 233 | $self->reverse->first_by($block); | ||
| 234 | } | ||
| 235 | |||
| 236 | sub contains { | ||
| 237 | my ($self, $value) = @_; | ||
| 238 | |||
| 239 | my $step = $self->{step}; | ||
| 240 | my $asc = ($self->{_asc} //= !!$step->is_pos); | ||
| 241 | |||
| 242 | my ($from, $to) = ( | ||
| 243 | $asc | ||
| 244 | ? ($self->{from}, $self->{to}) | ||
| 245 | : ($self->{to}, $self->{from}) | ||
| 246 | ); | ||
| 247 | |||
| 248 | ( | ||
| 249 | $value->ge($from) | ||
| 250 | and $value->le($to) | ||
| 251 | and ( | ||
| 252 | do { $self->{_is_one} //= ($asc ? $step->is_one : $step->abs->is_one) } | ||
| 253 | && $value->is_int ? 1 | ||
| 254 | : $value->sub( | ||
| 255 | $asc ? $from | ||
| 256 | : $to | ||
| 257 | )->div($step)->int->mul($step)->eq( | ||
| 258 | $value->sub( $asc ? $from | ||
| 259 | : $to) | ||
| 260 | ) | ||
| 261 | ) | ||
| 262 | ) ? (Sidef::Types::Bool::Bool::TRUE) | ||
| 263 | : (Sidef::Types::Bool::Bool::FALSE); | ||
| 264 | } | ||
| 265 | |||
| 266 | *contain = \&contains; | ||
| 267 | *include = \&contains; | ||
| 268 | *includes = \&contains; | ||
| 269 | |||
| 270 | sub length { | ||
| 271 | my ($self) = @_; | ||
| 272 | my $len = $self->{to}->sub($self->{from})->add($self->{step})->div($self->{step})->int; | ||
| 273 | $len->is_neg ? Sidef::Types::Number::Number::ZERO : $len; | ||
| 274 | } | ||
| 275 | |||
| 276 | *len = \&length; | ||
| 277 | |||
| 278 | sub each { | ||
| 279 | my ($self, $block) = @_; | ||
| 280 | |||
| 281 | my $iter = $self->iter; | ||
| 282 | |||
| 283 | for (; ;) { | ||
| 284 | $block->run($iter->run() // last); | ||
| 285 | } | ||
| 286 | |||
| 287 | $self; | ||
| 288 | } | ||
| 289 | |||
| 290 | *for = \&each; | ||
| 291 | *foreach = \&each; | ||
| 292 | |||
| 293 | sub group_by { | ||
| 294 | my ($self, $block) = @_; | ||
| 295 | my (%groups, @order); | ||
| 296 | my $iter = $self->iter; | ||
| 297 | for (; ;) { | ||
| 298 | my $obj = $iter->run() // last; | ||
| 299 | my $key = $block->run($obj); | ||
| 300 | my $k = "$key"; | ||
| 301 | push(@order, $key) unless exists $groups{$k}; | ||
| 302 | push @{$groups{$k}}, $obj; | ||
| 303 | } | ||
| 304 | Sidef::Types::Hash::Hash->new(map { ("$_" => Sidef::Types::Array::Array->new($groups{"$_"})) } @order); | ||
| 305 | } | ||
| 306 | |||
| 307 | sub each_kv { | ||
| 308 | my ($self, $block) = @_; | ||
| 309 | my $i = 0; | ||
| 310 | my $iter = $self->iter; | ||
| 311 | for (; ;) { | ||
| 312 | my $obj = $iter->run() // last; | ||
| 313 | $block->run(Sidef::Types::Number::Number::_set_int($i++), $obj); | ||
| 314 | } | ||
| 315 | $self; | ||
| 316 | } | ||
| 317 | |||
| 318 | sub while { | ||
| 319 | my ($self, $block) = @_; | ||
| 320 | |||
| 321 | my $iter = $self->iter; | ||
| 322 | |||
| 323 | for (; ;) { | ||
| 324 | $block->run($iter->run() // last) || last; | ||
| 325 | } | ||
| 326 | |||
| 327 | $self; | ||
| 328 | } | ||
| 329 | |||
| 330 | sub each_cons { | ||
| 331 | my ($self, $n, $block) = @_; | ||
| 332 | |||
| 333 | $n = CORE::int($n); | ||
| 334 | $n > 0 or return $self; | ||
| 335 | |||
| 336 | my @values; | ||
| 337 | my $count = 0; | ||
| 338 | my $iter = $self->iter; | ||
| 339 | |||
| 340 | for (; ;) { | ||
| 341 | my $value = $iter->run() // last; | ||
| 342 | |||
| 343 | if (++$count > $n) { | ||
| 344 | shift @values; | ||
| 345 | --$count; | ||
| 346 | } | ||
| 347 | |||
| 348 | push @values, $value; | ||
| 349 | |||
| 350 | if ($count == $n) { | ||
| 351 | $block->run(@values); | ||
| 352 | } | ||
| 353 | } | ||
| 354 | |||
| 355 | $self; | ||
| 356 | } | ||
| 357 | |||
| 358 | sub each_slice { | ||
| 359 | my ($self, $n, $block) = @_; | ||
| 360 | |||
| 361 | $n = CORE::int($n); | ||
| 362 | $n > 0 or return $self; | ||
| 363 | |||
| 364 | my @values; | ||
| 365 | my $count = 0; | ||
| 366 | my $iter = $self->iter; | ||
| 367 | |||
| 368 | for (; ;) { | ||
| 369 | my $value = $iter->run() // do { | ||
| 370 | $block->run(@values) if @values; | ||
| 371 | return $self; | ||
| 372 | }; | ||
| 373 | |||
| 374 | push @values, $value; | ||
| 375 | |||
| 376 | if (++$count == $n) { | ||
| 377 | $count = 0; | ||
| 378 | $block->run(splice(@values)); | ||
| 379 | } | ||
| 380 | } | ||
| 381 | |||
| 382 | $self; | ||
| 383 | } | ||
| 384 | |||
| 385 | sub map { | ||
| 386 | my ($self, $block) = @_; | ||
| 387 | |||
| 388 | $block //= Sidef::Types::Block::Block::IDENTITY; | ||
| 389 | |||
| 390 | my @values; | ||
| 391 | my $iter = $self->iter; | ||
| 392 | |||
| 393 | for (; ;) { | ||
| 394 | push @values, $block->run($iter->run() // last); | ||
| 395 | } | ||
| 396 | |||
| 397 | Sidef::Types::Array::Array->new(\@values); | ||
| 398 | } | ||
| 399 | |||
| 400 | sub zip { | ||
| 401 | my ($self, $other) = @_; | ||
| 402 | my $iter1 = $self->iter; | ||
| 403 | my $iter2 = $other->iter; | ||
| 404 | my @pairs; | ||
| 405 | for (; ;) { | ||
| 406 | my $a = $iter1->run() // last; | ||
| 407 | my $b = $iter2->run() // last; | ||
| 408 | push @pairs, Sidef::Types::Array::Array->new([$a, $b]); | ||
| 409 | } | ||
| 410 | Sidef::Types::Array::Array->new(\@pairs); | ||
| 411 | } | ||
| 412 | |||
| 413 | sub map_cons { | ||
| 414 | my ($self, $n, $block) = @_; | ||
| 415 | $self->to_a->map_cons($n, $block); | ||
| 416 | } | ||
| 417 | |||
| 418 | *cons = \&map_cons; | ||
| 419 | |||
| 420 | sub map_slice { | ||
| 421 | my ($self, $n, $block) = @_; | ||
| 422 | $self->to_a->map_slice($n, $block); | ||
| 423 | } | ||
| 424 | |||
| 425 | *slices = \&map_slice; | ||
| 426 | |||
| 427 | sub grep { | ||
| 428 | my ($self, $block) = @_; | ||
| 429 | |||
| 430 | $block //= Sidef::Types::Block::Block::IDENTITY; | ||
| 431 | |||
| 432 | my @values; | ||
| 433 | my $iter = $self->iter; | ||
| 434 | |||
| 435 | for (; ;) { | ||
| 436 | my $obj = $iter->run() // last; | ||
| 437 | push(@values, $obj) if $block->run($obj); | ||
| 438 | } | ||
| 439 | |||
| 440 | Sidef::Types::Array::Array->new(\@values); | ||
| 441 | } | ||
| 442 | |||
| 443 | *select = \&grep; | ||
| 444 | |||
| 445 | sub take_while { | ||
| 446 | my ($self, $block) = @_; | ||
| 447 | my @values; | ||
| 448 | my $iter = $self->iter; | ||
| 449 | for (; ;) { | ||
| 450 | my $obj = $iter->run() // last; | ||
| 451 | $block->run($obj) or last; | ||
| 452 | push @values, $obj; | ||
| 453 | } | ||
| 454 | Sidef::Types::Array::Array->new(\@values); | ||
| 455 | } | ||
| 456 | |||
| 457 | sub any { | ||
| 458 | my ($self, $block) = @_; | ||
| 459 | |||
| 460 | $block //= Sidef::Types::Block::Block::IDENTITY; | ||
| 461 | |||
| 462 | my $iter = $self->iter; | ||
| 463 | |||
| 464 | for (; ;) { | ||
| 465 | $block->run($iter->run() // last) | ||
| 466 | && return Sidef::Types::Bool::Bool::TRUE; | ||
| 467 | } | ||
| 468 | |||
| 469 | Sidef::Types::Bool::Bool::FALSE; | ||
| 470 | } | ||
| 471 | |||
| 472 | sub all { | ||
| 473 | my ($self, $block) = @_; | ||
| 474 | |||
| 475 | $block //= Sidef::Types::Block::Block::IDENTITY; | ||
| 476 | |||
| 477 | my $iter = $self->iter; | ||
| 478 | |||
| 479 | for (; ;) { | ||
| 480 | $block->run($iter->run() // last) | ||
| 481 | || return Sidef::Types::Bool::Bool::FALSE; | ||
| 482 | } | ||
| 483 | |||
| 484 | Sidef::Types::Bool::Bool::TRUE; | ||
| 485 | } | ||
| 486 | |||
| 487 | sub none { | ||
| 488 | my ($self, $block) = @_; | ||
| 489 | |||
| 490 | $block //= Sidef::Types::Block::Block::IDENTITY; | ||
| 491 | |||
| 492 | my $iter = $self->iter; | ||
| 493 | |||
| 494 | for (; ;) { | ||
| 495 | $block->run($iter->run() // last) | ||
| 496 | && return Sidef::Types::Bool::Bool::FALSE; | ||
| 497 | } | ||
| 498 | |||
| 499 | Sidef::Types::Bool::Bool::TRUE; | ||
| 500 | } | ||
| 501 | |||
| 502 | sub to_array { | ||
| 503 | my ($self) = @_; | ||
| 504 | |||
| 505 | ref($self) || return Sidef::Types::Array::Array->new($self); | ||
| 506 | |||
| 507 | my @array; | ||
| 508 | my $iter = $self->iter; | ||
| 509 | |||
| 510 | for (; ;) { | ||
| 511 | push @array, ($iter->run() // last); | ||
| 512 | } | ||
| 513 | |||
| 514 | Sidef::Types::Array::Array->new(\@array); | ||
| 515 | } | ||
| 516 | |||
| 517 | *to_a = \&to_array; | ||
| 518 | |||
| 519 | sub sort { | ||
| 520 | my ($self, $block) = @_; | ||
| 521 | $self->to_array->sort($block); | ||
| 522 | } | ||
| 523 | |||
| 524 | sub sort_by { | ||
| 525 | my ($self, $block) = @_; | ||
| 526 | $self->to_array->sort_by($block); | ||
| 527 | } | ||
| 528 | |||
| 529 | sub to_vector { | ||
| 530 | my ($self) = @_; | ||
| 531 | $self->to_a->to_v; | ||
| 532 | } | ||
| 533 | |||
| 534 | *to_v = \&to_vector; | ||
| 535 | *to_vec = \&to_vector; | ||
| 536 | |||
| 537 | sub to_list { | ||
| 538 | my ($self) = @_; | ||
| 539 | |||
| 540 | ref($self) or return; | ||
| 541 | |||
| 542 | my @array; | ||
| 543 | my $iter = $self->iter; | ||
| 544 | |||
| 545 | for (; ;) { | ||
| 546 | push @array, ($iter->run() // last); | ||
| 547 | } | ||
| 548 | |||
| 549 | (@array); | ||
| 550 | } | ||
| 551 | |||
| 552 | sub join { | ||
| 553 | my ($self, $sep) = @_; | ||
| 554 | Sidef::Types::String::String->new(CORE::join("$sep", $self->to_list)); | ||
| 555 | } | ||
| 556 | |||
| 557 | sub kv { | ||
| 558 | my ($self) = @_; | ||
| 559 | $self->to_array->kv; | ||
| 560 | } | ||
| 561 | |||
| 562 | *pairs = \&kv; | ||
| 563 | *zip_indices = \&kv; | ||
| 564 | |||
| 565 | sub accumulate { | ||
| 566 | my ($self, $arg) = @_; | ||
| 567 | $self->to_array->accumulate($arg); | ||
| 568 | } | ||
| 569 | |||
| 570 | sub accumulate_by { | ||
| 571 | my ($self, $arg) = @_; | ||
| 572 | $self->to_array->accumulate_by($arg); | ||
| 573 | } | ||
| 574 | |||
| 575 | sub rand { | ||
| 576 | my ($self, $n) = @_; | ||
| 577 | |||
| 578 | my $from = $self->{from}; | ||
| 579 | my $to = $self->{to}; | ||
| 580 | my $step = $self->{step}; | ||
| 581 | |||
| 582 | my $limit = $to->sub($from)->div($step); | ||
| 583 | my $is_empty = $limit->lt(Sidef::Types::Number::Number::ZERO); | ||
| 584 | |||
| 585 | if ($is_empty) { | ||
| 586 | return (defined($n) ? Sidef::Types::Array::Array->new([]) : undef); | ||
| 587 | } | ||
| 588 | |||
| 589 | if (not defined $n) { | ||
| 590 | return $limit->irand->mul($step)->add($from); | ||
| 591 | } | ||
| 592 | |||
| 593 | my @array; | ||
| 594 | for (1 .. CORE::int($n)) { | ||
| 595 | push @array, $limit->irand->mul($step)->add($from); | ||
| 596 | } | ||
| 597 | |||
| 598 | Sidef::Types::Array::Array->new(\@array); | ||
| 599 | } | ||
| 600 | |||
| 601 | *sample = \&rand; | ||
| 602 | |||
| 603 | sub pick { | ||
| 604 | my ($self, $n) = @_; | ||
| 605 | |||
| 606 | my $from = $self->{from}; | ||
| 607 | my $to = $self->{to}; | ||
| 608 | my $step = $self->{step}; | ||
| 609 | |||
| 610 | my $limit = $to->sub($from)->div($step); | ||
| 611 | my $is_empty = $limit->lt(Sidef::Types::Number::Number::ZERO); | ||
| 612 | |||
| 613 | if ($is_empty) { | ||
| 614 | return (defined($n) ? Sidef::Types::Array::Array->new([]) : undef); | ||
| 615 | } | ||
| 616 | |||
| 617 | if (not defined $n) { | ||
| 618 | return $limit->irand->mul($step)->add($from); | ||
| 619 | } | ||
| 620 | |||
| 621 | my (%seen, @array); | ||
| 622 | my $amount = CORE::int($n); | ||
| 623 | my $total = CORE::int($limit) + 1; | ||
| 624 | |||
| 625 | if ($amount <= 0) { | ||
| 626 | return Sidef::Types::Array::Array->new([]); | ||
| 627 | } | ||
| 628 | |||
| 629 | while (1) { | ||
| 630 | my $rand = $limit->irand->mul($step)->add($from); | ||
| 631 | last if keys(%seen) == $total; | ||
| 632 | next if $seen{$rand}++; | ||
| 633 | push @array, $rand; | ||
| 634 | last if --$amount == 0; | ||
| 635 | } | ||
| 636 | |||
| 637 | Sidef::Types::Array::Array->new(\@array); | ||
| 638 | } | ||
| 639 | |||
| 640 | sub count_by { | ||
| 641 | my ($self, $arg) = @_; | ||
| 642 | |||
| 643 | my $count = 0; | ||
| 644 | my $iter = $self->iter; | ||
| 645 | |||
| 646 | for (; ;) { | ||
| 647 | ++$count if $arg->run($iter->run() // last); | ||
| 648 | } | ||
| 649 | |||
| 650 | Sidef::Types::Number::Number::_set_int($count); | ||
| 651 | } | ||
| 652 | |||
| 653 | sub count { | ||
| 654 | my ($self, $arg) = @_; | ||
| 655 | |||
| 656 | if (ref($arg) eq 'Sidef::Types::Block::Block') { | ||
| 657 | goto &count_by; | ||
| 658 | } | ||
| 659 | |||
| 660 | $self->contains($arg) | ||
| 661 | ? Sidef::Types::Number::Number::ONE | ||
| 662 | : Sidef::Types::Number::Number::ZERO; | ||
| 663 | } | ||
| 664 | |||
| 665 | sub shuffle { | ||
| 666 | Sidef::Types::Array::Array->new([List::Util::shuffle($_[0]->to_list)]); | ||
| 667 | } | ||
| 668 | |||
| 669 | sub reduce { | ||
| 670 | my ($self, $op, $initial) = @_; | ||
| 671 | |||
| 672 | if (ref($op) eq 'Sidef::Types::Block::Block') { | ||
| 673 | |||
| 674 | my $iter = $self->iter; | ||
| 675 | my $value = $initial // $iter->run(); | ||
| 676 | |||
| 677 | for (; ;) { | ||
| 678 | $value = $op->run($value, $iter->run() // last); | ||
| 679 | } | ||
| 680 | |||
| 681 | return $value; | ||
| 682 | } | ||
| 683 | |||
| 684 | $self->reduce_operator("$op", $initial); | ||
| 685 | } | ||
| 686 | |||
| 687 | sub reduce_operator { | ||
| 688 | my ($self, $op, $initial) = @_; | ||
| 689 | |||
| 690 | $op = "$op" if ref($op); | ||
| 691 | |||
| 692 | my $iter = $self->iter; | ||
| 693 | my $value = $initial // $iter->run(); | ||
| 694 | |||
| 695 | for (; ;) { | ||
| 696 | $value = $value->$op($iter->run() // last); | ||
| 697 | } | ||
| 698 | |||
| 699 | $value; | ||
| 700 | } | ||
| 701 | |||
| 702 | sub map_operator { | ||
| 703 | my ($self, @args) = @_; | ||
| 704 | $self->to_array->map_operator(@args); | ||
| 705 | } | ||
| 706 | |||
| 707 | sub pam_operator { | ||
| 708 | my ($self, @args) = @_; | ||
| 709 | $self->to_array->pam_operator(@args); | ||
| 710 | } | ||
| 711 | |||
| 712 | sub unroll_operator { | ||
| 713 | my ($self, @args) = @_; | ||
| 714 | $self->to_array->unroll_operator(@args); | ||
| 715 | } | ||
| 716 | |||
| 717 | sub cross_operator { | ||
| 718 | my ($self, @args) = @_; | ||
| 719 | $self->to_array->cross_operator(@args); | ||
| 720 | } | ||
| 721 | |||
| 722 | sub zip_operator { | ||
| 723 | my ($self, @args) = @_; | ||
| 724 | $self->to_array->zip_operator(@args); | ||
| 725 | } | ||
| 726 | |||
| 727 | sub add { | ||
| 728 | my ($self, $arg) = @_; | ||
| 729 | $self->new($self->{from}->add($arg), $self->{to}->add($arg), $self->{step}); | ||
| 730 | } | ||
| 731 | |||
| 732 | sub sub { | ||
| 733 | my ($self, $arg) = @_; | ||
| 734 | $self->new($self->{from}->sub($arg), $self->{to}->sub($arg), $self->{step}); | ||
| 735 | } | ||
| 736 | |||
| 737 | sub mul { | ||
| 738 | my ($self, $arg) = @_; | ||
| 739 | $self->new($self->{from}->mul($arg), $self->{to}->mul($arg), $self->{step}); | ||
| 740 | } | ||
| 741 | |||
| 742 | sub div { | ||
| 743 | my ($self, $arg) = @_; | ||
| 744 | $self->new($self->{from}->div($arg), $self->{to}->div($arg), $self->{step}); | ||
| 745 | } | ||
| 746 | |||
| 747 | sub neg { | ||
| 748 | my ($self) = @_; | ||
| 749 | $self->new($self->{from}->neg, $self->{to}, $self->{step}); | ||
| 750 | } | ||
| 751 | |||
| 752 | sub eq { | ||
| 753 | my ($r1, $r2) = @_; | ||
| 754 | |||
| 755 | ref($r1) eq ref($r2) | ||
| 756 | && $r1->{from}->eq($r2->{from}) | ||
| 757 | && $r1->{to}->eq($r2->{to}) | ||
| 758 | && $r1->{step}->eq($r2->{step}) | ||
| 759 | |||
| 760 | ? (Sidef::Types::Bool::Bool::TRUE) | ||
| 761 | : (Sidef::Types::Bool::Bool::FALSE); | ||
| 762 | } | ||
| 763 | |||
| 764 | sub ne { | ||
| 765 | my ($r1, $r2) = @_; | ||
| 766 | $r1->eq($r2)->not; | ||
| 767 | } | ||
| 768 | |||
| 769 | { | ||
| 770 | no strict 'refs'; | ||
| 771 | *{__PACKAGE__ . '::' . '+'} = \&add; | ||
| 772 | *{__PACKAGE__ . '::' . '-'} = \⊂ | ||
| 773 | *{__PACKAGE__ . '::' . '*'} = \&mul; | ||
| 774 | *{__PACKAGE__ . '::' . '/'} = \÷ | ||
| 775 | *{__PACKAGE__ . '::' . '÷'} = \÷ | ||
| 776 | *{__PACKAGE__ . '::' . '=='} = \&eq; | ||
| 777 | *{__PACKAGE__ . '::' . '!='} = \≠ | ||
| 778 | *{__PACKAGE__ . '::' . '≠'} = \≠ | ||
| 779 | *{__PACKAGE__ . '::' . '...'} = \&to_list; | ||
| 780 | } | ||
| 781 | |||
| 782 | 1 |