[r7647]: sandbox / jlf / samples / extension / functional-test.rex  Maximize  Restore  History

Download this file

1019 lines (670 with data), 35.9 kB

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 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
 272
 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
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 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
 638
 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
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
/*
This script needs a modified ooRexx interpreter which allows to extend predefined ooRexx classes.
Something similar to C# extension methods :
http://msdn.microsoft.com/en-us/library/bb383977.aspx
http://weblogs.asp.net/scottgu/archive/2007/03/13/new-orcas-language-feature-extension-methods.aspx
Here, the extensions are declared with ::extension instead of ~define (see ..\functional for ~define)
Organization of this file :
Doer of type message
Reducing
Collection reduce
String reduce
Mutable buffer reduce
Coactivity reduce
Mapping
Collection map
String map
Mutable buffer map
Doer of type routine
Reducing
Collection reduce
String reduce
Mutable buffer reduce
Coactivity reduce
Mapping
Collection map
String map
Mutable buffer map
Repeating & collecting : times, upto, downto
Iterating & collecting
Collection each
Supplier each
String each
Mutable buffer each
Coactivity each
Doer of type method
Mapping
*/
call evaluate "demonstration"
say
say "Ended coactivities:" .Coactivity~endAll
--::options trace i
::routine demonstration
--- Strange... without this instruction, I don't get the first comments
nop
-- --------------------------------------------------------------
-- Reducing a collection with a message
-- --------------------------------------------------------------
-- A doer of type string is a message name
-- Reduce
-- arg(1) - accu : accumulated result
-- arg(2) - item : current item of collection
-- arg(3) - index : current index of collection (passed if the action has the ~functionDoer method)
-- The messages like "+" "-" etc. have no ~functionDoer method, and as such are called with two arguments (no index).
-- Any doer which is created from a RexxBlock is called with three arguments (index).
-- Ordered collection, the operation can be non-commutative
.Array~of(1,2,3)~reduce("-")~dump -- initial value is the first item (default)
.Array~of(1,2,3)~reduce(100, "-")~dump -- initial value is 100
-- Non-ordered collection, the operation must be commutative
.Bag~of(1,2,3)~reduce("+")~dump
.Bag~of(1,2,3)~reduce(100, "+")~dump
-- Ordered collection, the operation can be non-commutative
.CircularQueue~of(1,2,3)~reduce("-")~dump
.CircularQueue~of(1,2,3)~reduce(100, "-")~dump
-- Non-ordered collection, the operation must be commutative
.Directory~new~~put(1, "v1")~~put(2, "v2")~~put(3, "v3")~reduce("+")~dump
.Directory~new~~put(1, "v1")~~put(2, "v2")~~put(3, "v3")~reduce(100, "+")~dump
-- Ordered collection, the operation can be non-commutative
.List~of(1,2,3)~reduce("-")~dump
.List~of(1,2,3)~reduce(100, "-")~dump
-- Non-ordered collection, the operation must be commutative
.Properties~new~~put(1, "v1")~~put(2, "v2")~~put(3, "v3")~reduce("+")~dump
.Properties~new~~put(1, "v1")~~put(2, "v2")~~put(3, "v3")~reduce(100, "+")~dump
-- Ordered collection, the operation can be non-commutative
.Queue~of(1,2,3)~reduce("-")~dump
.Queue~of(1,2,3)~reduce(100, "-")~dump
-- Non-ordered collection, the operation must be commutative
.Relation~new~~put(1, "v1")~~put(2, "v2")~~put(3, "v3")~reduce("+")~dump
.Relation~new~~put(1, "v1")~~put(2, "v2")~~put(3, "v3")~reduce(100, "+")~dump
-- A relation which has more than one item per index is supported
.Relation~new~~put(1, "v1")~~put(2, "v1")~~put(3, "v3")~reduce("+")~dump
.Relation~new~~put(1, "v1")~~put(2, "v1")~~put(3, "v3")~reduce(100, "+")~dump
-- Non-ordered collection, the operation must be commutative
.Set~of(1,2,3)~reduce("+")~dump
.Set~of(1,2,3)~reduce(100, "+")~dump
-- Non-ordered collection, the operation must be commutative
.Stem~new~~put(1, "v1")~~put(2, "v2")~~put(3, "v3")~reduce("+")~dump
.Stem~new~~put(1, "v1")~~put(2, "v2")~~put(3, "v3")~reduce(100, "+")~dump
-- Non-ordered collection, the operation must be commutative
.Table~new~~put(1, "v1")~~put(2, "v2")~~put(3, "v3")~reduce("+")~dump
.Table~new~~put(1, "v1")~~put(2, "v2")~~put(3, "v3")~reduce(100, "+")~dump
-- Non-ordered collection, the operation must be commutative
.IdentityTable~new~~put(1, "v1")~~put(2, "v2")~~put(3, "v3")~reduce("+")~dump
.IdentityTable~new~~put(1, "v1")~~put(2, "v2")~~put(3, "v3")~reduce(100, "+")~dump
-- --------------------------------------------------------------
-- Reducing a string with a message
-- --------------------------------------------------------------
-- initial value is the first char (default)
123~reduceC("-")~dump
-- initial value is 100
123~reduceC(100, "-")~dump
123~reduceC("min")~dump
123~reduceC("max")~dump
-- initial value is the first word (default)
"10 20 30"~reduceW("-")~dump
-- initial value is 100
"10 20 30"~reduceW(100, "-")~dump
-- --------------------------------------------------------------
-- Reducing a mutable buffer with a message
-- --------------------------------------------------------------
-- initial value is the first char (default)
.MutableBuffer~new(123)~reduceC("-")~dump
-- initial value is 100
.MutableBuffer~new(123)~reduceC(100, "-")~dump
.MutableBuffer~new(123)~reduceC("min")~dump
.MutableBuffer~new(123)~reduceC("max")~dump
-- initial value is the first word (default)
.MutableBuffer~new("10 20 30")~reduceW("-")~dump
-- initial value is 100
.MutableBuffer~new("10 20 30")~reduceW(100, "-")~dump
-- --------------------------------------------------------------
-- Reducing a coactivity with a message
-- --------------------------------------------------------------
-- The full tag is "::coactivity".
-- The shortest abbreviation is "::c".
-- initial value is the first yielded item (default)
{::coactivity .yield[10]; .yield[20]; .yield[30]}~doer~reduce("-")~dump
-- initial value is 100
{::coactivity .yield[10]; .yield[20]; .yield[30]}~doer~reduce(100, "-")~dump
-- --------------------------------------------------------------
-- Mapping a collection with a message, not-in-place
-- --------------------------------------------------------------
array = .Array~of(-1,2,-3)
array~dump -- collection before mapping
array~map("sign")~dump
array~dump -- collection after mapping (unchanged)
bag = .Bag~of(-1,2,-3)
bag~dump -- collection before mapping
bag~map("sign")~dump
bag~dump -- collection after mapping (unchanged)
circularQueue = .CircularQueue~of(-1,2,-3)
circularQueue~dump -- collection before mapping
circularQueue~map("sign")~dump
circularQueue~dump -- collection after mapping (unchanged)
directory = .Directory~new~~put(-1, "v1")~~put(2, "v2")~~put(-3, "v3")
directory~dump -- collection before mapping
directory~map("sign")~dump
directory~dump -- collection after mapping (unchanged)
list = .List~of(-1,2,-3)
list~dump -- collection before mapping
list~map("sign")~dump
list~dump -- collection after mapping (unchanged)
properties = .Properties~new~~put(-1, "v1")~~put(2, "v2")~~put(-3, "v3")
properties~dump -- collection before mapping
properties~map("sign")~dump
properties~dump -- collection after mapping (unchanged)
queue = .Queue~of(-1,2,-3)
queue~dump -- collection before mapping
queue~map("sign")~dump
queue~dump -- collection after mapping (unchanged)
relation = .Relation~new~~put(-1, "v1")~~put(2, "v2")~~put(-3, "v3")
relation~dump -- collection before mapping
relation~map("sign")~dump
relation~dump -- collection after mapping (unchanged)
-- A relation which has more than one item per index is supported
relation = .Relation~new~~put(-1, "v1")~~put(1, "v1")~~put(-3, "v3")
relation~dump -- collection before mapping
relation~map("sign")~dump
relation~dump -- collection after mapping (unchanged)
-- The resulting set has less items (normal... it's a set)
set = .Set~of(-1,2,-3)
set~dump -- collection before mapping
set~map("sign")~dump
set~dump -- collection after mapping (unchanged)
stem = .Stem~new~~put(-1, "v1")~~put(2, "v2")~~put(-3, "v3")
stem~dump -- collection before mapping
stem~map("sign")~dump
stem~dump -- collection after mapping (unchanged)
table = .Table~new~~put(-1, "v1")~~put(2, "v2")~~put(-3, "v3")
table~dump -- collection before mapping
table~map("sign")~dump
table~dump -- collection after mapping (unchanged)
identityTable = .IdentityTable~new~~put(-1, "v1")~~put(2, "v2")~~put(-3, "v3")
identityTable~dump -- collection before mapping
identityTable~map("sign")~dump
identityTable~dump -- collection after mapping (unchanged)
-- --------------------------------------------------------------
-- Mapping a collection with a message, in-place (replace the items)
-- --------------------------------------------------------------
array = .Array~of(-1,2,-3)
array~dump -- collection before mapping
array~mapR("sign")~dump
array~dump -- collection after mapping (impacted by mapping)
bag = .Bag~of(-1,2,-3)
bag~dump -- collection before mapping
bag~mapR("sign")~dump
bag~dump -- collection after mapping (impacted by mapping
circularQueue = .CircularQueue~of(-1,2,-3)
circularQueue~dump -- collection before mapping
circularQueue~mapR("sign")~dump
circularQueue~dump -- collection after mapping (impacted by mapping
directory = .Directory~new~~put(-1, "v1")~~put(2, "v2")~~put(-3, "v3")
directory~dump -- collection before mapping
directory~mapR("sign")~dump
directory~dump -- collection after mapping (impacted by mapping
list = .List~of(-1,2,-3)
list~dump -- collection before mapping
list~mapR("sign")~dump
list~dump -- collection after mapping (impacted by mapping
properties = .Properties~new~~put(-1, "v1")~~put(2, "v2")~~put(-3, "v3")
properties~dump -- collection before mapping
properties~mapR("sign")~dump
properties~dump -- collection after mapping (impacted by mapping
queue = .Queue~of(-1,2,-3)
queue~dump -- collection before mapping
queue~mapR("sign")~dump
queue~dump -- collection after mapping (impacted by mapping
relation = .Relation~new~~put(-1, "v1")~~put(2, "v2")~~put(-3, "v3")
relation~dump -- collection before mapping
relation~mapR("sign")~dump
relation~dump -- collection after mapping (impacted by mapping
-- A relation which has more than one item per index is supported
relation = .Relation~new~~put(-1, "v1")~~put(1, "v1")~~put(-3, "v3")
relation~dump -- collection before mapping
relation~mapR("sign")~dump
relation~dump -- collection after mapping (impacted by mapping
-- The resulting set has less items (normal... it's a set)
set = .Set~of(-1,2,-3)
set~dump -- collection before mapping
set~mapR("sign")~dump
set~dump -- collection after mapping (impacted by mapping
stem = .Stem~new~~put(-1, "v1")~~put(2, "v2")~~put(-3, "v3")
stem~dump -- collection before mapping
stem~mapR("sign")~dump
stem~dump -- collection after mapping (impacted by mapping
table = .Table~new~~put(-1, "v1")~~put(2, "v2")~~put(-3, "v3")
table~dump -- collection before mapping
table~mapR("sign")~dump
table~dump -- collection after mapping (impacted by mapping
identityTable = .IdentityTable~new~~put(-1, "v1")~~put(2, "v2")~~put(-3, "v3")
identityTable~dump -- collection before mapping
identityTable~mapR("sign")~dump
identityTable~dump -- collection after mapping (impacted by mapping
-- --------------------------------------------------------------
-- Mapping a string with a message
-- --------------------------------------------------------------
"abcdefghijklmnopqrstuvwxyz"~mapC("c2x")~dump
"The quick brown fox jumps over the lazy dog"~mapW("length")~dump
-- --------------------------------------------------------------
-- Mapping a mutable buffer with a message
-- --------------------------------------------------------------
-- Not-in-place mapping (map the characters)
buffer = .MutableBuffer~new("abcdefghijklmnopqrstuvwxyz")
buffer~dump -- mutable buffer before mapping
buffer~mapC("c2x")~dump
buffer~dump -- mutable buffer after mapping
-- Not-in-place mapping (map the words)
buffer = .MutableBuffer~new("The quick brown fox jumps over the lazy dog")
buffer~dump -- mutable buffer before mapping
buffer~mapW("length")~dump
buffer~dump -- mutable buffer after mapping
-- In place mapping (Replace the characters)
buffer = .MutableBuffer~new("abcdefghijklmnopqrstuvwxyz")
buffer~dump -- mutable buffer before mapping
buffer~mapCR("c2x")~dump
buffer~dump -- mutable buffer after mapping
-- In place mapping (Replace the words)
buffer = .MutableBuffer~new("The quick brown fox jumps over the lazy dog")
buffer~dump -- mutable buffer before mapping
buffer~mapWR("length")~dump
buffer~dump -- mutable buffer after mapping
-- --------------------------------------------------------------
-- Reducing a collection with a routine
-- --------------------------------------------------------------
-- A literal source is a routine source by default
-- Reduce :
-- arg(1) - accu : accumulated result
-- arg(2) - item : current item of collection
-- arg(3) - index : current index of collection (passed if the doer has the ~functionDoer method)
-- The source literal is transformed by ~reduce before creating an executable,
-- which lets use an implicit return.
-- See method ~functionDoer in doers.cls for more details.
-- You can see the transformed source by running that from ooRexxShell :
-- {accu + item + index}~functionDoer("use arg accu, item, index")~source=
-- The output is :
-- # 1: index=[1] -> item=[use arg accu, item, index ; options "NOCOMMANDS" ; accu + item + index]
-- # 2: index=[2] -> item=[ ; if var("result") then return result]
.Array~of(10, 20, 30)~reduce{accu + item + index}~dump -- returns 10 + 20+2 + 30+3 = 65
.Array~of(10, 20, 30)~reduce(0){accu + item + index}~dump -- returns 0 + 10+1 + 20+2 + 30+3 = 66
-- Remember ! In a bag, the index and the item have the same value
.Bag~of(10 ,20 ,30)~reduce{accu + item + index}~dump
.Bag~of(10 ,20 ,30)~reduce(0){accu + item + index}~dump
.CircularQueue~of(10, 20, 30)~reduce{accu + item + index}~dump
.CircularQueue~of(10, 20, 30)~reduce(0){accu + item + index}~dump
-- Here the index is not a number, hence the (arbitrary) use of ~c2d to derive a number from the index
.Directory~new~~put(10, "v1")~~put(20, "v2")~~put(30, "v3")~reduce{accu + item + index~c2d}~dump
.Directory~new~~put(10, "v1")~~put(20, "v2")~~put(30, "v3")~reduce(0){accu + item + index~c2d}~dump
-- Special case ! The index of a list starts at 0...
.List~of(10, 20, 30)~reduce{accu + item + index}~dump
.List~of(10, 20, 30)~reduce(0){accu + item + index}~dump
-- Here the index is not a number, hence the (arbitrary) use of ~c2d to derive a number from the index
.Properties~new~~put(10, "v1")~~put(20, "v2")~~put(30, "v3")~reduce{accu + item + index~c2d}~dump
.Properties~new~~put(10, "v1")~~put(20, "v2")~~put(30, "v3")~reduce(0){accu + item + index~c2d}~dump
.Queue~of(10, 20, 30)~reduce{accu + item + index}~dump
.Queue~of(10, 20, 30)~reduce(0){accu + item + index}~dump
-- Here the index is not a number, hence the (arbitrary) use of ~c2d to derive a number from the index
.Relation~new~~put(10, "v1")~~put(20, "v2")~~put(30, "v3")~reduce{accu + item + index~c2d}~dump
.Relation~new~~put(10, "v1")~~put(20, "v2")~~put(30, "v3")~reduce(0){accu + item + index~c2d}~dump
-- A relation which has more than one item per index is supported
-- Here the index is not a number, hence the (arbitrary) use of ~c2d to derive a number from the index
.Relation~new~~put(10, "v1")~~put(20, "v1")~~put(30, "v3")~reduce{accu + item + index~c2d}~dump
.Relation~new~~put(10, "v1")~~put(20, "v1")~~put(30, "v3")~reduce(0){accu + item + index~c2d}~dump
-- Remember ! In a set, the index and the item have the same value
.Set~of(10 ,20 ,30)~reduce{accu + item + index}~dump
.Set~of(10 ,20 ,30)~reduce(0){accu + item + index}~dump
-- Here the index is not a number, hence the (arbitrary) use of ~c2d to derive a number from the index
.Stem~new~~put(10, "v1")~~put(20, "v2")~~put(30, "v3")~reduce{accu + item + index~c2d}~dump
.Stem~new~~put(10, "v1")~~put(20, "v2")~~put(30, "v3")~reduce(0){accu + item + index~c2d}~dump
-- Here the index is not a number, hence the (arbitrary) use of ~c2d to derive a number from the index
.Table~new~~put(10, "v1")~~put(20, "v2")~~put(30, "v3")~reduce{accu + item + index~c2d}~dump
.Table~new~~put(10, "v1")~~put(20, "v2")~~put(30, "v3")~reduce(0){accu + item + index~c2d}~dump
-- Here the index is not a number, hence the (arbitrary) use of ~c2d to derive a number from the index
.IdentityTable~new~~put(10, "v1")~~put(20, "v2")~~put(30, "v3")~reduce{accu + item + index~c2d}~dump
.IdentityTable~new~~put(10, "v1")~~put(20, "v2")~~put(30, "v3")~reduce(0){accu + item + index~c2d}~dump
-- --------------------------------------------------------------
-- Reducing a string with a routine
-- --------------------------------------------------------------
-- initial value is the first char (default), index passed as 3rd argument, returns 1 + 2+2 + 3+3 = 11
123~reduceC{accu + item + index}~dump
-- initial value is 0, index passed as 3rd argument, returns 0 + 1+1 + 2+2 + 3+3 = 12
123~reduceC(0){accu + item + index}~dump
-- initial value is the first word (default), index passed as 3rd argument, returns 10 + 20+2 + 30+3 = 65
"10 20 30"~reduceW{accu + item + index}~dump
-- initial value is 0, index passed as 3rd argument, returns 0 + 10+1 + 20+2 + 30+3 = 66
"10 20 30"~reduceW(0){accu + item + index}~dump
-- --------------------------------------------------------------
-- Reducing a mutable buffer with a routine
-- --------------------------------------------------------------
-- initial value is the first char (default), index passed as 3rd argument, returns 1 + 2+2 + 3+3 = 11
.MutableBuffer~new(123)~reduceC{accu + item + index}~dump
-- initial value is 0, index passed as 3rd argument, returns 0 + 1+1 + 2+2 + 3+3 = 12
.MutableBuffer~new(123)~reduceC(0){accu + item + index}~dump
-- initial value is the first word (default), index passed as 3rd argument, returns 10 + 20+2 + 30+3 = 65
.MutableBuffer~new("10 20 30")~reduceW{accu + item + index}~dump
-- initial value is 0, index passed as 3rd argument, returns 0 + 10+1 + 20+2 + 30+3 = 66
.MutableBuffer~new("10 20 30")~reduceW(0){accu + item + index}~dump
-- --------------------------------------------------------------
-- Reducing a coactivity with a routine
-- --------------------------------------------------------------
-- initial value is the first yielded item (default)
{::coactivity .yield[10]; .yield[20]; .yield[30]}~doer~reduce{accu + item + index}~dump
-- initial value is 0
{::coactivity .yield[10]; .yield[20]; .yield[30]}~doer~reduce(0){accu + item + index}~dump
-- --------------------------------------------------------------
-- Mapping a collection with a routine
-- --------------------------------------------------------------
-- Map :
-- arg(1) - item : current item of collection
-- arg(2) - index : current index of collection (passed if the action has the ~functionDoer method)
.Array~of(1,2,3,4)~map{item * 2}~dump
.Array~of(1,2,3,4)~map{use arg n ; if n == 0 then 1 ; else n * .context~executable~call(n - 1)}~dump
.List~of(1,2,3,4)~map{item * 2}~dump
.List~of(1,2,3,4)~map{use arg n ; if n == 0 then 1 ; else n * .context~executable~call(n - 1)}~dump
.Queue~of(1,2,3,4)~map{item * 2}~dump
.Queue~of(1,2,3,4)~map{use arg n ; if n == 0 then 1 ; else n * .context~executable~call(n - 1)}~dump
.CircularQueue~of(1,2,3,4)~map{item * 2}~dump
.CircularQueue~of(1,2,3,4)~map{use arg n ; if n == 0 then 1 ; else n * .context~executable~call(n - 1)}~dump
-- Filtering is not possible on collections using map (to keep the indexes unchanged).
-- When no value is returned by ~map, then the current item is kept unchanged in the collection.
.array~of("one", "two", "three")~map{if item~length == 3 then item}~dump
-- A source can be tagged explicitely as a routine (but you don't need that, because it's the default)
.Array~of(1,2,3,4)~map{::routine item * 2}~dump
-- The shortest abbreviation of "::routine" is ":"
.Array~of(1,2,3,4)~map{: use arg n ; if n == 0 then 1 ; else n * .context~executable~call(n - 1)}~dump
-- A routine object can be used directly.
-- In this case, there is no source transformation.
.Array~of(1,2,3,4)~map(.context~package~findRoutine("factorial"))~dump
-- --------------------------------------------------------------
-- Mapping a string with a routine
-- --------------------------------------------------------------
"abcdefghijklmnopqrstuvwxyz"~mapC{item~verify('aeiouy')}~dump
-- Filtering (if no result returned by the doer, then nothing appended)
"abcdefghijklmnopqrstuvwxyz"~mapC{if item~verify('aeiouy') then item}~dump
"one two three"~mapW{if item~length == 3 then item}~dump
-- Reminder : index passed as 2nd argument
"one two three"~mapW{index":"item}~dump
-- --------------------------------------------------------------
-- Mapping a mutable buffer with a routine
-- --------------------------------------------------------------
-- Looks like a filtering, but it's not : a value is returned for each character (can be empty string)
.MutableBuffer~new("abcdefghijklmnopqrstuvwxyz")~mapC{if item~verify('aeiouy') == 1 then item ; else ''}~dump
.MutableBuffer~new("abcdefghijklmnopqrstuvwxyz")~mapC{if item~verify('aeiouy') == 1 then item}~dump
translation = .Directory~of("quick", "slow", "lazy", "nervous", "brown", "yellow", "dog", "cat")
translation~setMethod("UNKNOWN", "return arg(1)")
"The quick brown fox jumps over the lazy dog"~mapW{::closure expose translation ; translation[arg(1)]}~dump
.MutableBuffer~new("The quick brown fox jumps over the lazy dog")~mapW{::closure expose translation ; translation[arg(1)]}~dump
-- --------------------------------------------------------------
-- Repeating & collecting with a routine
-- --------------------------------------------------------------
-- No resulting array because no result returned during the iteration.
3~times{say 2*item}
-- ~times can act as an array collector : it collects the values returned by the doer.
3~times{2*item}~dump
3~times{0}~dump
-- returns .array~of(1,2,3) because the default action is {item}
3~times~dump
-- No resulting array because no result returned during the iteration.
11~upto(13){say 2*item}
-- ~upto can act as an array collector : it collects the values returned by the doer.
11~upto(13){2*item}~dump
11~upto(13){0}~dump
-- returns .array~of(11,12,13) because the default action is {item}
11~upto(13)~dump
-- Note that -1 MUST be surrounded by paren or quotes
(-1)~upto(3)~dump
-- No resulting array because no result returned during the iteration
13~downto(11){say 2*item}
-- ~downto can act as an array collector : it collects the values returned by the doer.
13~downto(11){2*item}~dump
-- returns .array~of(13,12,11) because the default action is {item}
13~downto(11)~dump
3~downto(-1)~dump
-- --------------------------------------------------------------
-- Iterating over a collection & collecting with a routine
-- --------------------------------------------------------------
-- each :
-- The values returned by the action are collected in an array
.Array~of(1,2,3)~each{2*item}~dump
.Array~of(1,2,3)~each{2*item + index}~dump
.Array~of(1,2,3)~eachI{2*item + index}~dump
.Bag~of(1,2,3)~each{2*item}~dump
.Bag~of(1,2,3)~each{2*item + index}~dump
.Bag~of(1,2,3)~eachI{2*item + index}~dump
.CircularQueue~of(1,2,3)~each{2*item}~dump
.CircularQueue~of(1,2,3)~each{2*item + index}~dump
.CircularQueue~of(1,2,3)~eachI{2*item + index}~dump
.Directory~new~~put(1, "v1")~~put(2, "v2")~~put(3, "v3")~each{2*item}~dump
-- Here the index is not a number, hence the (arbitrary) use of ~c2d to derive a number from the index
.Directory~new~~put(1, "v1")~~put(2, "v2")~~put(3, "v3")~each{2*item + index~c2d}~dump
.Directory~new~~put(1, "v1")~~put(2, "v2")~~put(3, "v3")~eachI{2*item + index~c2d}~dump
.List~of(1,2,3)~each{2*item}~dump
.List~of(1,2,3)~each{2*item + index}~dump
.List~of(1,2,3)~eachI{2*item + index}~dump
.Properties~new~~put(1, "v1")~~put(2, "v2")~~put(3, "v3")~each{2*item}~dump
-- Here the index is not a number, hence the (arbitrary) use of ~c2d to derive a number from the index
.Properties~new~~put(1, "v1")~~put(2, "v2")~~put(3, "v3")~each{2*item + index~c2d}~dump
.Properties~new~~put(1, "v1")~~put(2, "v2")~~put(3, "v3")~eachI{2*item + index~c2d}~dump
.Queue~of(1,2,3)~each{2*item}~dump
.Queue~of(1,2,3)~each{2*item + index}~dump
.Queue~of(1,2,3)~eachI{2*item + index}~dump
.Relation~new~~put(1, "v1")~~put(2, "v2")~~put(3, "v3")~each{2*item}~dump
-- Here the index is not a number, hence the (arbitrary) use of ~c2d to derive a number from the index
.Relation~new~~put(1, "v1")~~put(2, "v2")~~put(3, "v3")~each{2*item + index~c2d}~dump
.Relation~new~~put(1, "v1")~~put(2, "v2")~~put(3, "v3")~eachI{2*item + index~c2d}~dump
.Set~of(1,2,3)~each{2*item}~dump
.Set~of(1,2,3)~each{2*item + index}~dump
.Set~of(1,2,3)~eachI{2*item + index}~dump
.Stem~new~~put(1, "v1")~~put(2, "v2")~~put(3, "v3")~each{2*item}~dump
-- Here the index is not a number, hence the (arbitrary) use of ~c2d to derive a number from the index
.Stem~new~~put(1, "v1")~~put(2, "v2")~~put(3, "v3")~each{2*item + index~c2d}~dump
.Stem~new~~put(1, "v1")~~put(2, "v2")~~put(3, "v3")~eachI{2*item + index~c2d}~dump
.Table~new~~put(1, "v1")~~put(2, "v2")~~put(3, "v3")~each{2*item}~dump
-- Here the index is not a number, hence the (arbitrary) use of ~c2d to derive a number from the index
.Table~new~~put(1, "v1")~~put(2, "v2")~~put(3, "v3")~each{2*item + index~c2d}~dump
.Table~new~~put(1, "v1")~~put(2, "v2")~~put(3, "v3")~eachI{2*item + index~c2d}~dump
.IdentityTable~new~~put(1, "v1")~~put(2, "v2")~~put(3, "v3")~each{2*item}~dump
-- Here the index is not a number, hence the (arbitrary) use of ~c2d to derive a number from the index
.IdentityTable~new~~put(1, "v1")~~put(2, "v2")~~put(3, "v3")~each{2*item + index~c2d}~dump
.IdentityTable~new~~put(1, "v1")~~put(2, "v2")~~put(3, "v3")~eachI{2*item + index~c2d}~dump
-- --------------------------------------------------------------
-- Iterating over a supplier & collecting with a routine
-- --------------------------------------------------------------
-- supplier : the collection generated by ~each is always an array
.set~of(1,2,3)~supplier~each{2*item}~dump
-- --------------------------------------------------------------
-- Iterating over a string & collecting with a routine
-- --------------------------------------------------------------
-- string : the collection generated by ~each is always an array
"abcdef"~eachC{item}~dump
"abcdef"~eachC{".."~copies(index)item}~dump
"abcdef"~eachCI{".."~copies(index)item}~dump
"The quick brown fox"~eachW{item}~dump
"The quick brown fox"~eachW{".."~copies(index)item}~dump
"The quick brown fox"~eachWI{".."~copies(index)item}~dump
-- --------------------------------------------------------------
-- Iterating over a mutable buffer & collecting with a routine
-- --------------------------------------------------------------
-- mutable buffer : the collection generated by ~each is always an array
.MutableBuffer~new("abcdef")~eachC{item}~dump
.MutableBuffer~new("abcdef")~eachC{".."~copies(index)item}~dump
.MutableBuffer~new("abcdef")~eachCI{".."~copies(index)item}~dump
.MutableBuffer~new("The quick brown fox")~eachW{item}~dump
.MutableBuffer~new("The quick brown fox")~eachW{".."~copies(index)item}~dump
.MutableBuffer~new("The quick brown fox")~eachWI{".."~copies(index)item}~dump
-- --------------------------------------------------------------
-- Iterating over a coactivity & collecting with a routine
-- --------------------------------------------------------------
-- coactivity : ~makeArray is not a lazy method : it collects ALL the yielded values and return an array
{::coactivity do i=1 to 3; .yield[i]; end}~doer~each{2*item}~makeArray~dump
{::coactivity do i=1 to 3; .yield[i]; end}~doer~each{2*item + index}~makeArray~dump
{::coactivity do i=1 to 3; .yield[i]; end}~doer~eachI{2*item + index}~makeArray~dump
-- --------------------------------------------------------------
-- Iterating over a coactivity & generating with a routine
-- --------------------------------------------------------------
-- coactivity : each is a lazy method which returns another coactivity, not an array
-- Note : each example ends with c~do, to end the coactivities. This is not mandatory.
-- If you put in comment these c~do, then you will see 6 pending coactivities at the end of the output :
-- each example creates 2 coactivities : the first one by ~doer, the second one by ~each.
c = {::coactivity do i=1 to 3; .yield[i]; end}~doer~each{2*item}
c~dump
c~do~dump
c~do~dump
c~do~dump
c~do -- optional, this is to end the chain of coactivities
c = {::coactivity do i=1 to 3; .yield[i]; end}~doer~each{2*item + index}
c~dump
c~do~dump
c~do~dump
c~do~dump
c~do -- optional, this is to end the chain of coactivities
c = {::coactivity do i=1 to 3; .yield[i]; end}~doer~eachI{2*item + index}
c~dump
c~do~dump
c~do~dump
c~do~dump
c~do -- optional, this is to end the chain of coactivities
-- --------------------------------------------------------------
-- Mapping with a method
-- --------------------------------------------------------------
colors = .Array~of( ,
.Color~new("black", "000000") ,,
.Color~new("blue", "0000FF") ,,
.Color~new("green", "008000") ,,
.Color~new("grey", "BEBEBE") ,
)
-- A source can be tagged explicitely as a method (you need that, because it's a routine by default)
colors~map{::method self~rgbInteger "("self~redIntensity", "self~greenIntensity", "self~blueIntensity")"}~dump
-- A method object can be used directly
-- No need to define the method on the receiver class...
colors~map(.methods~entry("decimalColor"))~dump
-- ... except when the method is recursive and recalls itself by name
.String~define("factorial", .methods~entry("factorial"))
.Array~of(1,2,3,4)~map(.methods~entry("factorial"))~dump
-- Here, the method is recursive, but does not recall itself by name
.Array~of(1,2,3,4)~map(.methods~entry("factorialExecutable"))~dump
-----------------------------------------------------------------
-- Definitions
-----------------------------------------------------------------
::routine factorial
use strict arg n
if n == 0 then return 1
return n * factorial(n - 1) -- here, the routine 'factorial' has a global name, and can be called
::method factorial
if self == 0 then return 1
return self * (self - 1)~factorial -- this code will work only if self understands 'factorial'
::method factorialExecutable
if self == 0 then return 1
return self * (self - 1)~run(.context~executable) -- recursive call, but not by name
::method decimalColor
return self~rgbInteger "("self~redIntensity", "self~greenIntensity", "self~blueIntensity")"
::class Color
::attribute name
::attribute rgb
::attribute rgbInteger private
::method init
use strict arg name, rgb
self~name = name
self~rgb = rgb
self~rgbInteger = rgb~x2d
::method redIntensity
return self~rgb~substr(1,2)~x2d
::method greenIntensity
return self~rgb~substr(3,2)~x2d
::method blueIntensity
return self~rgb~substr(5,2)~x2d
-----------------------------------------------------------------
-- Helpers
-----------------------------------------------------------------
::extension Directory
::method of class
use strict arg key, value, ...
directory = .Directory~new
do i = 1 to arg() by 2
directory[arg(i)] = arg(i+1)
end
return directory
::extension Object
::method dump
say '['self~string']'
::extension String
::method dump
valstr = self~string
isnum = self~dataType("N")
if \isnum then valstr = '"'valstr'"' -- strings are surrounded by quotes, except string numbers
say '['valstr']'
::extension MutableBuffer
-- Unlike the routine pp2, this method does not display the identityHash
-- (to avoid false differences when comparing with previous output)
::method dump
self~string~dump
::extension Collection
::method dump
call dump self
::extension Supplier
::method dump
call dump self
-----------------------------------------------------------------
::routine dump
use arg coll, indent=""
say indent"["coll~class~id":"
s=coll~supplier
do while s~available
.output~charout(indent layout(s~index)~left(3)" : ")
if s~item~isA(.Collection) then do
say
call dump s~item, " "indent
end
else say layout(s~item)
s~next
end
say indent"]"
::routine layout
use strict arg obj
if \obj~isA(.String) then return obj~string
if \obj~dataType("N") then return obj
if obj < 0 then return obj
return " "obj
-----------------------------------------------------------------
::routine evaluate
use strict arg evaluate_routineName
evaluate_routine = .context~package~findRoutine(evaluate_routineName)
evaluate_routineSource = evaluate_routine~source
evaluate_curly_bracket_count = 0
evaluate_string = ""
evaluate_clause_separator = ""
evaluate_supplier = evaluate_routineSource~supplier
loop:
if \ evaluate_supplier~available then return
evaluate_sourceline = evaluate_supplier~item
if evaluate_sourceline~strip~left(3) == "---" then nop -- Comments starting with 3 '-' are removed
else if evaluate_sourceline~strip == "nop" then nop -- nop is a workaround to get the first comments
else if evaluate_sourceline~strip~left(2) == "--" then say evaluate_sourceline -- Comments starting with 2 '-' are kept
else if evaluate_sourceline~strip == "" then say
else do
say " "evaluate_sourceline
evaluate_curly_bracket_count += evaluate_sourceline~countStr("{") - evaluate_sourceline~countStr("}")
if ",-"~pos(evaluate_sourceline~right(1)) <> 0 then do
evaluate_string ||= evaluate_clause_separator || evaluate_sourceline~left(evaluate_sourceline~length - 1)
evaluate_clause_separator = ""
end
else if evaluate_curly_bracket_count > 0 then do
evaluate_string ||= evaluate_clause_separator || evaluate_sourceline
evaluate_clause_separator = "; "
end
else if evaluate_curly_bracket_count == 0 then do
evaluate_string ||= evaluate_clause_separator || evaluate_sourceline
evaluate_clause_separator = ""
signal on syntax
interpret evaluate_string
evaluate_string = ""
end
end
iterate:
evaluate_supplier~next
signal loop
syntax:
say "*** got an error :" condition("O")~message
evaluate_string = ""
signal iterate
-----------------------------------------------------------------
::requires "extension/extensions.cls"

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks