Fix cookbook examples
parent
c0fb69f63b
commit
c992607677
|
@ -182,6 +182,7 @@ $nl
|
||||||
ARTICLE: "cookbook-io" "Input and output cookbook"
|
ARTICLE: "cookbook-io" "Input and output cookbook"
|
||||||
"Ask the user for their age, and print it back:"
|
"Ask the user for their age, and print it back:"
|
||||||
{ $code
|
{ $code
|
||||||
|
"USING: io math.parser ;"
|
||||||
": ask-age ( -- ) \"How old are you?\" print ;"
|
": ask-age ( -- ) \"How old are you?\" print ;"
|
||||||
": read-age ( -- n ) readln string>number ;"
|
": read-age ( -- n ) readln string>number ;"
|
||||||
": print-age ( n -- )"
|
": print-age ( n -- )"
|
||||||
|
@ -193,22 +194,26 @@ ARTICLE: "cookbook-io" "Input and output cookbook"
|
||||||
}
|
}
|
||||||
"Print the lines of a file in sorted order:"
|
"Print the lines of a file in sorted order:"
|
||||||
{ $code
|
{ $code
|
||||||
"utf8 \"lines.txt\" file-lines natural-sort [ print ] each"
|
"USING: io io.encodings.utf8 io.files sequences sorting ;"
|
||||||
|
"\"lines.txt\" utf8 file-lines natural-sort [ print ] each"
|
||||||
}
|
}
|
||||||
"Read 1024 bytes from a file:"
|
"Read 1024 bytes from a file:"
|
||||||
{ $code
|
{ $code
|
||||||
|
"USING: io io.encodings.binary io.files ;"
|
||||||
"\"data.bin\" binary [ 1024 read ] with-file-reader"
|
"\"data.bin\" binary [ 1024 read ] with-file-reader"
|
||||||
}
|
}
|
||||||
"Convert a file of 4-byte cells from little to big endian or vice versa, by directly mapping it into memory and operating on it with sequence words:"
|
"Convert a file of 4-byte cells from little to big endian or vice versa, by directly mapping it into memory and operating on it with sequence words:"
|
||||||
{ $code
|
{ $code
|
||||||
|
"USING: accessors grouping io.files io.mmap kernel sequences ;"
|
||||||
"\"mydata.dat\" dup file-info size>> ["
|
"\"mydata.dat\" dup file-info size>> ["
|
||||||
" 4 <sliced-groups> [ reverse-here ] change-each"
|
" 4 <sliced-groups> [ reverse-here ] change-each"
|
||||||
"] with-mapped-file"
|
"] with-mapped-file"
|
||||||
}
|
}
|
||||||
"Send some bytes to a remote host:"
|
"Send some bytes to a remote host:"
|
||||||
{ $code
|
{ $code
|
||||||
"\"myhost\" 1033 <inet>"
|
"USING: io io.encodings.ascii io.sockets strings ;"
|
||||||
"[ { 12 17 102 } >string write ] with-client"
|
"\"myhost\" 1033 <inet> ascii"
|
||||||
|
"[ B{ 12 17 102 } write ] with-client"
|
||||||
}
|
}
|
||||||
{ $references
|
{ $references
|
||||||
{ }
|
{ }
|
||||||
|
|
Loading…
Reference in New Issue