Skip to content

Commit 3fe0998

Browse files
committed
Go over Read documentation
* Mention Hold[Expression] in list of forms * Given example of Read[x, Hold[Expression]] when a comment is read.
1 parent 4c1796c commit 3fe0998

1 file changed

Lines changed: 28 additions & 10 deletions

File tree

mathics/builtin/files_io/files.py

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -238,18 +238,24 @@ class Read(Builtin):
238238
<dt>'Read[$stream$, $type$]'
239239
<dd>reads the input stream and returns an object of the given type.
240240
241+
<dt>'Read[$stream$, $type$]'
242+
<dd>reads the input stream and returns an object of the given type.
243+
244+
<dt>'Read[$stream$, Hold[Expression]]'
245+
<dd>reads the input stream for an Expression and puts it inside 'Hold'.
246+
241247
</dl>
242248
$type$ is one of:
243249
<ul>
244-
<li>Byte</li>
245-
<li>Character</li>
246-
<li>Expression</li>
247-
<li>HoldExpression</li>
248-
<li>Number</li>
249-
<li>Real</li>
250-
<li>Record</li>
251-
<li>String</li>
252-
<li>Word</li>
250+
<li>Byte
251+
<li>Character
252+
<li>Expression
253+
<li>HoldExpression
254+
<li>Number
255+
<li>Real
256+
<li>Record
257+
<li>String
258+
<li>Word
253259
</ul>
254260
255261
## Malformed InputString
@@ -332,12 +338,24 @@ class Read(Builtin):
332338
333339
## HoldExpression:
334340
>> stream = StringToStream["2+2\\n2+3"];
341+
342+
'Read' with a 'Hold[Expression]' returns the expression it reads unevaluated so it can be later inspected and evaluated:
343+
335344
>> Read[stream, Hold[Expression]]
336345
= Hold[2 + 2]
346+
337347
>> Read[stream, Expression]
338348
= 5
339349
>> Close[stream];
340350
351+
Reading a comment however will return the empy list:
352+
>> stream = StringToStream["(* ::Package:: *)"];
353+
354+
>> Read[stream, Hold[Expression]]
355+
= {}
356+
357+
>> Close[stream];
358+
341359
## Multiple types
342360
>> stream = StringToStream["123 abc"];
343361
>> Read[stream, {Number, Word}]
@@ -629,7 +647,7 @@ def reader(stream, word_separators, accepted=None):
629647
expr = Expression("Hold", expr)
630648
result.append(expr)
631649
# else:
632-
# TO: Supposedly we can't get here
650+
# TODO: Supposedly we can't get here
633651
# what code should we put here?
634652

635653
elif typ == Symbol("Number"):

0 commit comments

Comments
 (0)