Format: Difference between revisions

From APL Wiki
Jump to navigation Jump to search
(Created page with "{{Built-in|Format|⍕}} is a primitive function. == Examples == Format in its monadic form, alongside ravel, allows the user to concatenate numbers with strings:...")
 
m (Text replacement - "</source>" to "</syntaxhighlight>")
 
(9 intermediate revisions by 4 users not shown)
Line 1: Line 1:
{{Built-in|Format|⍕}} is a [[primitive function]].
{{Built-in|Format|⍕}} is an [[ambivalent]] [[primitive function]] which formats the right [[argument]] into a [[simple]] [[character]] array, optionally following the specification supplied as the left argument. The usage of the left argument varies across implementations.


== Examples ==
== Examples ==


Format in its [[monadic]] form, alongside [[ravel]], allows the user to concatenate numbers with strings:
=== Monadic form ===
<source lang=apl>
 
Format in its [[monadic]] form allows the user to convert [[Array model|arrays]] of any [[type]] into simple character arrays (for example for concatenation with other character arrays). The result usually matches the interpreter's default display, either by <syntaxhighlight lang=apl inline>⎕←</syntaxhighlight> or by REPL.
 
<syntaxhighlight lang=apl>
       supper ← 10
       supper ← 10
       'I ate ',⍕supper,'shrimp.'
       'I ate ',(⍕supper),' shrimp.'
I ate 10 shrimp.
I ate 10 shrimp.
</source>
 
It is very powerful when combined with [[execute]], which allows the user to interpret a string as APL code:
      ⎕←DATA←(⍳3) (2 2⍴⍳4) 'TEXT' 100
<source lang=apl>
1 2 3  1 2  TEXT 100
      data ← 7
        3 4
      ⍎'6 × ',⍕data
      ⍕DATA
42
1 2 3  1 2  TEXT 100
</source>
        3 4
      (⍴DATA) (⍴⍕DATA)
4  2 22
</syntaxhighlight>
 
=== Dyadic form ===
 
The [[dyadic]] form of Format varies across implementations.
 
[[Dyalog APL]] and [[NARS2000]] support column width and the number of decimal places for formatting [[numeric]] arrays. The following example formats the 2-by-3 array with 12 spaces per column, rounded to 2 decimal places:
 
<syntaxhighlight lang=apl>
      ⎕←C←2 3⍴ 32.10958 0 ¯101.4914 ¯99.40878 ¯101.872 1001.48173
32.10958    0    ¯101.4914
¯99.40878 ¯101.872 1001.48173
      ⍴C
2 3
 
      12 2⍕C
      32.11        0.00    ¯101.49
      ¯99.41    ¯101.87    1001.48
      ⍴(12 2⍕C)
2 36
</syntaxhighlight>{{Works in|[[Dyalog APL]], [[NARS2000]]}}
 
[[APLX]] supports '''Format by example''', which uses string left argument for a rich set of format specifications:
 
<syntaxhighlight lang=apl>
      '55.55' ⍕22.234 1.398 11.00
22.23 1.4 11
      '55.55 5.555 55.55 55'⍕22.234 1.398 0.00 11.0
22.23 1.398      11
      '555,555,555.55'⍕1234567.89
  1,234,567.89
</syntaxhighlight>{{Works in|[[APLX]]}}
 
== See also ==
* [[Execute]]
 
