]> gitweb.factorcode.org Git - factor.git/blob - extra/websites/factorcode/examples.txt
factor: Move math.ranges => ranges.
[factor.git] / extra / websites / factorcode / examples.txt
1 USING: io math sequences ;
2
3 "Hello world" print
4 10 [ "Hello, Factor" print ] times
5 "Hello, " "Factor" append print
6 ----
7 USING: io kernel sequences
8 http.client xml xml.data xml.traversal ;
9
10 "http://factorcode.org" http-get nip string>xml
11 "a" deep-tags-named
12 [ "href" attr ] map
13 [ print ] each
14 ----
15 USING: accessors kernel math math.constants
16 math.functions prettyprint ;
17
18 TUPLE: circle radius ;
19 TUPLE: rectangle width height ;
20
21 GENERIC: area ( shape -- area )
22 M: circle area radius>> sq pi * ;
23 M: rectangle area [ width>> ] [ height>> ] bi * ;
24
25 rectangle new 10 >>width 20 >>height area .
26 ----
27 USING: accessors smtp ;
28
29 <email>
30     "john@foobar.com" >>from
31     { "jane@foobar.com" } >>to
32     "Up for lunch?" >>subject
33     "At Tracy's." >>body
34 send-email
35 ----
36 USING: io.files io.encodings.utf8 kernel
37 sequences splitting ;
38
39 "table.txt" utf8 [
40     file-lines
41     [ "|" split ] map flip [ "|" join ] map
42 ] 2keep
43 set-file-lines
44 ----
45 USING: sequences xml.syntax xml.writer ;
46
47 { "three" "blind" "mice" }
48 [ [XML <li><-></li> XML] ] map
49 [XML <ul><-></ul> XML]
50 pprint-xml
51 ----
52 USING: inspector io.files.info
53 io.pathnames system tools.files ;
54
55 home directory.
56 home file-system-info free-space>> .
57 image-path file-info describe
58 ----
59 USING: io kernel sequences ;
60
61 4 <iota> [
62     "Happy Birthday " write
63     2 = "dear NAME" "to You" ? print
64 ] each
65 ----
66 USING: dice formatting ;
67
68 ROLL: 2d8+4
69 "You do %s points of damage!" printf
70 ----
71 USING: kernel rot13 ;
72
73 "Hello, world!" rot13
74 "Uryyb, jbeyq!" assert=
75 ----
76 USING: ascii tools.test ;
77
78 { "HELLO" } [ "HeLlO" >upper ] unit-test
79 { "Hello" } [ "HeLlO" >title ] unit-test
80 { "hello" } [ "HeLlO" >lower ] unit-test
81 ----
82 USING: io io.encodings.ascii io.files
83 random sequences splitting ;
84
85 "/usr/share/games/fortune/fortunes"
86 ascii file-lines { "%" } split random
87 [ print ] each
88 ----
89 USING: combinators io kernel locals
90 math.functions math.parser ranges
91 sequences ;
92
93 100 [1,b] [| i |
94     {
95         { [ i 15 divisor? ] [ "FizzBuzz" ] }
96         { [ i  3 divisor? ] [ "Fizz" ] }
97         { [ i  5 divisor? ] [ "Buzz" ] }
98         [ i number>string ]
99     } cond print
100 ] each
101 ----
102 USING: kernel math sequences text-to-speech ;
103
104 "factor"
105
106 dup [ "aeiou" member? ] find drop [
107     [ "way" append ]
108     [ cut swap "ay" 3append ] if-zero
109 ] when*
110
111 speak-text
112 ----
113 USING: math.text.english morse sequences ;
114
115 { 4 8 15 16 23 42 } [
116     number>text play-as-morse
117 ] each