+ - 1 +-- 11 lines: --
1
| 2
| 3
| 4
| 5
| 6
| 7
| 8
| 9
| 10
| 11
12
13 :__prld__ __defmodule__[
14
15 TODO
16
17
18
19
20
21 ...
22
23
24
25
26 ...
27
28
29
30
31
32
33
+ - 34 +-- 68 lines: -- Aliases for Primitives --
34
| 35
| 36 :def '__def__ __def__
| 37
| 38 :call '__call__ def
| 39 :apply '__apply__ def
| 40 :apply-dict '__apply-dict__ def
| 41
| 42 :if '__if__ def
| 43
| 44 :defmulti '__defmulti__ def
| 45 :defrecord '__defrecord__ def
| 46
| 47 :=> '__=>__ def
| 48 :dict '__dict__ def
| 49
| 50 :puts! '__puts!__ def
| 51 :ask! '__ask!__ def
| 52
| 53 :type '__type__ def
| 54 :callable? '__callable?__ def
| 55 :function? '__function?__ def
| 56
| 57 :defmodule '__defmodule__ def
| 58 :import '__import__ def
| 59 :import-from '__import-from__ def
| 60
| 61 := '__=__ def
| 62 :not= '__not=__ def
| 63 :< '__<__ def
| 64 :<= '__<=__ def
| 65 :> '__>__ def
| 66 :>= '__>=__ def
| 67 :<=> '__<=>__ def
| 68
| 69 :eq '__eq__ def
| 70 :neq '__neq__ def
| 71 :lt '__lt__ def
| 72 :lte '__lte__ def
| 73 :gt '__gt__ def
| 74 :gte '__gte__ def
| 75 :cmp '__cmp__ def
| 76
| 77 :abs '__abs__ def
| 78 :trunc '__trunc__ def
| 79 :round '__round__ def
| 80 :ceil '__ceil__ def
| 81 :floor '__floor__ def
| 82
| 83 :int->float '__int->float__ def
| 84 :record->dict '__record->dict__ def
| 85
| 86 :record-type '__record-type__ def
| 87 :record-vals '__record-values__ def
| 88 :record-values '__record-values__ def
| 89 :record-type-name '__record-type-name__ def
| 90 :record-type-fields '__record-type-fields__ def
| 91
| 92 :fail '__fail__ def
| 93 :try '__try__ def
| 94
| 95 :rx-match '__rx-match__ def
| 96 :rx-sub '__rx-sub__ def
| 97
| 98 :par '__par__ def
| 99 :sleep '__sleep__ def
| 100
| 101
102
+ - 103 +--137 lines: -- Stack Shuffling --
103
| 104
| 105
| 106
| 107 >>> , 1 2 s!
| 108
| 109
| 110
| 111
| 112 >>> , swap s!
| 113
| 114
| 115
| 116
| 117
| 118 :swap [ x y . 'y 'x ] def
| 119 :>< 'swap def
| 120
| 121
| 122
| 123 >>> , 1 2 3 rot> s!
| 124
| 125
| 126
| 127
| 128
| 129 >>> , <rot s!
| 130
| 131
| 132
| 133
| 134
| 135
| 136 :rot> [ x y z . 'z 'x 'y ] def
| 137 :<rot [ x y z . 'y 'z 'x ] def
| 138
| 139
| 140
| 141 >>> , 1 2 3 4 rot4> s!
| 142
| 143
| 144
| 145
| 146
| 147
| 148 >>> , <rot4 s!
| 149
| 150
| 151
| 152
| 153
| 154
| 155
| 156 :rot4> [ w x y z . 'z 'w 'x 'y ] def
| 157 :<rot4 [ w x y z . 'x 'y 'z 'w ] def
| 158
| 159
| 160
| 161 >>> , 42 dup s!
| 162
| 163
| 164
| 165
| 166 >>> c!
| 167
| 168 >>> , 1 2 2dup s!
| 169
| 170
| 171
| 172
| 173
| 174
| 175 >>> ( 1 2 3 3dup )
| 176
| 177
| 178 :dup [ x . 'x 'x ] def
| 179 :2dup [ x y . 'x 'y 'x 'y ] def
| 180 :3dup [ 2over over2 ] def
| 181
| 182
| 183
| 184 >>> nil
| 185
| 186 >>> drop
| 187 >>> drop
| 188
| 189 >>> 42 37 2drop
| 190 >>> 1 2 3 3drop
| 191
| 192 :drop [ _ . ] def
| 193 :2drop [ _ _ . ] def
| 194 :3drop [ _ _ _ . ] def
| 195
| 196
| 197
| 198 >>> , 42 37 nip s!
| 199
| 200
| 201
| 202
| 203 :nip [ _ y . 'y ] def
| 204
| 205
| 206
| 207 >>> , 1 2 over s!
| 208
| 209
| 210
| 211
| 212
| 213 >>> c!
| 214
| 215 >>> , 1 2 3 2over s!
| 216
| 217
| 218
| 219
| 220
| 221
| 222
| 223
| 224 :over [ x y . 'x 'y 'x ] def
| 225 :2over [ x y z . 'x 'y 'z 'x 'y ] def
| 226
| 227
| 228
| 229 >>> , 1 2 3 over2 s!
| 230
| 231
| 232
| 233
| 234
| 235
| 236
| 237 :over2 [ x y z . 'x 'y 'z 'x ] def
| 238
| 239
240
+ - 241 +--285 lines: -- Combinators --
241
| 242
| 243
| 244
| 245 >>> , 1 '+ $
| 246 >>> 2 swap call
| 247
| 248 >>> 2, 3 4 [ + * ] 2$, call
| 249
| 250 >>> 1, 2 3 4 [ + + + ] 3$, call
| 251
| 252 >>> :foo [] $ call
| 253
| 254
| 255 >>> , [ 1 + ] [ 3 * ] @
| 256 >>> 2 swap call
| 257
| 258 >>> [ 2 * ] [ 1 + ] % call
| 259
| 260
| 261 >>> , 2 '-
| 262 >>> , 1 $$
| 263 >>> call
| 264
| 265 >>> 2 [ + * ], 3 4 2$$, call
| 266
| 267 >>> 1 [ + + + ], 2 3 4 3$$, call
| 268
| 269
| 270 :$ '[ '1 .2 ] def
| 271 :2$ '[ '1 '2 .3 ] def TODO
| 272 :3$ '[ '1 '2 '3 .4 ] def
| 273
| 274 :@ '[ .1 .2 ] def
| 275 :% '[ .2 .1 ] def
| 276
| 277 :$$ '[ f . '1 f ] def
| 278 :2$$ '[ f . '1 '2 f ] def
| 279 :3$$ '[ f . '1 '2 '3 f ] def
| 280
| 281
| 282
| 283 >>> , 1 2 'dup dip s!
| 284
| 285
| 286
| 287
| 288
| 289 >>> c!
| 290
| 291 >>> , 1 2 3 4 '- 2dip s!
| 292
| 293
| 294
| 295
| 296
| 297 >>> c!
| 298
| 299 >>> , 1 2 3 4 'neg 3dip s!
| 300
| 301
| 302
| 303
| 304
| 305
| 306
| 307 :dip [ x f . f 'x ] def
| 308 :2dip [ x y f . f 'x 'y ] def
| 309 :3dip [ '2dip $ dip ] def
| 310
| 311
| 312
| 313 >>> , 2 [ dup * ] keep s!
| 314
| 315
| 316
| 317
| 318 >>> c!
| 319
| 320 >>> , 2 3 '* 2keep s!
| 321
| 322
| 323
| 324
| 325
| 326
| 327 :keep [ over 'call dip ] def
| 328 :2keep [ 2over 'call 2dip ] def
| 329
| 330
| 331
| 332 >>> , 35 [ 2 + ] [ 7 + ] bi s!
| 333
| 334
| 335
| 336
| 337 >>> c!
| 338
| 339 >>> , 2 [ 1 + ] [ 2 * ] [ 3 - ] tri s!
| 340
| 341
| 342
| 343
| 344
| 345
| 346 >>> ( 2 ( [ 1 + ] [ 2 * ] [ 3 - ] ) mlt )
| 347
| 348
| 349
| 350 >>> 42 'num? 'pos? bi-and
| 351
| 352 >>> -1 'num? 'pos? bi-and
| 353
| 354 >>> nil 'num? 'pos? bi-and
| 355
| 356 >>> nil 'num? 'pos? bi and
| 357
| 358 >>> nil 'nil? 'pos? bi-or
| 359
| 360 >>> nil 'nil? 'pos? bi or
| 361
| 362
| 363 :bi [ x f g . 'x f 'x g ] def
| 364 :tri [ x f g h . 'x f 'x g 'x h ] def
| 365
| 366 :mlt [ swap $$ each ] def
| 367
| 368 :bi-and [ x p1? p2? . [ 'x p1? ] [ 'x p2? ] and' ] def
| 369 :bi-or [ x p1? p2? . [ 'x p1? ] [ 'x p2? ] or' ] def
| 370
| 371
| 372
| 373 >>> 2 3 [ dup * ] bi$ +
| 374
| 375 >>> c!
| 376
| 377 >>> , 2 3 4 [ dup * ] tri$ s!
| 378
| 379
| 380
| 381
| 382
| 383
| 384 >>> ( ( 2 3 4 ) [ dup * ] mlt$ )
| 385
| 386
| 387
| 388 >>> ( 0 2 4 ) 'even? all?
| 389
| 390 >>> ( 37 42 ) 'even? any?
| 391
| 392
| 393
| 394 >>> -1 nil 'pos? bi$-and
| 395
| 396 >>> 42 nil 'pos? bi$-or
| 397
| 398 >>> 42 nil 'pos? bi$ or
| 399
| 400
| 401 :bi$ [ x y f . 'x f 'y f ] def
| 402 :tri$ [ x y z f . 'x f 'y f 'z f ] def
| 403
| 404 :mlt$ [ each ] def
| 405
| 406 :all? [ p? . [ #t ] [ >< p? dup [ drop 'p? all? ] 'nip if ] ^seq ] def
| 407 :any? [ p? . [ #f ] [ >< p? dup 'nip [ drop 'p? any? ] if ] ^seq ] def
| 408
| 409 :bi$-and [ x y p? . [ 'x p? ] [ 'y p? ] and' ] def
| 410 :bi$-or [ x y p? . [ 'x p? ] [ 'y p? ] or' ] def
| 411
| 412
| 413
| 414 >>> , 4 9 [ 2 + ] [ 3 div ] bi~ s!
| 415
| 416
| 417
| 418
| 419 >>> c!
| 420
| 421 >>> ( 1 2 3 :x :y :z '[ '1 swap => ] tri$ tri~ )
| 422
| 423
| 424 >>> ( ( 1 2 3 ) ( :x :y :z ) '[ '1 swap => ] map mlt~ )
| 425
| 426
| 427 :bi~ [ x y f g . 'x f 'y g ] def
| 428 :tri~ [ x y z f g h . 'x f 'y g 'z h ] def
| 429
| 430 :mlt~ [ 'call zip [] each ] def
| 431
| 432 TODO...
| 433
| 434
| 435
| 436 >>> , 2 3 [ dup * ] 'neg bi* s!
| 437
| 438
| 439
| 440
| 441
| 442
| 443
| 444 :bi* [ [ 'bi$ $ 2keep ] dip bi$ ] def
| 445
| 446
| 447
| 448 >>> , 1 2 '+ '- 2bi s!
| 449
| 450
| 451
| 452
| 453 >>> c!
| 454
| 455 >>> , 7 2 '+ '- 'div 2tri s!
| 456
| 457
| 458
| 459
| 460
| 461
| 462 :2bi [ '2keep dip call ] def
| 463 :2tri [ '2keep 2dip 2bi ] def
| 464
| 465
| 466
| 467 >>> , :x 1 :y 2 '=> 2bi$ s!
| 468
| 469
| 470
| 471
| 472 >>> c!
| 473
| 474 >>> , :x :y 1 2 '=> 2bi$' s!
| 475
| 476
| 477
| 478
| 479
| 480 :2bi$ [ dup 2bi~ ] def
| 481 :2bi$' [ 'swap 2dip 2bi$ ] def
| 482
| 483
| 484
| 485 >>> , 1 2 3 4 '+ '- 2bi~ s!
| 486
| 487
| 488
| 489
| 490 >>> c!
| 491
| 492 >>> , 1 3 2 4 '+ '- 2bi~' s!
| 493
| 494
| 495
| 496
| 497
| 498 :2bi~ [ '2dip dip call ] def
| 499 :2bi~' [ 'swap 3dip 2bi~ ] def
| 500
| 501
| 502
| 503 >>> 5 [ 37 + ] [ 37 - ] ~pos
| 504
| 505 >>> 5 37 '+ '- $bi ~pos
| 506
| 507
| 508 :$bi [ x f g . 'x 'f $ 'x 'g $ ] def
| 509
| 510
| 511
| 512 >>> 5 'neg [ 37 + ] ~neg
| 513
| 514 >>> 5 37 'neg '+ $snd ~neg
| 515
| 516
| 517 >>> 5 37 '+ 'neg '$ dip ~pos
| 518
| 519 >>> 5 37 '+ 'neg $fst ~pos
| 520
| 521
| 522 :$fst [ '$ dip ] def
| 523 :$snd [ x f g . 'f 'x 'g $ ] def
| 524
| 525
526
+ - 527 +--233 lines: -- Conditionals, Logic & Order --
527
| 528
| 529
| 530
| 531
| 532
| 533
| 534
| 535
| 536 >>> #t 42 37 ?
| 537
| 538
| 539 :? [ '[ '1 ] bi$ if ] def
| 540
| 541
| 542
| 543
| 544 >>> 1 2 = [ "oh no!" say! ] when
| 545 >>> 1 1 = [ "good!" say! ] when
| 546
| 547 >>> , 42 dup 2 mod 0 = [ 2 div ] when s!
| 548
| 549
| 550
| 551 >>> c!
| 552
| 553 >>> 1 2 2dup = '+ when1
| 554
| 555 >>> 2 2 2 2dup = [ + * ] when2
| 556
| 557
| 558 :when [ [] if ] def
| 559 :when1 [ 'drop if ] def
| 560 :when2 [ '2drop if ] def
| 561
| 562 ......
| 563
| 564 >>> 1 1 = [ "oh no!" say! ] unless
| 565 >>> 1 2 = [ "good!" say! ] unless
| 566
| 567 >>> 2 2 2dup = '+ unless1
| 568
| 569 >>> 1 2 3 2dup = [ + * ] unless2
| 570
| 571
| 572 :unless [ [] swap if ] def
| 573 :unless1 [ 'drop swap if ] def
| 574 :unless2 [ '2drop swap if ] def
| 575
| 576
| 577
| 578
| 579
| 580 >>> , :collatz [
| 581 ... [ [ 2 div ] [ 3 * 1 + ] 'even? ~? ]
| 582 ... iterate [ 1 not= ] take-while ( 1 ) ++
| 583 ... ] def
| 584 >>> 19 collatz ->list
| 585
| 586
| 587 :~? [ 'dup % 2dip if ] def
| 588
| 589
| 590
| 591
| 592
| 593
| 594
| 595
| 596
| 597
| 598 >>> 5 bool
| 599
| 600 >>> 5 not
| 601
| 602 >>> nil not
| 603
| 604 >>> nil 5 or
| 605
| 606 >>> nil 5 and
| 607
| 608 >>> 2 3 or
| 609
| 610 >>> 2 3 and
| 611
| 612
| 613 >>> [ nil ] [ "oops" fail ] and'
| 614
| 615 >>> [ 42 ] [ "oops" fail ] or'
| 616
| 617
| 618 :bool [ not not ] def
| 619 :not [ #f #t ? ] def
| 620 :and [ over ? ] def
| 621 :or [ 'dup dip ? ] def
| 622
| 623 :and' [ f g . f dup [ drop g ] [] if ] def
| 624 :or' [ f g . f dup [] [ drop g ] if ] def
| 625
| 626
| 627
| 628 >>> 41 'inc 'dec 41 ~[=]
| 629
| 630 >>> 41 'dec 'inc 41 ~[not=]
| 631
| 632 >>> 41 'inc 'dec 99 ~[<]
| 633
| 634 >>> 43 'inc 'dec 37 ~[<=]
| 635
| 636 >>> 41 'inc 'dec 37 ~[>]
| 637
| 638 >>> 41 'inc 'dec 41 ~[>=]
| 639
| 640
| 641 >>> 42 37 '- '* '+ ~<=>
| 642
| 643 >>> 37 [ :< ] [ := ] [ :> ] 42 ~[<=>] nip
| 644
| 645
| 646
| 647
| 648 :~[=] [ '= $ ~? ] def
| 649 :~[not=] [ 'not= $ ~? ] def
| 650 :~[<] [ '< $ ~? ] def
| 651 :~[<=] [ '<= $ ~? ] def
| 652 :~[>] [ '> $ ~? ] def
| 653 :~[>=] [ '>= $ ~? ] def
| 654
| 655 :~<=> [ f g h . 2dup <=> [ drop g ] [ 0 < 'f 'h if ] 0 ~[=] ] def
| 656 :~[<=>] [ [] $ 3dip [ 'drop % ] tri$ ~<=> ] def
| 657
| 658
| 659
| 660 >>> 41.0 'inc 'dec 41 ~[eq]
| 661
| 662 >>> 41.0 'dec 'inc 41 ~[neq]
| 663
| 664 >>> 41.0 'inc 'dec 99 ~[lt]
| 665
| 666 >>> 43.0 'inc 'dec 37 ~[lte]
| 667
| 668 >>> 41.0 'inc 'dec 37 ~[gt]
| 669
| 670 >>> 41.0 'inc 'dec 41 ~[gte]
| 671
| 672
| 673 >>> 42 37.0 '- '* '+ ~cmp
| 674
| 675 >>> 37 [ :< ] [ := ] [ :> ] 42.0 ~[cmp] nip
| 676
| 677
| 678
| 679
| 680 :~[eq] [ 'eq $ ~? ] def
| 681 :~[neq] [ 'neq $ ~? ] def
| 682 :~[lt] [ 'lt $ ~? ] def
| 683 :~[lte] [ 'lte $ ~? ] def
| 684 :~[gt] [ 'gt $ ~? ] def
| 685 :~[gte] [ 'gte $ ~? ] def
| 686
| 687 :~cmp [ f g h . 2dup cmp [ drop g ] [ neg? 'f 'h if ] 0 ~[eq] ] def
| 688 :~[cmp] [ [] $ 3dip [ 'drop % ] tri$ ~cmp ] def
| 689
| 690
| 691
| 692 >>> 1 2 min
| 693
| 694 >>> -1 -2 max
| 695
| 696
| 697 >>> 2 1.0 min
| 698
| 699 >>> 2 1.0 min'
| 700
| 701 >>> 2 1.0 max'
| 702
| 703
| 704 :min [ '<= min-by ] def
| 705 :max [ '>= max-by ] def
| 706
| 707 :min' [ 'lte min-by ] def
| 708 :max' [ 'gte max-by ] def
| 709
| 710 :min-by [ f . 2dup f 'drop 'nip if ] def
| 711 :max-by [ f . 2dup f 'drop 'nip if ] def
| 712
| 713
| 714
| 715
| 716
| 717
| 718
| 719
| 720
| 721 >>> , :temp [
| 722 ... [ show " is " ++ ]
| 723 ... [ ( [ 15 < ] "cold!" [ 25 > ] "warm!" :else "ok!" ) cond1 ]
| 724 ... bi ++ say!
| 725 ... ] def
| 726 >>> 10 temp
| 727
| 728 >>> 20 temp
| 729
| 730 >>> 30 temp
| 731
| 732
| 733 :cond1 [ _cond1 dup block? 'call when ] def TODO
| 734 :_cond1 [ cons '_&cond1 apply ] def
| 735 :_&cond1 [ x p? f & .
| 736 'x 'p? 'call 'nip 'function? ~? [ 'f ] [ 'x '& _cond1 ] if
| 737 ] def
| 738
| 739 TODO...
| 740
| 741
| 742
| 743 >>> -1 [ :pos ] [ :neg ] [ :zero ] ( '~pos '~neg ) ~>> nip
| 744
| 745
| 746 >>> , :~type [ .[ type '1 = ] ~? ] def
| 747 >>> , :~strint [ ( [ :str ~type ] [ :int ~type ] ) ~>> ] def
| 748 >>> , :f [ [ "bar" ++ ] [ 5 + ] [ drop :oops ] ~strint ] def
| 749 >>> "foo" f
| 750
| 751 >>> 37 f
| 752
| 753 >>> nil f
| 754
| 755
| 756 :~>> [ reverse _~>> ] def
| 757 :_~>> [ 'call [ f ft . .[ '1 '2 f ] 'ft _~>> ] ^seq ] def
| 758
| 759
760
+ - 761 +--109 lines: -- Arithmetic --
761
| 762
| 763
| 764
| 765
| 766
| 767
| 768
| 769
| 770 >>> 1 2 +
| 771
| 772 >>> 4 3 -
| 773
| 774 >>> 6 7 *
| 775
| 776 >>> 1.0 2.0 +
| 777
| 778 >>> 4.0 3 -
| 779
| 780 >>> 6 7.0 *
| 781
| 782
| 783 :+ ( :int :int ) [ __int+__ ] defmulti
| 784 :+ ( :float :float ) [ __float+__ ] defmulti
| 785 :+ ( :int :float ) [ 'int->float dip + ] defmulti
| 786 :+ ( :float :int ) [ int->float + ] defmulti
| 787
| 788 :- ( :int :int ) [ __int-__ ] defmulti
| 789 :- ( :float :float ) [ __float-__ ] defmulti
| 790 :- ( :int :float ) [ 'int->float dip - ] defmulti
| 791 :- ( :float :int ) [ int->float - ] defmulti
| 792
| 793 :* ( :int :int ) [ __int*__ ] defmulti
| 794 :* ( :float :float ) [ __float*__ ] defmulti
| 795 :* ( :int :float ) [ 'int->float dip * ] defmulti
| 796 :* ( :float :int ) [ int->float * ] defmulti
| 797
| 798
| 799
| 800 >>> 10 neg
| 801
| 802 >>> -10 neg
| 803
| 804 >>> 3.14 neg
| 805
| 806
| 807 :neg '__neg__ def
| 808
| 809
| 810
| 811 >>> 1.0 2.0 /
| 812
| 813 >>> 8 3 div
| 814
| 815 >>> 8 3 mod
| 816
| 817
| 818 >>> 8.0 3 floor/
| 819
| 820 >>> -8 3.0 floor/
| 821
| 822
| 823 :/ '__float/__ def
| 824 :div '__div__ def
| 825 :mod '__mod__ def
| 826
| 827 :floor/ ( :int :int ) [ div ] defmulti
| 828 :floor/ ( :float :float ) [ / floor ] defmulti
| 829 :floor/ ( :int :float ) [ 'int->float dip floor/ ] defmulti
| 830 :floor/ ( :float :int ) [ int->float floor/ ] defmulti
| 831
| 832
| 833
| 834 >>> 10 3 div?
| 835
| 836 >>> 42 7 div?
| 837
| 838 >>> ( 2 3 4 ) 'even? filterl
| 839
| 840 >>> ( 1 2 3 ) 'odd? filterl
| 841
| 842
| 843 >>> , ( -1 -1.1 0 0.0 1 1.1 )
| 844 >>> dup 'neg? filterl
| 845
| 846 >>> drop dup 'zero? filterl
| 847
| 848 >>> drop dup 'pos? filterl
| 849
| 850
| 851 :div? [ mod 0 = ] def
| 852 :even? [ 2 div? ] def
| 853 :odd? [ even? not ] def
| 854
| 855 :neg? [ 0 lt ] def
| 856 :zero? [ 0 eq ] def
| 857 :pos? [ 0 gt ] def
| 858
| 859
| 860
| 861 >>> 41 inc
| 862
| 863 >>> dec
| 864
| 865
| 866 :inc [ 1 + ] def
| 867 :dec [ 1 - ] def
| 868
| 869
870
+ - 871 +--121 lines: -- Strings & Characters --
871
| 872
| 873
| 874
| 875
| 876
| 877
| 878 >>> "猫" ord
| 879
| 880 >>> 0x732b chr
| 881
| 882
| 883 :ord '.ord def
| 884 :chr '__chr__ def
| 885
| 886
| 887
| 888 >>> ( "猫" "foo" "" 42 ) 'char? filterl
| 889
| 890
| 891 :char? [ 'str? [ len 1 = ] bi-and ] def
| 892
| 893
| 894
| 895 >>> 42 show
| 896
| 897 >>> 0x20 show
| 898
| 899 >>> "foo" show
| 900
| 901 >>> :foo show
| 902
| 903 >>> x: 42 show
| 904
| 905 >>> { x: [1-), y: ( 1 nil :x ) } show
| 906
| 907
| 908 :show ( :pair ) [ [ 'show bi$ " " ++sep++ " =>" ++ ] ^pair ] defmulti
| 909 :show ( :list ) [ [ "()" ] [ "( " " " " )" _showseq ] ~seq ] defmulti
| 910 :show ( :dict ) [ [ "{ }" ] [ "{ " ", " " }" _showseq ] ~seq ] defmulti
| 911 :show ( :_ ) [ '_showrec '__show__ 'record? ~? ] defmulti
| 912
| 913 :_showrec [ [ record-type record-type-name __show__ 1 [i-) ]
| 914 [ record->dict show ] bi ++ ] def
| 915 :_showseq [ b d a . 'show map 'b swap 'd join-with 'a ++ ++ ] def
| 916
| 917
| 918
| 919 >>> "foo" show
| 920
| 921 >>> "foo" ->str
| 922
| 923
| 924 >>> :foo show
| 925
| 926 >>> :foo ->str
| 927
| 928 >>> :foo kwd->str
| 929
| 930
| 931 >>> 42 ->str
| 932
| 933
| 934 :->str ( :str ) [ ] defmulti
| 935 :->str ( :_ ) [ show ] defmulti
| 936
| 937 :kwd->str [ show [ 2 -1 ] [ 1 nil ] [ "\"" ends-with? ] ~? [i-j) ] def
| 938
| 939
| 940
| 941 >>> "foobar" ->list
| 942
| 943 >>> join
| 944
| 945 >>> ( "Hello" "World" ) ", " join-with
| 946
| 947
| 948 >>> "foo" "bar" " & " ++sep++
| 949
| 950
| 951 :join [ "" join-with ] def TODO
| 952 :join-with [ s . [ "" ] [ >< [ 's ++sep++ ] foldl ] ^seq ] def
| 953 :++sep++ [ >< ++ ++ ] def
| 954
| 955
| 956
| 957 >>> "foo" upper-case
| 958
| 959 >>> "BAR" lower-case
| 960
| 961
| 962 :lower-case '.lower def
| 963 :upper-case '.upper def
| 964
| 965
| 966
| 967 >>> " foo " trim
| 968
| 969 >>> " foo " triml
| 970
| 971 >>> " foo " trimr
| 972
| 973
| 974 :trim '.trim def
| 975 :triml '.triml def
| 976 :trimr '.trimr def
| 977
| 978
| 979
| 980 >>> "foo" "f" starts-with?
| 981
| 982 >>> "bar" "ar" ends-with?
| 983
| 984
| 985 >>> "foobar" "oba" elem?
| 986
| 987
| 988 :starts-with? [ swap !starts-with? ] def
| 989 :ends-with? [ swap !ends-with? ] def
| 990
| 991
992
+ - 993 +--133 lines: -- Nil, Num, Pair & Tuples --
993
| 994
| 995
| 996
| 997 >>> , :f [ [ "nil!" say! ] [ type show say! ] ~nil ] def
| 998 >>> nil f
| 999
| 1000 >>> 42 f
| 1001
| 1002
| 1003 >>> ( 1 ) 'rest ~> 'first ~> [ 1 + ] ~>
| 1004
| 1005 >>> ( 1 2 ) 'rest ~> 'first ~> [ 1 + ] ~>
| 1006
| 1007 >>> ( 3 4 ) ( 'rest 'first [ 1 + ] ) ~~>
| 1008
| 1009
| 1010 :~nil [ [ 'drop % ] dip 'nil? ~? ] def
| 1011 :~> [ [ nil ] swap ~nil ] def
| 1012 :~~> [ [ .[ .1 '2 ~~> ] ~> ] ^seq' ] def
| 1013
| 1014
| 1015
| 1016 >>> ( "foo" "" 42 0 ( 1 2 3 ) () ) '->nil mapl
| 1017
| 1018
| 1019 :->nil ( :nil ) [ ] defmulti
| 1020 :->nil ( :bool ) [ #t nil ? ] defmulti
| 1021
| 1022 :->nil ( :int ) [ [ drop nil ] [] ~zero ] defmulti
| 1023 :->nil ( :_ ) [ [ nil ] [] ~seq ] defmulti
| 1024
| 1025
| 1026
| 1027 >>> 1 num?
| 1028
| 1029 >>> 3.14 num?
| 1030
| 1031 >>> () num?
| 1032
| 1033
| 1034 :num? [ 'int? 'float? bi-or ] def
| 1035
| 1036 TODO
| 1037
| 1038
| 1039
| 1040 >>> 0 [ " negative" ] [ " non-negative" ] ~neg 'show dip ++ say!
| 1041
| 1042 >>> 0 [ " zero" ] [ " non-zero" ] ~zero 'show dip ++ say!
| 1043
| 1044 >>> 0 [ " positive" ] [ " non-positive" ] ~pos 'show dip ++ say!
| 1045
| 1046
| 1047 >>> 4 [ :neg ] [ :zero ] [ :pos ] ~num nip
| 1048
| 1049
| 1050 :~neg [ 0 ~[lt] ] def
| 1051 :~zero [ 0 ~[eq] ] def
| 1052 :~pos [ 0 ~[gt] ] def
| 1053
| 1054 :~num [ 0 ~[cmp] ] def
| 1055
| 1056
| 1057
| 1058 >>> , x: 42 'swap ^pair, s!
| 1059
| 1060
| 1061
| 1062
| 1063 >>> c!
| 1064
| 1065 >>> , y: 37 'val 'key bi, s!
| 1066
| 1067
| 1068
| 1069
| 1070
| 1071 :^pair [ [ 'key 'val bi ] dip call ] def
| 1072
| 1073 :key '.key def
| 1074 :val '.value def
| 1075
| 1076
| 1077
| 1078 >>> 1 2 2T
| 1079
| 1080 >>> dup .1st
| 1081
| 1082 >>> drop .2nd
| 1083
| 1084
| 1085 >>> T( 1 2 3 4 5 )
| 1086
| 1087 >>> .5th
| 1088
| 1089
| 1090 :T() [ 0T ] def
| 1091 :T [ & . '& '_T '& len get^ apply ] def TODO
| 1092
| 1093 :0T ( ) defrecord
| 1094 :1T ( :1st ) defrecord
| 1095 :2T ( :1st :2nd ) defrecord
| 1096 :3T ( :1st :2nd :3rd ) defrecord
| 1097 :4T ( :1st :2nd :3rd :4th ) defrecord
| 1098 :5T ( :1st :2nd :3rd :4th :5th ) defrecord
| 1099
| 1100 :_T ( '0T '1T '2T '3T '4T '5T ) def
| 1101
| 1102 :show ( :0T ) [ tuple-show ] defmulti
| 1103 :show ( :1T ) [ tuple-show ] defmulti
| 1104 :show ( :2T ) [ tuple-show ] defmulti
| 1105 :show ( :3T ) [ tuple-show ] defmulti
| 1106 :show ( :4T ) [ tuple-show ] defmulti
| 1107 :show ( :5T ) [ tuple-show ] defmulti
| 1108
| 1109 :len ( :0T ) [ drop 0 ] defmulti
| 1110 :len ( :1T ) [ drop 1 ] defmulti
| 1111 :len ( :2T ) [ drop 2 ] defmulti
| 1112 :len ( :3T ) [ drop 3 ] defmulti
| 1113 :len ( :4T ) [ drop 4 ] defmulti
| 1114 :len ( :5T ) [ drop 5 ] defmulti
| 1115
| 1116 :->list ( :0T ) [ vals ] defmulti
| 1117 :->list ( :1T ) [ vals ] defmulti
| 1118 :->list ( :2T ) [ vals ] defmulti
| 1119 :->list ( :3T ) [ vals ] defmulti
| 1120 :->list ( :4T ) [ vals ] defmulti
| 1121 :->list ( :5T ) [ vals ] defmulti
| 1122
| 1123 :tuple-show [ vals show "T" swap ++ ] def
| 1124
| 1125
1126
+ - 1127 +--570 lines: -- Sequences, Lists & Ranges --
1127
| 1128
| 1129
| 1130 :Range ( :start :stop :step ) defrecord
| 1131
| 1132 :show ( :Range ) [ [ m n s . { f: [ show " " ++ ] } let[ (
| 1133 'm f
| 1134 'n [ "" "" ")" ] [ f "n" "]" ] ~nil
| 1135 's [ drop "" "" ] [ f ":s" ] 1 ~[eq]
| 1136 <rot [ "[m-" <rot ] 2dip
| 1137 ) ] join ] ^Range ] defmulti
| 1138
| 1139 :range [ 3dup 3list 'float? any? [ '_->float tri$ ] when
| 1140 2dup float? and [ [ 2.0 / + ] keep ] when Range ] def
| 1141
| 1142 :range-unseq [ [ nil nil ]
| 1143 [ [ dup [ over + ] $ 2dip Range ] ^Range ] ~seq ] def
| 1144 :range-empty? [ [ >< [ 2drop #f ] [ rot> _pos<> ] ~nil ] ^Range ] def
| 1145 :range-len [ [ >< [ 2drop nil ] [ rot> '- dip floor/ inc 0 max ]
| 1146 ~nil ] ^Range ] def
| 1147
| 1148 :range-get^' [ over 0 < [ 2drop nil ] [ [ i m n s .
| 1149 'm 's 'i * + 'n
| 1150 [] [ over 's _pos<> [ drop nil ] when ] ~nil
| 1151 ] ^Range ] if ] def
| 1152 :range-has?' [ range-get^' nil? not ] def
| 1153 :range-elem?' [ [ k m n s .
| 1154 'k 'm 's _pos<>, 'n nil? not, 'n 'k 's _pos<>,
| 1155 and or not
| 1156 'k 'm - 's,
| 1157 'k float? [ / dup trunc eq ] 'div? if and
| 1158 ] ^Range ] def TODO
| 1159
| 1160 :_->float [ dup int? 'int->float when ] def
| 1161 :_pos<> [ pos? '< '> if ] def
| 1162
| 1163
| 1164
| 1165 >>> 2 10 [m-n] ->list
| 1166
| 1167 >>> 2 10 [m-n) ->list
| 1168
| 1169 >>> 4 [m-) 10 take-first ->list
| 1170
| 1171 >>> 10 [0-n] ->list
| 1172
| 1173 >>> 10 [0-n) ->list
| 1174
| 1175 >>> [0-) 10 take-first ->list
| 1176
| 1177 >>> 10 [1-n] ->list
| 1178
| 1179 >>> 10 [1-n) ->list
| 1180
| 1181 >>> [1-) 10 take-first ->list
| 1182
| 1183
| 1184
| 1185
| 1186 >>> 2.1 10.1 [m-n] ->list
| 1187
| 1188 >>> 2.1 10.1 [m-n) ->list
| 1189
| 1190 >>> 4.1 [m-) 10 take-first ->list
| 1191
| 1192 >>> 10.0 [0-n] ->list
| 1193
| 1194 >>> 10.0 [0-n) ->list
| 1195
| 1196 >>> 10.0 [1-n] ->list
| 1197
| 1198 >>> 10.0 [1-n) ->list
| 1199
| 1200
| 1201
| 1202
| 1203 >>> 2 10 2 [m-n:s] ->list
| 1204
| 1205 >>> 2 11 2.0 [m-n:s) ->list
| 1206
| 1207 >>> 4 2 [m-:s) 5 take-first ->list
| 1208
| 1209 >>> 10.0 2 [0-n:s] ->list
| 1210
| 1211 >>> 10 2 [0-n:s) ->list
| 1212
| 1213 >>> 2.0 [0-:s) 5 take-first ->list
| 1214
| 1215 >>> 10 3 [1-n:s] ->list
| 1216
| 1217 >>> 10 3.0 [1-n:s) ->list
| 1218
| 1219 >>> 2 [1-:s) 5 take-first ->list
| 1220
| 1221
| 1222 :[m-n] [ 1 range ] def
| 1223 :[m-n) [ dec [m-n] ] def
| 1224 :[m-) [ nil [m-n] ] def
| 1225 :[0-n] [ 0 swap [m-n] ] def
| 1226 :[0-n) [ 0 swap [m-n) ] def
| 1227 :[0-) [ 0 nil [m-n] ] def
| 1228 :[1-n] [ 1 swap [m-n] ] def
| 1229 :[1-n) [ 1 swap [m-n) ] def
| 1230 :[1-) [ 1 nil [m-n] ] def
| 1231
| 1232 :[m-n:s] 'range def
| 1233 :[m-n:s) [ '- keep [m-n:s] ] def
| 1234 :[m-:s) [ nil >< [m-n:s] ] def
| 1235 :[0-n:s] [ 0 rot> [m-n:s] ] def
| 1236 :[0-n:s) [ 0 rot> [m-n:s) ] def
| 1237 :[0-:s) [ 0 nil <rot [m-n:s] ] def
| 1238 :[1-n:s] [ 1 rot> [m-n:s] ] def
| 1239 :[1-n:s) [ 1 rot> [m-n:s) ] def
| 1240 :[1-:s) [ 1 nil <rot [m-n:s] ] def
| 1241
| 1242
| 1243
| 1244 >>> 42 1list
| 1245
| 1246 >>> :x :y 2list
| 1247
| 1248 >>> :x :y :z 3list
| 1249
| 1250
| 1251 :1list [ () cons ] def
| 1252 :2list [ 1list cons ] def
| 1253 :3list [ 2list cons ] def
| 1254
| 1255
| 1256
| 1257
| 1258 >>> , :fibs ( 0 1 ) [ 'fibs dup rest '+ zip ] lseq def
| 1259 >>> 'fibs 10 take-first ->list
| 1260
| 1261
| 1262 >>> :head [ ( :tail :is :lazy ) ] lseq1 ->list
| 1263
| 1264
| 1265
| 1266
| 1267
| 1268 >>> , [ "evaluated once" say!, ( 1 2 3 ) ] lazy-seq
| 1269 >>> dup !thunk
| 1270
| 1271
| 1272 >>> drop !thunk
| 1273
| 1274
| 1275 :LSeq ( :chunk :thunk ) defrecord
| 1276
| 1277 :show ( :LSeq ) [ .chunk show "#<seq" swap "(...)>" ++ ++ ] defmulti
| 1278
| 1279 :lseq [ __thunk__ LSeq ] def
| 1280 :lseq1 [ '1list dip lseq ] def
| 1281 :lazy-seq [ () swap lseq ] def
| 1282
| 1283
| 1284
| 1285
| 1286
| 1287
| 1288
| 1289
| 1290
| 1291
| 1292
| 1293 >>> nil seq
| 1294
| 1295 >>> "foo" seq
| 1296
| 1297 >>> ( 1 2 3 ) seq
| 1298
| 1299 >>> "" seq
| 1300
| 1301 >>> () seq
| 1302
| 1303 >>> { x: 1, y: 2 } seq
| 1304
| 1305 >>> 10 [1-n] seq ->list
| 1306
| 1307 >>> [ ( 1 2 3 ) ] lazy-seq seq
| 1308
| 1309
| 1310 >>> ( nil "foo" { x: 1 } ( 1 2 ) [1-), ( 1 2 ) 'inc map, 42 ) 'seq? mapl
| 1311
| 1312
| 1313 :seq ( :nil ) [ ] defmulti
| 1314 :seq ( :str ) [ ->nil '->list ~> ] defmulti TODO
| 1315 :seq ( :dict ) [ ->nil '.pairs ~> ] defmulti TODO
| 1316 :seq ( :list ) [ ->nil ] defmulti
| 1317 :seq ( :Range ) [ ->nil ] defmulti
| 1318 :seq ( :LSeq ) [ lseq-seq ] defmulti
| 1319
| 1320 :lseq-seq [ dup .chunk empty? [ !thunk seq ] when ] def
| 1321
| 1322 :seq? ( :nil ) [ drop #t ] defmulti
| 1323 :seq? ( :str ) [ drop :partial ] defmulti
| 1324 :seq? ( :dict ) [ drop :partial ] defmulti
| 1325 :seq? ( :list ) [ drop #t ] defmulti
| 1326 :seq? ( :Range ) [ drop #t ] defmulti
| 1327 :seq? ( :LSeq ) [ drop #t ] defmulti
| 1328 :seq? ( :_ ) [ drop #f ] defmulti
| 1329
| 1330
| 1331
| 1332
| 1333
| 1334 >>> , ( 1 2 ) unseq s!
| 1335
| 1336
| 1337
| 1338
| 1339 >>> c!
| 1340
| 1341 >>> , 3 10 [m-n] unseq ->list s!
| 1342
| 1343
| 1344
| 1345
| 1346 >>> c!
| 1347
| 1348 >>> , [ ( 1 2 ) ] lazy-seq unseq s!
| 1349
| 1350
| 1351
| 1352
| 1353
| 1354 :unseq ( :nil ) [ nil ] defmulti
| 1355 :unseq ( :list ) [ [ nil nil ] [] ^list ] defmulti
| 1356 :unseq ( :Range ) [ range-unseq ] defmulti
| 1357 :unseq ( :LSeq ) [ lseq-unseq ] defmulti
| 1358
| 1359 :lseq-unseq [ [ t . [ t unseq ] [ .uncons^ 't LSeq ] ~seq ] ^LSeq ] def
| 1360
| 1361
| 1362
| 1363
| 1364
| 1365
| 1366
| 1367
| 1368
| 1369 >>> ( 1 2 3 ) first
| 1370
| 1371 >>> 4 [1-n] rest ->list
| 1372
| 1373
| 1374 :first ( :_ ) [ unseq drop ] defmulti
| 1375 :rest ( :_ ) [ unseq nip ] defmulti
| 1376
| 1377
| 1378
| 1379
| 1380
| 1381 >>> "foo" empty?
| 1382
| 1383 >>> () empty?
| 1384
| 1385 >>> { x: 1 } empty?
| 1386
| 1387 >>> 1 [1-n] rest empty?
| 1388
| 1389 >>> ( 1 2 3 ) [ 1 + ] map empty?
| 1390
| 1391
| 1392 :empty? ( :str ) [ .empty? ] defmulti
| 1393 :empty? ( :list ) [ .empty? ] defmulti
| 1394 :empty? ( :dict ) [ .empty? ] defmulti
| 1395 :empty? ( :Range ) [ range-empty? ] defmulti
| 1396 :empty? ( :_ ) [ seq not ] defmulti
| 1397
| 1398
| 1399
| 1400
| 1401
| 1402 >>> "foo" len
| 1403
| 1404 >>> ( 1 2 ) len
| 1405
| 1406 >>> { x: 1 } len
| 1407
| 1408 >>> 37 42 [m-n) len
| 1409
| 1410 >>> 42 [m-) len
| 1411
| 1412 >>> ( 1 2 3 ) [ 2 >= ] filter len
| 1413
| 1414
| 1415 :len ( :str ) [ .len ] defmulti
| 1416 :len ( :list ) [ .len ] defmulti
| 1417 :len ( :dict ) [ .len ] defmulti
| 1418 :len ( :Range ) [ range-len ] defmulti
| 1419 :len ( :_ ) [ seq-len ] defmulti
| 1420
| 1421 :seq-len [ 0 swap [ drop inc ] each ] def
| 1422
| 1423
| 1424
| 1425
| 1426
| 1427 >>> "foo" "bar" ++
| 1428
| 1429 >>> ( 1 2 3 ) ( 4 5 ) ++
| 1430
| 1431
| 1432 >>> ( 1 2 3 ) [ ( 4 5 ) ] lazy-seq ++ ->list
| 1433
| 1434 >>> [ ( 1 2 3 ) ] lazy-seq ( 4 5 ) ++ ->list
| 1435
| 1436
| 1437 :++ ( :str :str ) [ !append ] defmulti
| 1438 :++ ( :list :list ) [ !append ] defmulti
| 1439
| 1440 :++ ( :nil :list ) [ nip ] defmulti TODO
| 1441 :++ ( :nil :LSeq ) [ nip ] defmulti
| 1442
| 1443 :++ ( :list :nil ) [ drop ] defmulti TODO
| 1444 :++ ( :LSeq :nil ) [ drop ] defmulti
| 1445
| 1446 :++ ( :list :LSeq ) [ [ '++ dip LSeq ] ^LSeq ] defmulti
| 1447 :++ ( :LSeq :list ) [ _lseq++ ] defmulti
| 1448 :++ ( :LSeq :LSeq ) [ _lseq++ ] defmulti
| 1449
| 1450 :_lseq++ [ .[ [ '1 ++ ] @ lseq ] ^LSeq ] def
| 1451
| 1452
| 1453
| 1454 >>> "foo" ->list
| 1455
| 1456 >>> ( 1 2 3 ) ->list
| 1457
| 1458 >>> { x: 1, y: 2 } ->list
| 1459
| 1460
| 1461 :->list ( :str ) [ .->list ] defmulti
| 1462 :->list ( :list ) [ ] defmulti
| 1463 :->list ( :dict ) [ .pairs ] defmulti
| 1464
| 1465 :->list ( :_ ) [ xs . ( 'xs [] each ) ] defmulti TODO
| 1466
| 1467
| 1468
| 1469
| 1470
| 1471
| 1472
| 1473 >>> ( 1 2 3 ) [ "empty" ] [ hd tl . 'hd ] ^list
| 1474
| 1475 >>> () [ "empty" ] 'drop ^seq
| 1476
| 1477 >>> ( 4 5 ) [ "empty" ] 'head^ ~seq
| 1478
| 1479 >>> "foo" [ () ] 'nip ^seq
| 1480
| 1481 >>> "foo" [ "" ] [] ~seq
| 1482
| 1483
| 1484 :^list [ [ 'drop % ] dip '.uncons^ % '.empty? ~? ] def
| 1485 :^seq [ 'seq 2dip 'unseq % ~nil ] def
| 1486 :~seq [ [ 'drop % ] dip 'empty? ~? ] def
| 1487
| 1488 :^seq' [ [] swap ^seq ] def
| 1489
| 1490
| 1491
| 1492
| 1493
| 1494
| 1495 >>> () 'len when-seq
| 1496
| 1497 >>> ( 1 2 3 ) 'len when-seq
| 1498
| 1499 >>> ( 1 2 3 ) 'drop with-seq
| 1500
| 1501 >>> () 'drop with-seq
| 1502
| 1503
| 1504 :when-seq [ [ seq dup ] dip when ] def
| 1505 :with-seq [ [ nil ] swap ^seq ] def
| 1506
| 1507
| 1508
| 1509 >>> ( 1 2 3 ) [ dup * ] map ->list
| 1510
| 1511 >>> ( 1 2 3 ) 'dup map ->list
| 1512
| 1513 >>> ( 1 2 3 4 ) 'even? filterl
| 1514
| 1515
| 1516 :map [ f . [ () swap [ [ x xt . ( 'x f ) !append 'xt ] with-seq ]
| 1517 32 times .[ '1 'f map ] lseq ] when-seq ] def TODO
| 1518
| 1519 :filter [ .[ [] 'drop '1 ~? ] map ] def
| 1520
| 1521 :mapl [ map ->list ] def
| 1522 :filterl [ filter ->list ] def
| 1523
| 1524
| 1525
| 1526 >>> ( :x :y ) ( 1 2 3 ) zip' ->list
| 1527
| 1528 >>> ( :x :y ) ( 1 2 3 ) [] zip ->list
| 1529
| 1530 >>> [1-) ( :x :y ) [ swap => ] zip ->list dict
| 1531
| 1532
| 1533 :zip [ f . [ drop nil ] [ y yt . [ x xt .
| 1534 ( 'x 'y f ) [ 'xt 'yt 'f zip ] lseq
| 1535 ] with-seq ] ^seq ] def
| 1536 :zip' [ '2T zip ] def
| 1537
| 1538 TODO
| 1539
| 1540
| 1541
| 1542
| 1543
| 1544
| 1545 >>> ( 2 3 4 ) 10 '- foldl
| 1546
| 1547 >>> ( 2 3 4 ) 10 '- foldr
| 1548
| 1549 >>> ( 2 3 4 ) () [ [ 1 + ] dip cons ] foldr
| 1550
| 1551 >>> ( 2 3 4 ) () [ [ 1 + ] dip call cons ] foldr'
| 1552
| 1553
| 1554 :foldl [ f . swap [ rot> f 'f foldl ] ^seq' ] def
| 1555 :foldr [ f . swap [ <rot 'f foldr f ] ^seq' ] def
| 1556 :foldr' [ f . swap [ <rot 'f 'foldr' 3$ f ] ^seq' ] def
| 1557
| 1558 TODO
| 1559
| 1560
| 1561
| 1562 >>> ( ( 1 2 3 ) dup [ dup * ] map dup 'even? filter ) concat ->list
| 1563
| 1564
| 1565 :concat [ () [ lazy-seq ++ ] foldr' ] def
| 1566
| 1567
| 1568
| 1569
| 1570
| 1571
| 1572 >>> ( 1 2 3 ) reverse
| 1573
| 1574 >>> ( 1 2 3 ) [ dup * ] map reverse
| 1575
| 1576 >>> "foobar" reverse
| 1577
| 1578 >>> 10 20 [m-n] reverse ->list
| 1579
| 1580
| 1581 :reverse ( :str ) [ .reverse ] defmulti
| 1582 :reverse ( :Range ) [ range-reverse ] defmulti
| 1583 :reverse ( :_ ) [ reverse-as-list ] defmulti
| 1584
| 1585 :range-reverse [ [ m n s . 'n [ "reverse: infinite range" fail ]
| 1586 [ 'm 's neg Range ] ~nil ] ^Range ] def
| 1587 :reverse-as-list [ () [ swap cons ] foldl ] def
| 1588
| 1589
| 1590
| 1591 >>> , ( "Hello" "World" ) 'say! each
| 1592
| 1593
| 1594 >>> , ( 1 2 3 ) [] each s!
| 1595
| 1596
| 1597
| 1598
| 1599
| 1600 >>> ( ( 1 2 ) 'dup each )
| 1601
| 1602
| 1603 :each [ f . unseq dup [ 'f dip 'f each ] when2 ] def TODO
| 1604
| 1605
| 1606
| 1607
| 1608 >>> ( 1 2 3 ) cycle 10 take-first ->list
| 1609
| 1610 >>> 0 'inc iterate 10 take-first 2 drop-first ->list
| 1611
| 1612 >>> 1 [ 2 * ] iterate [ 10 < ] drop-while [ 80 < ] take-while ->list
| 1613
| 1614 >>> 42 repeat 4 take-first ->list
| 1615
| 1616 >>> :x 3 replicate ->list
| 1617
| 1618 >>> 10 [1-n] 2 take-nth ->list
| 1619
| 1620
| 1621 :iterate [ x f . ( 'x [ dup f ] 31 times ) dup 31 swap !get^
| 1622 .[ '1 f 'f iterate ] lseq ] def TODO
| 1623
| 1624
| 1625
| 1626
| 1627 :cycle [ repeat concat ] def
| 1628 :repeat [ dup 'repeat $ lseq1 ] def
| 1629 :replicate [ 'repeat dip take-first ] def
| 1630
| 1631 :take-first [ .[ '1 [ dec 'take-first 2$ lseq1 ] [ 3drop nil ] ~pos ]
| 1632 with-seq ] def
| 1633 :drop-first [ .[ '1 0 > [ rest '1 dec drop-first ] when ] when-seq ] def
| 1634
| 1635 :take-while [ p? . nil [ over p? 'lseq1 [ 2drop nil ] if ] foldr' ] def
| 1636 :drop-while [ p? . [ dup first p? [ rest 'p? drop-while ] when ]
| 1637 when-seq ] def
| 1638
| 1639 :take-nth [ n . [ [ 'n dec drop-first 'n take-nth ] $ lseq1 ]
| 1640 with-seq ] def
| 1641
| 1642 TODO...
| 1643 TODO
| 1644
| 1645
| 1646
| 1647
| 1648
| 1649 >>> [1-) [ 4 > ] find
| 1650
| 1651 >>> 10 [1-n] [ 0 < ] find
| 1652
| 1653
| 1654 :find [ filter 'drop with-seq ] def
| 1655
| 1656
| 1657
| 1658
| 1659
| 1660
| 1661
| 1662 >>> , "Hello World!" [ "aeiou" elem?' ] partition 'join bi$ s!
| 1663
| 1664
| 1665
| 1666
| 1667
| 1668 :partition [ 'filter [ 'not @ filter ] 2bi ] def TODO
| 1669
| 1670
| 1671
| 1672 >>> ( 1 2 4 -1 7 3 ) minimum
| 1673
| 1674 >>> ( 1 2 4 -1 7 3 ) maximum
| 1675
| 1676 >>> () maximum
| 1677
| 1678
| 1679 :minimum [ [ "minimum: empty list" fail ] [ swap 'min foldl ] ^seq ] def
| 1680 :maximum [ [ "maximum: empty list" fail ] [ swap 'max foldl ] ^seq ] def
| 1681
| 1682
| 1683
| 1684 >>> ( 1 2 3 4 ) sum
| 1685
| 1686 >>> ( 1 2 3 4 ) product
| 1687
| 1688 >>> () sum
| 1689
| 1690 >>> () product
| 1691
| 1692
| 1693 :sum [ 0 '+ foldl ] def
| 1694 :product [ 1 '* foldl ] def
| 1695
| 1696
1697
+ - 1698 +--378 lines: -- Lists, Dicts & Indexing --
1698
| 1699
| 1700
| 1701
| 1702
| 1703
| 1704
| 1705 >>> ( 1 2 3 )
| 1706
| 1707 >>> dup head^
| 1708
| 1709 >>> drop dup tail^
| 1710
| 1711 >>> , drop uncons^ s!
| 1712
| 1713
| 1714
| 1715
| 1716 >>> cons
| 1717
| 1718
| 1719 >>> () head^
| 1720
| 1721 >>> () tail^
| 1722
| 1723 >>> () head
| 1724
| 1725 >>> () tail
| 1726
| 1727
| 1728 :head^ '.head^ def
| 1729 :tail^ '.tail^ def
| 1730
| 1731 :head [ ->nil 'head^ ~> ] def
| 1732 :tail [ ->nil 'tail^ ~> ] def
| 1733
| 1734 :uncons^ '.uncons^ def
| 1735 :cons '!cons def
| 1736
| 1737
| 1738
| 1739 >>> ( 4 2 1 3 ) sort
| 1740
| 1741
| 1742 >>> ( nil #f #t 0 10 -2.0 4.0 "foo" :bar ) dup sort =
| 1743
| 1744
| 1745 >>> ( 1 2.0 3 4.0 ) sort
| 1746
| 1747 >>> ( 1 2.0 3 4.0 ) sort'
| 1748
| 1749
| 1750 :sort [ ->list .sort ] def
| 1751 :sort' [ ->list .sort' ] def
| 1752
| 1753 TODO
| 1754
| 1755
| 1756
| 1757 >>> ( 1 2 2 3 2 ) uniq ->list
| 1758
| 1759 >>> ( 1 2 2 3 2 ) sort uniq ->list
| 1760
| 1761
| 1762 >>> ( 0.0 0.0 / dup ) uniq ->list
| 1763
| 1764
| 1765 :uniq [ '= uniq-by ] def
| 1766 :uniq-by [ f . [ over .[ '1 [ '2 swap f ] drop-while 'f uniq-by ]
| 1767 lseq1 ] with-seq ] def
| 1768
| 1769
| 1770
| 1771 >>> { x: 1, y: 2 } { x: 99 } update
| 1772
| 1773
| 1774 >>> , :Point ( :x :y ) defrecord
| 1775 >>> Point( 1 2 )
| 1776
| 1777 >>> { y: 3 } update
| 1778
| 1779
| 1780 :update ( :dict :dict ) [ !merge ] defmulti
| 1781 :update ( :_ :_ ) [ over [ 'record->dict dip !merge ] dip
| 1782 record-type apply-dict ] defmulti TODO
| 1783
| 1784 TODO
| 1785
| 1786
| 1787
| 1788 >>> { x: 1, y: 2 } dup keys
| 1789
| 1790 >>> drop vals
| 1791
| 1792
| 1793 >>> c!
| 1794
| 1795 >>> , :Point ( :x :y ) defrecord
| 1796 >>> , Point( 1 2 ) 'keys 'vals bi s!
| 1797
| 1798
| 1799
| 1800
| 1801
| 1802 :keys ( :dict ) [ .keys ] defmulti
| 1803 :values ( :dict ) [ .values ] defmulti
| 1804
| 1805 :keys ( :_ ) [ record-type record-type-fields ] defmulti
| 1806 :values ( :_ ) [ record-values ] defmulti
| 1807
| 1808 :vals 'values def
| 1809
| 1810
| 1811
| 1812
| 1813
| 1814
| 1815
| 1816
| 1817
| 1818
| 1819 >>> "0123456789" 5 [i-)
| 1820
| 1821 >>> "0123456789" -5 [-j)
| 1822
| 1823 >>> "0123456789" 3 -3 [i-j)
| 1824
| 1825
| 1826 >>> ( 0 1 2 3 4 5 6 7 8 9 ) -5 [i-)
| 1827
| 1828 >>> ( 0 1 2 3 4 5 6 7 8 9 ) 5 [-j)
| 1829
| 1830
| 1831 >>> 10 20 [m-n] 2 -2 [i-j) ->list
| 1832
| 1833 >>> [1-) 10 [-j) ->list
| 1834
| 1835 >>> 10 -2 -2 [m-n:s] -2 0 -2 [i-j:s) ->list
| 1836
| 1837 >>> 10 -2 -2 [m-n:s] -2 2 -1 [i-j:s) ->list
| 1838
| 1839 >>> [1-) -2 [-j)
| 1840
| 1841
| 1842 >>> [1-) [ dup * ] map 2 10 2 [i-j:s) ->list
| 1843
| 1844
| 1845 :slice [ <rot4 slice' ] def
| 1846
| 1847 :[i-j) [ 1 slice ] def TODO
| 1848 :[i-) [ nil [i-j) ] def
| 1849 :[-j) [ nil swap [i-j) ] def
| 1850
| 1851 :[i-j:s) 'slice def
| 1852 :[i-:s) [ nil swap [i-j:s) ] def
| 1853 :[-j:s) [ nil rot> [i-j:s) ] def
| 1854 :[:s) [ nil nil <rot [i-j:s) ] def
| 1855
| 1856 :slice' ( :str ) [ !slice ] defmulti
| 1857 :slice' ( :list ) [ !slice ] defmulti
| 1858 :slice' ( :Range ) [ rot4> range-slice ] defmulti
| 1859 :slice' ( :LSeq ) [ rot4> seq-slice ] defmulti
| 1860
| 1861 :range-slice [ i j t . [ m n s .
| 1862 { :tn 't neg? =>,
| 1863 f: [ inc 's * 'n
| 1864 [ "range-slice: infinite range w/ negative "
| 1865 "index" ++ fail ] [] ~nil + ],
| 1866 g: [ 's * 'm + ] }
| 1867 let[
| 1868 'i [ 'tn 'n 'm ? ] [ 'f 'g ~neg ] ~nil,
| 1869 'j [ 'tn 'm 'n ? ] [
| 1870 'tn 'inc 'dec if
| 1871 'f [ g 'n [] [ 's neg? 'max 'min if ] ~nil ] ~neg
| 1872 ] ~nil, 's 't *
| 1873 ] Range ] ^Range ] def TODO
| 1874
| 1875 :seq-slice [ i j s .
| 1876 'i [] 'drop-first ~nil
| 1877 'j [] [ 'i 0 or - take-first ] ~nil
| 1878 's 1 = [ 's take-nth ] unless
| 1879 ] def TODO
| 1880
| 1881
| 1882
| 1883 >>> ( :one :two :three ) 1 get^
| 1884
| 1885 >>> () 0 get^
| 1886
| 1887 >>> ( 1 2 3 ) 1 get
| 1888
| 1889 >>> () 0 get
| 1890
| 1891
| 1892 >>> { x: 1, y: 2 } dup :x get
| 1893
| 1894 >>> drop :z get
| 1895
| 1896 >>> "foobar" 3 get
| 1897
| 1898 >>> "foobar" 10 get
| 1899
| 1900 >>> [1-) 10 get
| 1901
| 1902 >>> 10 [1-n] 10 get
| 1903
| 1904
| 1905 >>> ( :one :two :three ) 1 has?
| 1906
| 1907 >>> ( :one :two :three ) :two elem?
| 1908
| 1909 >>> { x: 1, y: 2 } :y has?
| 1910
| 1911 >>> "foobar" 3 has?
| 1912
| 1913 >>> "hypotheekofferte" "theekoffer" elem?
| 1914
| 1915
| 1916 >>> [1-) 0 has?
| 1917
| 1918 >>> [1-) 0 elem?
| 1919
| 1920 >>> [1-) 99 elem?
| 1921
| 1922 >>> 100 [1-n) 100 elem?
| 1923
| 1924
| 1925 :get [ 2dup has? 'get^ [ 2drop nil ] if ] def
| 1926
| 1927 :get^ [ swap get^' ] def
| 1928 :has? [ swap has?' ] def
| 1929 :elem? [ swap elem?' ] def
| 1930
| 1931 :get^' ( :_ ) [ !get^ ] defmulti
| 1932 :has?' ( :_ ) [ !has? ] defmulti
| 1933 :elem?' ( :_ ) [ !elem? ] defmulti
| 1934
| 1935 :has?' ( :nil ) [ 2drop #f ] defmulti
| 1936
| 1937 :get^' ( :Range ) [ range-get^' ] defmulti
| 1938 :has?' ( :Range ) [ range-has?' ] defmulti
| 1939 :elem?' ( :Range ) [ range-elem?' ] defmulti
| 1940
| 1941
| 1942
| 1943 >>> :x :y 2list
| 1944
| 1945 >>> , '1st '2nd bi s!
| 1946
| 1947
| 1948
| 1949
| 1950 >>> ( 1 2 3 4 5 ) 3rd
| 1951
| 1952
| 1953 :1st 'first def
| 1954 :2nd [ rest '1st ~> ] def
| 1955 :3rd [ rest '2nd ~> ] def
| 1956
| 1957
| 1958
| 1959 >>> () last
| 1960
| 1961 >>> ( 1 2 3 ) last
| 1962
| 1963 >>> ( 1 2 3 ) [ dup * ] map last
| 1964
| 1965 >>> () last^
| 1966
| 1967 >>> ( :x :y :z ) last
| 1968
| 1969
| 1970 :last ( :LSeq ) [ :_ nil <rot [ 'nip dip unseq dup ] loop 2drop ] defmulti
| 1971 :last ( :_ ) [ dup len dec nth ] defmulti
| 1972
| 1973 :last^ ( :nil ) [ drop "last^: nil" fail ] defmulti
| 1974 :last^ ( :LSeq ) [ [ "last^: empty list" fail ] 'last ~seq ] defmulti
| 1975 :last^ ( :_ ) [ dup len dec get^ ] defmulti TODO
| 1976
| 1977
| 1978
| 1979 >>> [1-) 10 nth
| 1980
| 1981 >>> [1-) [ dup * ] map 10 nth
| 1982
| 1983
| 1984 :nth [ swap nth' ] def TODO
| 1985
| 1986 :nth' ( :_ ) [ swap get ] defmulti
| 1987 :nth' ( :LSeq ) [ [ drop nil ]
| 1988 [ x xt . [ dec 'xt nth' ] [ drop 'x ] ~pos ]
| 1989 ^seq ] defmulti
| 1990
| 1991
| 1992
| 1993 >>> "foo" indexed ->list
| 1994
| 1995 >>> "foo" indexed' ->list
| 1996
| 1997
| 1998 :indexed [ [0-) swap zip' ] def
| 1999 :indexed' [ [0-) zip' ] def
| 2000
| 2001
| 2002
| 2003 >>> , ( { x: 42 } { y: ( 37 ) } )
| 2004 >>> dup ( 0 :x ) get-in
| 2005
| 2006 >>> drop ( 1 :y 0 ) get-in
| 2007
| 2008
| 2009 :get-in [ [ 'get $ ] map ~~> ] def
| 2010
| 2011
| 2012
| 2013 >>> { x: 1, y: 2 } 3 :z assoc
| 2014
| 2015
| 2016 >>> , ( { x: 42 } { y: ( 37 ) } )
| 2017 >>> #t ( 1 :z ) assoc-in
| 2018
| 2019 >>> nil "magic" ( :x :y :z ) assoc-in
| 2020
| 2021
| 2022 >>> ( 1 2 ) :x 2 assoc
| 2023
| 2024 >>> ( 1 2 ) :x 3 assoc
| 2025
| 2026
| 2027 :assoc [ <rot assoc' ] def
| 2028 :assoc-in [ [ [] [ v k kt . dup 'k get 'v 'kt assoc-in 'k ] ~seq
| 2029 assoc ] ^seq' ] def TODO
| 2030
| 2031 :assoc' ( :nil ) [ v k _ . { 'k 'v => } ] defmulti
| 2032 :assoc' ( :list ) [ v i l .
| 2033 'l len inc 'i "assoc" assert-in-range
| 2034 'l 'i [-j) ( 'v ) 'l 'i inc [i-) ++ ++
| 2035 ] defmulti
| 2036 :assoc' ( :dict ) [ v k d . 'd { 'k 'v => } !merge ] defmulti
| 2037
| 2038 :assert-in-range [ l i s . 'i 0 < 'i 'l >= or [
| 2039 ( 's 'i ) "${0}: index ${1} is out of range" fmt
| 2040 fail
| 2041 ] when ] def
| 2042
| 2043
| 2044
| 2045 >>> { x: 1, y: 2 } 'inc :x modify
| 2046
| 2047
| 2048 >>> { x: { y: 1 } } 'inc ( :x :y ) modify-in
| 2049
| 2050 >>> { x: { y: 1 } } [ [ 42 ] 'inc ~nil ] ( :x :z ) modify-in
| 2051
| 2052
| 2053 >>> { x: 1, y: 2 } :x 'dec modify'
| 2054
| 2055 >>> { x: { y: 1 } } ( :x :y ) 'dec modify-in'
| 2056
| 2057
| 2058 :modify [ f k . dup 'k get f 'k assoc ] def
| 2059 :modify-in [ f ks . dup 'ks get-in f 'ks assoc-in ] def TODO
| 2060
| 2061 :modify' [ swap modify ] def
| 2062 :modify-in' [ swap modify-in ] def
| 2063
| 2064
| 2065
| 2066 >>> { x: 1, y: 2, z: 3 } :y dissoc
| 2067
| 2068 >>> :foo dissoc
| 2069
| 2070
| 2071 :dissoc [ swap dissoc' ] def
| 2072
| 2073 :dissoc' ( :dict ) [ !delete ] defmulti
| 2074
| 2075
2076
+ - 2077 +-- 51 lines: -- Regexes & String Formatting --
2077
| 2078
| 2079
| 2080
| 2081
| 2082
| 2083 >>> "foo" "^f" =~
| 2084
| 2085 >>> "bar" "^f" =~
| 2086
| 2087
| 2088 :=~ ( :str :str ) [ rx-match bool ] defmulti TODO
| 2089
| 2090
| 2091
| 2092 >>> ( "one" "two" "three" ) "^o|ee" grep ->list
| 2093
| 2094
| 2095 :grep [ '=~ $ filter ] def
| 2096
| 2097
| 2098
| 2099
| 2100
| 2101 >>> "1 2 3 4" "$2 $1" "(\w+) (\w+)" rx-sub1
| 2102
| 2103 >>> "1 2 3 4" "$2 $1" "(\w+) (\w+)" rx-suball
| 2104
| 2105
| 2106 >>> "foo bar baz" [ reverse ] "\w+" s///
| 2107
| 2108 >>> "foo bar baz" [ reverse ] "\w+" s///g
| 2109
| 2110
| 2111 :rx-sub1 [ #f rx-sub ] def
| 2112 :rx-suball [ #t rx-sub ] def
| 2113
| 2114 :s/// 'rx-sub1 def
| 2115 :s///g 'rx-suball def
| 2116
| 2117
| 2118
| 2119 >>> ( :x 42 "foo" ) "${2} ${1} ${0}" fmt
| 2120
| 2121
| 2122 :fmt [ >< .[ '1 >< str->int get ->str nip ]
| 2123 "\$\{(\d+)\}" s///g ] def TODO
| 2124
| 2125 TODO...
| 2126
| 2127
2128
+ - 2129 +-- 13 lines: -- "Quasi-Macros" --
2129
| 2130
| 2131
| 2132
| 2133 >>> { x: 1, y: 2 } [ 'y 'x + ] let
| 2134
| 2135 >>> { x: 1, y: 2 } let[ 'y 'x + ]
| 2136
| 2137
| 2138 :let [ [ 'vals 'keys bi ] dip '__block-code__ keep __block__
| 2139 apply ] def TODO
| 2140
| 2141
2142
+ - 2143 +--122 lines: -- Miscellaneous: Looping, I/O, Exceptions, etc. --
2143
| 2144
| 2145
| 2146
| 2147 >>> id
| 2148 >>> []
| 2149
| 2150
| 2151 :id [ ] def
| 2152 :[] [ 'id ] def
| 2153
| 2154
| 2155
| 2156 >>> ( 1 2 3 ) 42 const mapl
| 2157
| 2158
| 2159 :const '[ drop '1 ] def
| 2160
| 2161
| 2162
| 2163 >>> , [ "Hi!" say! ] 5 times
| 2164
| 2165
| 2166
| 2167
| 2168
| 2169 >>> 0 1 [ '+ keep swap ] 5 times
| 2170
| 2171
| 2172 :times [ f n . 'n 0 > [ f 'f 'n dec times ] when ] def
| 2173
| 2174
| 2175
| 2176
| 2177 >>> , :next-collatz [ [ 2 div ] [ 3 * 1 + ] 'even? ~? ] def
| 2178
| 2179 >>> ( 12 [ dup next-collatz dup 1 not= ] loop )
| 2180
| 2181 >>> ( 12 [ dup 1 not= ] [ dup next-collatz ] while )
| 2182
| 2183 >>> ( 12 [ dup 1 = ] [ dup next-collatz ] until )
| 2184
| 2185
| 2186 >>> ( 1 [ dup next-collatz dup 1 not= ] loop )
| 2187
| 2188
| 2189 >>> ( 1 [ dup 1 not= ] [ dup next-collatz ] while )
| 2190
| 2191 >>> ( 1 [ dup 1 = ] [ dup next-collatz ] until )
| 2192
| 2193
| 2194 >>> ( 1 [ dup 1 not= ] [ dup next-collatz ] do- while )
| 2195
| 2196 >>> ( 1 [ dup 1 = ] [ dup next-collatz ] do- until )
| 2197
| 2198
| 2199 :loop [ f . f [ 'f loop ] when ] def
| 2200 :while [ p? f . p? [ f 'p? 'f while ] when ] def
| 2201 :until [ [ 'not @ ] dip while ] def
| 2202
| 2203 :do- [ dup 2dip ] def
| 2204 :do-while [ do- while ] def
| 2205 :do-until [ do- until ] def
| 2206
| 2207
| 2208
| 2209 >>> "Hello, World!" say!
| 2210
| 2211
| 2212 >>> 42 display!
| 2213
| 2214 >>> "foo" ddisplay!
| 2215
| 2216
| 2217
| 2218 >>> , ( :x 42 "foo" ) "${2} ${1} ${0}\n" fmt!
| 2219
| 2220
| 2221 :say! [ "\n" !append puts! ] def
| 2222 :display! [ ->str say! ] def
| 2223 :ddisplay! [ dup display! ] def
| 2224 :fmt! [ fmt puts! ] def
| 2225
| 2226
| 2227
| 2228 >>> , :foo [ 1 2, "foo" trace!, + ] def
| 2229 >>> foo
| 2230
| 2231
| 2232
| 2233
| 2234
| 2235
| 2236
| 2237 :trace! [ 1list "--- TRACE: ${0} ---" fmt say! __show-stack!__ ] def
| 2238
| 2239
| 2240
| 2241
| 2242
| 2243 :read-line! [ nil ask! ] def TODO
| 2244 :lines! [ read-line! [ 'lines! lseq1 ] ~> ] def
| 2245
| 2246
| 2247
| 2248 >>> [ ... ] [ _ _ _ . :caught #t ] try-catch
| 2249
| 2250 >>> [ ... ] [ :finally ] try-finally
| 2251
| 2252
| 2253 :try-catch [ [] try ] def
| 2254 :try-finally [ nil swap try ] def
| 2255
| 2256
| 2257
| 2258 >>> [ 1 1 = ] assert
| 2259 >>> [ 1 2 = ] assert
| 2260
| 2261
| 2262 :assert [ b . b [ "assert failed: " 'b ->str ++ fail ] unless ] def
| 2263
| 2264
2265
+ - 2266 +--132 lines: -- Either, Functor, Monad, etc. --
2266
| 2267
| 2268 :Left ( :val ) defrecord
| 2269 :Right ( :val ) defrecord
| 2270
| 2271 :left 'Left def
| 2272 :right 'Right def
| 2273
| 2274 :show ( :Left ) [ .val show "left( " swap ++ " )" ++ ] defmulti
| 2275 :show ( :Right ) [ .val show "right( " swap ++ " )" ++ ] defmulti
| 2276
| 2277
| 2278
| 2279
| 2280
| 2281
| 2282 >>> , :f [ [ 2 div ] [ 2.0 / ] ^either ] def
| 2283 >>> 5 left f
| 2284
| 2285 >>> 7.0 right f
| 2286
| 2287
| 2288 >>> [ 1 0 div ] try->either
| 2289
| 2290 >>> [ 5 2 div ] try->either
| 2291
| 2292
| 2293 >>> "oops" left either->fail
| 2294
| 2295 >>> 42 right either->fail
| 2296
| 2297
| 2298 :~either [ <rot ~either' ] def
| 2299 :^either [ [ '.val % ] bi$ ~either ] def
| 2300 :either? [ 'Left? 'Right? bi-or ] def
| 2301
| 2302 :~either' ( :Left ) [ f _ x . 'x f ] defmulti
| 2303 :~either' ( :Right ) [ _ g x . 'x g ] defmulti
| 2304
| 2305 :try->either [ f . [ Right( f ) ] [ 3list Left #t ] [] try ] def
| 2306 :either->fail [ 'fail [] ^either ] def
| 2307
| 2308
| 2309
| 2310 >>> , :f [ 'inc -> [ dup * ] -> ] def
| 2311 >>> nil f
| 2312
| 2313 >>> x: 7 f
| 2314
| 2315 >>> ( 1 2 3 ) f ->list
| 2316
| 2317 >>> ( 1 2 3 ) 'odd? filter f ->list
| 2318
| 2319 >>> :oops left f
| 2320
| 2321 >>> 7 right f
| 2322
| 2323
| 2324 :-> [ swap ->' ] def
| 2325
| 2326 :->' ( :nil ) [ nip ] defmulti
| 2327 TODO
| 2328 TODO
| 2329 TODO
| 2330 ...TODO
| 2331 TODO
| 2332 :->' ( :pair ) [ swap ^pair => ] defmulti
| 2333 :->' ( :list ) [ swap map ] defmulti
| 2334 ...TODO
| 2335 :->' ( :block ) [ % ] defmulti TODO
| 2336 :->' ( :builtin ) [ % ] defmulti TODO
| 2337 :->' ( :multi ) [ % ] defmulti TODO
| 2338
| 2339 :->' ( :LSeq ) [ swap map ] defmulti
| 2340 :->' ( :Left ) [ nip ] defmulti
| 2341 :->' ( :Right ) [ swap ^Right Right ] defmulti
| 2342
| 2343
| 2344
| 2345 >>> ( 1 2 3 ) [ 'inc [ dup * ] bi 2list ] >>= ->list
| 2346
| 2347 >>> ( 1 2 3 ) [ x ret . ( 4 5 ) [ y . T( 'x 'y ) ret ] bind ]
| 2348 ... bind-with ->list
| 2349
| 2350
| 2351 >>> do[ ( 1 2 3 ) :x <- ( 4 5 ) :y <- T( 'x 'y ) return ] ->list
| 2352
| 2353
| 2354 >>> , :f [ [ drop "neg" left ] [ dup * right ] ~neg ] def
| 2355 >>> "oops" left 'f >>=
| 2356
| 2357 >>> -4 right 'f >>=
| 2358
| 2359 >>> 4 right 'f >>=
| 2360
| 2361
| 2362 >>> right( :x ) right( :y ) >>
| 2363
| 2364
| 2365 >>> nil 4 replicate ( 1 2 ) >> '1list >>= ->list
| 2366
| 2367 >>> do[ nil 4 replicate & ( 1 2 ) :x <- 'x return ] ->list
| 2368
| 2369
| 2370 :>>= [ swap =<< ] def
| 2371 :>> [ const >>= ] def
| 2372
| 2373 :=<< ( :list ) [ swap map concat ] defmulti
| 2374 :=<< ( :LSeq ) [ swap map concat ] defmulti
| 2375 :=<< ( :Left ) [ nip ] defmulti
| 2376 :=<< ( :Right ) [ swap ^Right ] defmulti
| 2377
| 2378 :return-as ( :list ) [ drop 1list ] defmulti
| 2379 :return-as ( :LSeq ) [ drop 1list ] defmulti
| 2380 :return-as ( :Left ) [ drop Right ] defmulti
| 2381 :return-as ( :Right ) [ drop Right ] defmulti
| 2382
| 2383 :bind '>>= def
| 2384 :bind-with [ x f . 'x [ [ 'x return-as ] f ] >>= ] def
| 2385
| 2386 { :_& :& __ident__ =>, :_<- :<- __ident__ =>, blk: '__block__,
| 2387 ret: :return, :bw :bind-with __ident__ => } let[
| 2388
| 2389 :do [ b . () 'b __block-code__ _do& 'b _do 'b blk call ] def
| 2390 :_do& [ [ dup '_& = [ drop :_ '_<- ] when ] mapl ] def
| 2391 :_do [ b . [ () ] [ dup 1st '_<- =
| 2392 [ uncons^ nip [ 'ret 2list ] dip 'b _do 'b blk 'bw 2list ]
| 2393 [ 'b _do cons ] if ] ^list ] def TODO
| 2394
| 2395 ]
| 2396
| 2397
2398
+ - 2399 +-- 25 lines: -- The Joy of Recursion --
2399
| 2400
| 2401
| 2402
| 2403 >>> , :fac1 [ dup zero? 'inc [ dup dec fac1 * ] if ] def
| 2404 >>> 5 fac1
| 2405
| 2406 >>> , :fac2 [ 'zero? 'inc [ dup dec ] '* linrec ] def
| 2407 >>> 5 fac2
| 2408
| 2409
| 2410 >>> , :qsort1 [ dup empty? [] [ unseq over '< $ partition
| 2411 ... 'qsort1 bi$ [ swap 1list ] dip ++ ++ ] if ] def
| 2412 >>> ( 5 2 7 2 -4 1 ) qsort1 ->list
| 2413
| 2414 >>> , :qsort2 [ 'empty? [] [ unseq over '< $ partition ]
| 2415 ... [ [ swap 1list ] dip ++ ++ ] binrec ] def
| 2416 >>> ( 5 2 7 2 -4 1 ) qsort2 ->list
| 2417
| 2418
| 2419 :linrec [ p? f g h . dup p? 'f [ g 'p? 'f 'g 'h linrec h ] if ] def
| 2420 :binrec [ p? f g h . dup p? 'f [ g [ 'p? 'f 'g 'h binrec ] bi$
| 2421 h ] if ] def TODO
| 2422
| 2423
2424
+ - 2425 +-- 23 lines: -- Conversion --
2425
| 2426
| 2427
| 2428
| 2429 >>> ( 1 2 3 ) [ 'inc map ] as
| 2430
| 2431 >>> "foo" [ 'upper-case map ] as
| 2432
| 2433
| 2434 :as [ over convert-> 'call dip call ] def
| 2435
| 2436 :convert-> ( :str ) [ drop 'convert->str ] defmulti TODO
| 2437 :convert-> ( :list ) [ drop 'convert->list ] defmulti
| 2438
| 2439 :convert->str ( :str ) [ ] defmulti TODO
| 2440 :convert->str ( :list ) [ join ] defmulti
| 2441 :convert->str ( :LSeq ) [ join ] defmulti
| 2442
| 2443 :convert->list ( :list ) [ ] defmulti TODO
| 2444 :convert->list ( :str ) [ ->list ] defmulti
| 2445 :convert->list ( :LSeq ) [ ->list ] defmulti
| 2446
| 2447
2448
+ - 2449 +-- 28 lines: -- Modules --
2449
| 2450
| 2451
| 2452
| 2453 >>> , :no-such-module require
| 2454
| 2455 >>> , :no-such-module [] defmodule
| 2456 >>> , :no-such-module require
| 2457
| 2458 :require [ dup __modules__ !elem? '__load-module__ unless1 ] def
| 2459
| 2460
| 2461
| 2462 >>> , :_test use
| 2463 ...
| 2464 >>> 'x
| 2465
| 2466
| 2467 >>> , ( :x :y ) :_test use-from
| 2468 >>> 'y
| 2469
| 2470
| 2471 :use [ __caller-module__ . 'require
| 2472 [ '__caller-module__ defmodule[ import ] ] bi ] def
| 2473 :use-from [ __caller-module__ . 'require
| 2474 [ '__caller-module__ defmodule[ import-from ] ] bi ] def
| 2475
| 2476
2477
+ - 2478 +-- 22 lines: -- Unicode Aliases --
2478
| 2479
| 2480 :← 'def def
| 2481
| 2482 :≠ 'not= ←
| 2483 :≤ '<= ←
| 2484 :≥ '>= ←
| 2485
| 2486 :∘ '% ←
| 2487
| 2488 :¬ 'not ←
| 2489 :∧ 'and ←
| 2490 :∨ 'or ←
| 2491
| 2492 :~[≠] '~[not=] ←
| 2493 :~[≤] '~[<=] ←
| 2494 :~[≥] '~[>=] ←
| 2495
| 2496 :∋ 'elem? ←
| 2497 :∌ [ ∋ not ] ←
| 2498
| 2499
2500
2501 ...
2502
2503
2504
2505 ]
2506
2507