== External links ==
=== Tutorials ===
* [https://chat.stackexchange.com/rooms/52405/conversation/lesson-10-apl-functions--#41869169 APL Cultivation]
=== Documentation ===
 
* Dyalog [https://help.dyalog.com/latest/#Language/Primitive%20Functions/Format%20Monadic.htm monadic], [https://help.dyalog.com/latest/#Language/Primitive%20Functions/Format%20Dyadic.htm dyadic]
* [http://wiki.nars2000.org/index.php/Symbol_Format NARS2000]
* APLX [http://microapl.com/apl_help/ch_020_020_680.htm monadic], [http://microapl.com/apl_help/ch_020_020_690.htm by specification], [http://microapl.com/apl_help/ch_020_020_700.htm by example]
* J [https://www.jsoftware.com/help/dictionary/d602.htm Dictionary], [https://code.jsoftware.com/wiki/Vocabulary/quoteco NuVoc]
 
{{APL built-ins}}[[Category:Primitive functions]]

Latest revision as of 21:02, 10 September 2022

Format () is an ambivalent primitive function which formats the right argument into a simple character array, optionally following the specification supplied as the left argument. The usage of the left argument varies across implementations.

Examples

Monadic form

Format in its monadic form allows the user to convert arrays of any type into simple character arrays (for example for concatenation with other character arrays). The result usually matches the interpreter's default display, either by ⎕← or by REPL.

      supper ← 10
      'I ate ',(⍕supper),' shrimp.'
I ate 10 shrimp.

      ⎕←DATA←(⍳3) (2 2⍴⍳4) 'TEXT' 100
 1 2 3   1 2   TEXT 100
         3 4
      ⍕DATA
 1 2 3   1 2   TEXT 100
         3 4
      (⍴DATA) (⍴⍕DATA)
 4  2 22

Dyadic form

The dyadic form of Format varies across implementations.

Dyalog APL and NARS2000 support column width and the number of decimal places for formatting numeric arrays. The following example formats the 2-by-3 array with 12 spaces per column, rounded to 2 decimal places:

      ⎕←C←2 3⍴ 32.10958 0 ¯101.4914 ¯99.40878 ¯101.872 1001.48173
 32.10958    0     ¯101.4914 
¯99.40878 ¯101.872 1001.48173
      ⍴C
2 3

      12 2⍕C
       32.11        0.00     ¯101.49
      ¯99.41     ¯101.87     1001.48
      ⍴(12 2⍕C)
2 36
Works in: Dyalog APL, NARS2000

APLX supports Format by example, which uses string left argument for a rich set of format specifications:

      '55.55' ⍕22.234 1.398 11.00
22.23 1.4 11
      '55.55 5.555 55.55 55'⍕22.234 1.398 0.00 11.0
22.23 1.398       11
      '555,555,555.55'⍕1234567.89
  1,234,567.89
Works in: APLX

See also

External links

Tutorials

Documentation


APL built-ins [edit]
Primitives (Timeline) Functions
Scalar
Monadic ConjugateNegateSignumReciprocalMagnitudeExponentialNatural LogarithmFloorCeilingFactorialNotPi TimesRollTypeImaginarySquare Root
Dyadic AddSubtractTimesDivideResiduePowerLogarithmMinimumMaximumBinomialComparison functionsBoolean functions (And, Or, Nand, Nor) ∙ GCDLCMCircularComplexRoot
Non-Scalar
Structural ShapeReshapeTallyDepthRavelEnlistTableCatenateReverseRotateTransposeRazeMixSplitEncloseNestCut (K)PairLinkPartitioned EnclosePartition
Selection FirstPickTakeDropUniqueIdentityStopSelectReplicateExpandSet functions (IntersectionUnionWithout) ∙ Bracket indexingIndexCartesian ProductSort
Selector Index generatorGradeIndex OfInterval IndexIndicesDealPrefix and suffix vectors
Computational MatchNot MatchMembershipFindNub SieveEncodeDecodeMatrix InverseMatrix DivideFormatExecuteMaterialiseRange
Operators Monadic EachCommuteConstantReplicateExpandReduceWindowed ReduceScanOuter ProductKeyI-BeamSpawnFunction axis
Dyadic BindCompositions (Compose, Reverse Compose, Beside, Withe, Atop, Over) ∙ Inner ProductDeterminantPowerAtUnderRankDepthVariantStencilCutDirect definition (operator)
Quad names Index originComparison toleranceMigration levelAtomic vector