! @@SKIP@@ from the typical "a = b(x)" of other programming languages.
: string "\\n" ;
: string "Jon 'Maddog' Orwant" ;
: string "\n" ; ! a "newline" character
: string "Jon \"Maddog\" Orwant" ;
USE: multiline
STRING: a
This is a multiline document
terminated by ; on a line by itself
;
: a
<" This is a multiline document
terminated by double quotes plus greater sign"> ;
from to str subseq
1 3 "abcdefg" subseq .
! "bc"
str n tail
1 "abcdefg" tail .
! "bcdefg"
newstr from to str replace-slice
"1234" 3 7 "abcdefg" [ replace-slice ] keep .
!"abc1234"
: replace-tail
dup length swap [ replace-slice ] keep ;
"1234" 3 "abcdefg" replace-tail .
! "abc1234"
USE: splitting
5 group
"abcdefghijklmnopqrstuvwxyz" 5 group .
! { "abcde" "fghij" "klmno" "pqrst" "uvwxy" "z" }
1 group
"abcd" 1 group .
! { "a" "b" "c" "d" }
: str "This is what you have" ;
0 1 str subseq ! "T" (on top of stack)
str 1 head ! "T"
5 7 str subseq ! "is"
str 13 tail ! "you have"
str 1 tail* ! "e"
str 4 tail* ! "have"
str length 8 - dup 3 + str subseq ! "you"
USE: locals
:: chars-from-rindex
str length from - dup count + str subseq ;
8 3 str chars-from-rindex ! "you"
SYMBOL: str
"This is what you have" str set
str get print
str get
! Top of stack: "This is what you have"
[ 5 head ] [ 7 tail ] bi "wasn't" swap 3append ! change "is" to "wasn't"
! Top of stack: "This wasn't what you have"
13 head "ondrous" append
! Top of stack: "This wasn't wondrous"
1 tail
! Top of stack: "his wasn't wondrous"
10 head*
! Top of stack: "his wasn'"
str set
! str = "his wasn'"
str get print
! you can test substrings with "subseq?"
10 tail* "substring" swap subseq?
[ "\"substring\" within the last 10 characters" print ]
when
! substitute "at" for "is", restricted to first five characters
USING: peg peg.search ;
5 cut >r "is" token [ drop "at" ] action replace r> append
"make a hat"
1 cut 1 cut* spin 3append
print
swap
! The "csv" vocabulary allows to parse real CSV
! reading the file "testdata.csv", UTF-8 encoded, CSV format
USE: csv
"testdata.csv" utf8 <file-reader> csv
! You could use the word "<string-reader>" to turn a string into a stream.
USING: csv io.streams.string ;
"XYZZY,\"\",\"O'Reilly, Inc\",\"Wall, Larry\",\"a \"\"glug\"\" bit,\",5,\"Error, Core Dumped\""
<string-reader> csv .
! "XYZZY"
! ""
! "O'Reilly, Inc"
! "Wall, Larry"
! "a \"glug\" bit,"
! "5"
! "Error, Core Dumped"
! All examples use the "calendar" vocabulary
USE: calendar
[ "Today is day " % now day-of-year # " of the current year." % ] "" make print
now
day>>
! Formatting the date with "calender.format" vocabulary
USE: calendar.format
now timestamp>rfc3339
! Top of stack: "2008-05-11T22:39:01+02:00"
now timestamp>ymd
! Top of stack: "2008-05-11"
now timestamp>millis
1000 / >integer
gmt timestamp>millis
1210538827773 millis>timestamp
! Durations can be added and substracted with the words "time+" and "time-"
now 5 days time+ timestamp>ymd print
now 5 days time- timestamp>ymd print
1973 1 18 3 45 50 gmt-offset-duration <timestamp>
0 0 55 2 17 5 <duration>
time+
timestamp>string "Then is " swap append print
"Nat was 55 days old on: " 1973 1 18 <date> 55 days time+ timestamp>ymd append print
! Timestamps can be substracted from each other with "time-". You get a duration,
now 5 days time-
now 5 days time+
time-
dt>days .
now
1969 7 20 20 17 40 instant <timestamp>
time-
USING: calendar math.parser ;
1981 6 16 4 35 25 instant <timestamp>
1973 1 18 3 45 50 instant <timestamp>
time-
dt>seconds
dup
[ "There were " % # " seconds between Nat and Bree" % ] "" make print
[ dup 60 mod dup ,
- 60 /
dup 60 mod dup ,
- 60 /
dup 24 mod dup ,
- 24 /
dup 7 mod dup ,
- 7 / ,
] { } make
[ [ "(" % # " weeks, " % # " days, " % # ":" % # ":" % # ")" % ] "" make ] with-datastack
first print
1981 6 16 <date>
1973 1 18 <date>
time-
dt>days
[ "There were " % # " days between Nat and Bree" % ] "" make print