std.fun 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #comment -[
  2. This module contains general purpose operations that are frequently
  3. used in the language.
  4. Copyright (C) 2007-2010 Dennis Furey]-
  5. #import nat
  6. #library+
  7. #export+
  8. #import cor
  9. characters = -<& %cI*~ upto 8
  10. letters = -- (take/26)^~(skip/97,skip/65) characters
  11. digits = '0123456789'
  12. gpl = # takes a version number as a character string
  13. -[This program is free software; you can redistribute it and/or modify
  14. it under the terms of the GNU General Public License as published by
  15. the Free Software Foundation; version -[.~&?\'3'! ~&iNC]-.
  16. This program is distributed in the hope that it will be useful,
  17. but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. GNU General Public License for more details.
  20. You should have received a copy of the GNU General Public License
  21. along with this program; if not, write to the Free Software Foundation,
  22. Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA]-
  23. #optimize+
  24. command_line :: files _file%L options _option%L
  25. file :: stamp %sbU path %sL preamble %sL contents %sLxU
  26. option :: position %n longform %b keyword %s parameters %sL
  27. invocation :: command _command_line environs %sm
  28. # first order
  29. choices = ^(iota@r,~&l); leql@a^& ~&al?\&! ~&arh2fabt2RDfalrtPXPRT
  30. closure = ^= ^Ts\~& *=iiD ^D/~&rl @rlX ~&r*+ ~| ~&lrPrlPE
  31. cross = -**=+ *-
  32. cuts = @rlX ~&al^?\~&arrNCNCB ^|Darl2falrrPXPRDSL/~& ^|D/predecessor @NiXNC ~&hr->txtlxPrXS ~&hrhPlCrtPXPiC
  33. enum = ~&ddvDlrdPErvPrNCQSL2Vo+ %-U:-0+ %-u*
  34. eql = ~&alParPfabt2RBarZPq
  35. gcp = ~&al^&~&arPalhPrhPEPalh2fabt2RCBB
  36. indexable = ~&l&&~&all2alr2fallPrXPRfalrPrXPRBarPfabl2RBQalr2arPfabr2RBalPQq
  37. intersecting = ~&ar^& ~&arhPlw!| ~&falrtPXPR
  38. iol = ~&NiX; ~&r->lx ^\~&rt ~&l; :^\~& ~&i&& successor+ ~&h
  39. leql = ~&alZ^!~&arPfabt2RB
  40. lleq = ~&alParPallPrlPEPfabr2Rfabl2RQNQNNXq
  41. num = ^p\~& iol
  42. permutations = ~&itB^?a\~&aNC @ahPfatPRD *= refer ^C/~&a ~&ar&& ~&arh2falrtPXPRD
  43. powerset = -<&+ ~&rFlSPS+ zipp0^*D/~& iota+ ~&NSNNXNCT
  44. singly_branched = ~&aalParPNfalPRQfarPRQNNXq
  45. skip = ~&alrB^?\~&ar ^R/~&f ^/~&ahPatPNatPCBNNXfatPRCq+~&al ~&art
  46. subset = *-; all -=
  47. substring = ^!~&arPfalrtPXPRB [=+ ~&a
  48. suffix = [=+~&bx
  49. take = ~&alrB^& :^/~&arh ^R/~&f ^\~&art ~&ahPatPNatPCBNNXfatPRCq+ ~&al
  50. upto = ~&\<<0>>; ~&l->xrLPO ^/~&ahPatPNatPCBNNXfatPRCq+~&l ^lLPrC\~&r cross*rrxPp
  51. zip = ~&alrB^?/~&abh2fabt2RC ~&alrY&& <'bad zip'>!%
  52. zipt = ~&alrB^&~&abh2fabt2RC
  53. # second order
  54. all = ~&a^?\&!+ &&~&fatPR+ ~&ah;
  55. all_same = ~&aatPBZ^!~&ahthPEPfatPRB++ *
  56. any = ~&a^&+ !|~&fatPR+ ~&ah;
  57. arc = choice+ !*
  58. associate_left = ~&i&&+ ~&t?\~&h+ ~&x;+ <:~&ahPatt2fatPRath2QX+ ~&rlX;
  59. border = +^|(~&,*)=>+ (^CihNCT\~&+ @h)*+ (*)|\x+ 0!!*+ iota
  60. both = ~&B++ ~~
  61. case = gcase ==
  62. cases = gcase -=
  63. choice = ^H\~&+ mtwist..u_enum++ !
  64. dot = "s". "f". * file$[stamp: &!,path: ~&iNC+ --(:/`. "s")+ ~&n,contents: "f"+ ~&m]
  65. either = ~&Y++ ~~
  66. fused = +^/~& ~&iNH; //~&; //+ ~&al^?\~&arlrY ~&fallPrbl2XlrPrbr2XXPW+ ~&falPbrlYPalrGPOXJ # record constructor
  67. gang = (^)=>0!
  68. gdif = ~&rlD;+ ~&r*++ *~+ not+ ~&rlD;+ any
  69. gint = ~&rlD;+ ~&r*++ *~+ ~&rlD;+ any
  70. gldif "r" = ~&al^& ~&alPfarPRT^J/~&f ~&a; ~&ar^?\~&alhPNCltPrXX "r"?abh/~&Nabt2X ~&rlPrrl2lrrr2CXXarh2falrtPXPRXO
  71. glimit = "f". ~&iNC; ~&htwZ->h ^("f"+ ~&h,~&); :^/~&l ^(weight+~&l,~&r); ~| nleq^\~&l weight+ ~&r
  72. glint "r" = ~&al^& ~&alPfarPRT^J/~&f ~&a; ~&ar^?\~&NaltPrXPX "r"?abh/~&alhPNCbtPX ~&rlPrrl2lrrr2CXXarh2falrtPXPRXO
  73. lesser = "r". "r"?/~&l ~&r
  74. mat = ~&i&&+ ~&t++ *=+ //:
  75. neither = ~&lNrZQ++ ~~
  76. not = ~&\&!+ ~&\0!
  77. ordered = ~&aatPBZ^!+ &&~&fatPR+ ~&ahthPX;
  78. pad = "p". ~&i&& ~&rSS+ zipp"p"^*D\~& leql$^
  79. psort = ~&?\~&! -<+ ~&at^?\~&ah +^(~&rlrE?\~&rl+ ~&l;+ ~&fatPR,^/~&+ ~&irlXX;+ ~~+ ~&ah)
  80. rlc = ~&a^&+ ~&at?\~&aNC+ ~&ahthPX;; \/? ~&/~&lrhPCrtPCahPfatPRXO ~&ahPNCfatPRC
  81. sep = ~&a^?\&!+ \/?=ah ~&/~&NfatPRC ~&lrhPCrtPCahPfatPRXO
  82. skipwhile = ->~&t+ ~&i&&+ ~&h;
  83. stochasm = ^H\~&+ mtwist..w_enum++ !+ * ^/~&m ~&n; %nI?\~& math..strtod+ ~&h+ %nP
  84. takewhile = ~&a^&+ &&~&ahPfatPRC+ ~&ah;
  85. words = "n". "a". ~&rlrK0liNCSPQ=>0 "a"!* iota"n"
  86. zipp = "p". ~&al^?(~&ar?/~&abh2fabt2RC *-"p"+ ~&al,~&ar&& "p"-*+ ~&ar)
  87. block =
  88. iota7?<(
  89. iota; ~&t?\~&iNCS! ~&a!*; ~&NiC|\; ^?^/-&&- ~&\~&aaNCB+ ^^(gang+ .\*&h,recur/&f+ ~&z),
  90. ~&a^&+ ~&alPfarPRC^J/~&f+ ~&a;+ //~&alrBPlrlPCrrPXarh2fabt2RXONarPXq+ 0!*+ iota)
  91. rep "n" =
  92. nleq$-+ <.
  93. "f". -++- "f"!* iota "n",
  94. "f". ~&/(0!* iota "n"); ~&l->r ^|\"f" ~&lt,
  95. "f". ~&/"n"; ~&l->r ^|\"f" ~&ahPatPNatPCBNNXfatPRCq>
  96. swin = # takes a number n to a function enumerating all length n sublists of a list
  97. iota8?< ~&\("n". ~&r->lx~&lht2rhPNCTlCrtPX+ ~&lNrXX/"n"; ~&lrrPB->rlxPNCrX ^|/~&ahPatPNatPCBNNXfatPRCq ~&rhPlCrtPX) -+
  98. +^(~&xSNX;+ ~&K7x++ ~+ --<0,0>+ ~&all2arlrPXPNfalrPrXPRXqNX*arPNfarPRXaNXqSxp,~&NiX;+ ~+ ~&=>&l+ ~&NiXS),
  99. ~&NNXiX; ~&r->lxt ^|/~&NhCiC ~&ahPatPNatPCBNNXfatPRCq+-
  100. next =
  101. ~&?(
  102. ~&ahPatPNatPCBNNXfatPRCq; "n". "f". "x". ~&x (rep"n" :^\~& "f"+ ~&h) <"x">,
  103. ! "f". "x". ~&wZ->tx(:^/"f"+~&h ~&) <"x">)
  104. lsm = # takes a set to its logarithmic-time membership predicate
  105. ~&?\0!! ^w/~&+ @NiX (leql/8+ length)^?ar\!@ar (&&^|\~& ~)=>^lrNCT(~&alryPlj,?^/~@alrz ~&fallrTPrGPW)^|J/~& -+
  106. ^(^|/~&l ~&a^& :/&+ ~&l?a/~&falPRiNXS ~&farPRNiXS,^H\~&lr !=+ ~@r),
  107. ^/~& @r (nleq+ weight~~)$-+ ^HZ(all_same+ ~@r,~&l)~|^/~& @alrBPfabbIPWNqK21 ~&a^?\<&>! ~&WliNXSPrNiXSPT+-
  108. # higher order
  109. gcase = # generalized case statement, takes a recognizing predicate to a case statement constructor
  110. ~&lZrB?^(
  111. ~&al^?\~&ar++ ?^\^(~&alhr,~&faltPrXPR)++ ~&alhl;++ !;++ ///+ ^;+ //+,
  112. ^\~&;+ ;;+ ;+ ~&al^?\-+~&r;,~&ar+-+ ?^\^(~&r;+ ~&alhr,~&faltPrXPR)+ ~&alhl;+ !;+ ~&/~&l;+ ^;+ //+)