/core/quotations/quotations-docs.factor

http://github.com/abeaumont/factor · Factor · 79 lines · 68 code · 11 blank · 0 comment · 0 complexity · 14def7d9dcce2eae17367b4296e05cb2 MD5 · raw file

  1. USING: arrays help.markup help.syntax strings sbufs
  2. vectors kernel combinators ;
  3. IN: quotations
  4. ARTICLE: "quotations" "Quotations"
  5. "A quotation is an anonymous function (a value denoting a snippet of code) which can be used as a value and called using the " { $link "call" } "."
  6. $nl
  7. "Quotation literals appearing in source code are delimited by square brackets, for example " { $snippet "[ 2 + ]" } "; see " { $link "syntax-quots" } " for details on their syntax."
  8. $nl
  9. "Quotations form a class of objects:"
  10. { $subsections
  11. quotation
  12. quotation?
  13. }
  14. "A more general class is provided for methods to dispatch on that includes quotations, " { $link curry } ", and " { $link compose } " objects:"
  15. { $subsections
  16. callable
  17. }
  18. "Quotations evaluate sequentially from beginning to end. Literals are pushed on the stack and words are executed. Details can be found in " { $link "evaluator" } ". Words can be placed in wrappers to suppress execution:"
  19. { $subsections "wrappers" }
  20. "Quotations implement the " { $link "sequence-protocol" } ", and existing sequences can be converted into quotations:"
  21. { $subsections
  22. >quotation
  23. 1quotation
  24. }
  25. "Although quotations can be treated as sequences, the compiler will be unable to reason about quotations manipulated as sequences at runtime. " { $link "compositional-combinators" } " are provided for runtime partial application and composition of quotations." ;
  26. ARTICLE: "wrappers" "Wrappers"
  27. "Wrappers evaluate to the object being wrapped when encountered in code. They are used to suppress the execution of " { $link "words" } " so that they can be used as values."
  28. { $subsections
  29. wrapper
  30. literalize
  31. }
  32. "Wrapper literal syntax is documented in " { $link "syntax-words" } "."
  33. { $example
  34. "IN: scratchpad"
  35. "DEFER: my-word"
  36. "\\ my-word name>> ."
  37. "\"my-word\""
  38. }
  39. { $see-also "combinators" } ;
  40. ABOUT: "quotations"
  41. HELP: callable
  42. { $class-description "The class whose instances can be passed to " { $link call } ". This includes quotations and composed quotations built up with " { $link curry } " or " { $link compose } "." } ;
  43. HELP: quotation
  44. { $description "The class of quotations. See " { $link "syntax-quots" } " for syntax and " { $link "quotations" } " for general information." } ;
  45. HELP: >quotation
  46. { $values { "seq" "a sequence" } { "quot" quotation } }
  47. { $description "Outputs a freshly-allocated quotation with the same elements as a given sequence." } ;
  48. HELP: 1quotation
  49. { $values { "obj" object } { "quot" quotation } }
  50. { $description "Constructs a quotation holding one element." }
  51. { $notes
  52. "The following two phrases are equivalent:"
  53. { $code "\\ reverse execute" }
  54. { $code "\\ reverse 1quotation call" }
  55. } ;
  56. HELP: wrapper
  57. { $description "The class of wrappers. Wrappers are created by calling " { $link literalize } ". See " { $link "syntax-words" } " for syntax." } ;
  58. HELP: <wrapper>
  59. { $values { "obj" object } { "wrapper" wrapper } }
  60. { $description "Creates an object which pushes " { $snippet "obj" } " on the stack when evaluated. User code should call " { $link literalize } " instead, since it avoids wrapping self-evaluating objects (which is redundant)." } ;
  61. HELP: literalize
  62. { $values { "obj" object } { "wrapped" object } }
  63. { $description "Outputs an object which evaluates to " { $snippet "obj" } " when placed in a quotation. If " { $snippet "obj" } " is not self-evaluating (for example, it is a word), then it will be wrapped." }
  64. { $examples
  65. { $example "USING: prettyprint quotations ;" "5 literalize ." "5" }
  66. { $example "USING: math prettyprint quotations sequences ;" "[ + ] [ literalize ] map ." "[ \\ + ]" }
  67. } ;
  68. { literalize curry <wrapper> POSTPONE: \ POSTPONE: W{ } related-words