REBOL in One Line (R2 One-liners)

REBOL has a long history of one-liner programs.

One-liners are like Haiku poetry. Not always practical, not always readable, they are fun to write. As with any art, some are better than others, but from time-to-time, a true gem pops up.

Here are 45 example one-liners that were submitted by REBOL users.

Notes:

To try one, just cut and paste the code into the REBOL console. How long is a line? Here, we limited users to 132 chars. It's the old line printer standard. Some examples can be made even smaller. We did not edit or optimize them.

This page has been reformatted, if you find errors, please contact us.

1 - Print web page source

print read http://www.rebol.com

Summary: Very simple. Prints to the console the HTML source for a web page. Author(s): RT Length: 31 characters

2 - Open a GUI, read web page, sent it as email

view layout [u: field "user@rebol.com" h: field "http://" btn "Send" [send to-email u/text read to-url h/text alert "Sent"]]

Summary: This example opens a window that displays a GUI form. You can input an email address and a web page URL. REBOL then sends the contents of that web page to that email address. Note that you must have your SMTP email configured in REBOL (in View's User preferences). Author(s): Carl Sassenrath Length: 125 characters

3 - FTP upload all files

foreach file load %./ [if not dir? file [write/binary join ftp://user:pass@example.com/ file read/binary file]]

Summary: Uploads all the files in a directory using FTP. Files can be text, images, web pages, anything... but not directories. Author(s): RT Length: 111 characters

4 - TCP port scanner

repeat n 100 [if not error? try [close open probe join tcp://localhost: n] [print [n "is open"]]]

Summary: This is a simple port scanner. Given a TCP address, it will tell you which of the first 100 ports are accessible. The address can be a host name or number. The localhost name will scan ports on your own machine. You can scan more ports by increasing the number (from 100), or you can scan ranges by using a FOR loop rather than REPEAT. Author(s): Anonymous Length: 98 characters

5 - Remove last item of a series (list, string, etc.)

remove back tail list

Summary: This line removes the last item from a block, string, or any series. It is more of an idiom, but worth posting. Author(s): RT Length: 21 characters

6 - Save web page text to a file

remove-each tag page: load/markup http://www.rebol.com [tag? tag] write %page.txt page

Summary: This line reads a web page, strips all its tags (leaving just the text) and writes it to a file called page.txt. Note: requires newer releases of REBOL. Author(s): Carl Sassenrath Length: 87 characters

7 - One Line Quine

REBOL[] do a: {view layout[t: field 500 rejoin["REBOL[] do a: {" a "}"] btn "do" [do do t/text]]}

Summary: creates a self modifying window. (includes the REBOL Header.) Author(s): Ammon Johnson Length: 97 characters

8 - Digital Clock

view layout[origin 0 t: h1 red black (to string! now/time) rate 1 feel[engage: [t/text: now/time show t]]]

Summary: Displays a digital clock. Author(s): Ammon Johnson Length: 106 characters Related: #7

9 - Print web server ID

p: open http://www.rebol.com:80 print p/locals/headers/server close p

Summary: Prints the name and version of a websites server. Author(s): Sunanda Length: 69 characters

10 - Push

Push: func[Stack[series!]Value /Only][head either Only[insert/only Stack :Value][insert Stack :Value]]

Summary: Inserts a value into a series and returns the series head. Author(s): Andrew Martin Length: 102 characters

11 - Pop

Pop: function[Stack[series!]][Value][Value: pick Stack 1 remove Stack :Value]

Summary: Returns the first value in a series and removes it from the series. Author(s): Andrew Martin Length: 77 characters Related: #10

12 - Short One Line Quine With GUI

REBOL[] do a: {view layout[field 500 rejoin["REBOL[] do a: {" a "}"]]}

Summary: A quine is a program that outputs a copy of its own source. Yes, very pointless. (: (Ammon Johnson's quine is an enhancement of this one.) Author(s): Carl Read Length: 70 characters Related: #7

13 - Rotate a series left

ROL: func ["Rotate Left" s [series!] n [integer!]][n: n // length? s remove/part append s copy/part s n n]

Summary: Rotate a series left by n positions. Author(s): Gregg Irwin Length: 108 characters

14 - Determine if A is a subset of B

subset?: func [set1 [series! bitset!] set2 [series! bitset!]][either empty? difference set1 intersect set1 set2 [true][false]]

Summary: Tells you if set1 is a subset of set2. Works for both series! and bitset! values. Author(s): Gregg Irwin Length: 126 characters

15 - Bye

view l: layout [image red "bye" font-size 50 rate 9 feel [engage: func[f a e][if a = 'time[f/text: "" f/image: to-image l show f]]]]

Summary: A red box with "bye" written on it disappears into the distance. Author(s): Anton Length: 132 characters

16 - Monty Hall Challenge

w: 100 loop w[w: w - any random[0 0 1]]

Summary: Simulates swapping doors after Monty shows a goat. Result is percentage wins after 100 runs. (MHC: a well-known and often debated probability puzzle involving three doors, two goats, and a car). Author(s): Sunanda (with tweaks from Romano T, Ryan C, Carl R, and Reichart) Length: 39 characters

17 - Surprise Message

random/seed pick sig: {B aRt hcJnetrhuEOkL!eraso} 21 random sig

Summary: Outputs a REBOLious signature. (The HTML is chewing up the second space after the B, you may need to re-add it before running) Author(s): Allen Kamp Length: 64 characters

18 - Show the actual dir or changes it

cd: func [d [unset! file!]][if value? 'd [change-dir d] what-dir]

Summary: Used alone, return the actual dir. Used with a dir name, changes the actual dir and return the new dir. Author(s): Romano Paolo Tenca Length: 65 characters

19 - Console history

hist: does [repeat k system/console/history [print [";" k]]]

Summary: Print the console history. Author(s): Romano Paolo Tenca Length: 60 characters

20 - NewProbe

newprobe: func [value][help value :value]

Summary: Requires last beta versions. A more usable probe. Try: view newprobe layout [???button] in oneliners.txt Author(s): Romano Paolo Tenca Length: 41 characters

21 - Random string

copy/part random "abcdefghijklmnop" 6

Summary: Creates a 6 character random string to use for passwords or whatever. Author(s): Anonymous Length: 37 characters

22 - Display REBOL logo

view center-face layout [image http://www.rebol.com/graphics/reb-logo.gif [unview]]

Summary: This one line a) downloads REBOL logo from the web, b) opens a window centered on screen, c) displays the logo, and d) closes the window when you click on the logo. Author(s): Anonymous Length: 83 characters

23 - Save history to file

write/lines %hist.r head reverse copy rebol/console/history

Summary: Takes the history of input into the console and prints it out, line by line, to a file. This way you can paste in functions and whole scripts, and save them for viewing and editing. Sorts it to display the oldest commands at the top. Author(s): Saw it on the ML a while ago Length: 59 characters Related: #19

24 - Remove all occurrences of a value from a series

while [ find list item ] [ remove find list item ]

Summary: This line removes all occurrences of a given value from a block, a string or any serie. Very simple but may save some time for a beginner. Shows how to use remove with find. Author(s): Laurent Chevalier Length: 50 characters Related: #5

25 - Remove all occurrences of a value from a series

until [ not remove find list item ]

Summary: This line removes all occurrences of a given value from a block, a string or any serie. A bit cleaner than one-liner 24. Author(s): Laurent Chevalier Length: 35 characters Related: #24

26 - Log

Log: func [Value /Clear][if Clear[delete %Log.txt]write/append/lines %Log.txt reform[now/time mold :Value]:Value]

Summary: Logs Rebol values to a file named %Log.txt. I use 'Log to help debug CGI scripts. Use 'Log like: Log/clear now ; Start a new log file.

; What's the CGI query string contents?

Log Rebol/options/cgi/query-string Author(s): Andrew Martin Length: 113 characters

27 - Weekday?

Weekday?: func[Date[date!]][pick system/locale/days Date/weekday]

Summary: Returns the weekday of the date. Use 'Weekday? like: Weekday? 25/Dec/2002 ; == "Wednesday" Author(s): Andrew Martin Length: 65 characters

28 - D6

D6: does [random 6]

Summary: D6 returns a random number from 1 to 6, just like rolling a six-sided dice. Make sure to seed the random number generator first! Like: random/seed now Author(s): Andrew Martin Length: 19 characters

29 - Leapyear?

Leapyear?: function[Date[date!]][Year][Year: Date/year any[all[0 = remainder Year 4 0 <> remainder Year 100]0 = remainder Year 400]]

Summary: 'Leapyear? returns 'true for date values that are leapyears. For example: >> leapyear? 1/1/2001

>> leapyear? 1/1/2000 Author(s): Andrew Martin Length: 132 characters

30 - clear-me

view f: layout for n 0 19 1[r:(random 19)+ n // 20 append[across]load rejoin["a"n": check on[a"r"/data: a"r"/data xor on show f]"]]

Summary: A one-line game. A line of twenty boxes appear, each with a cross on, the object being to remove all the crosses. Clicking on a box will toggle it and one other off and on. Sometimes the line is easy to clear, and sometimes not. And no, I don't know if sometimes it's impossible... Author(s): Carl Read Length: 131 characters

31 - Paint Drops

view layout[b: box rate 9 effect[draw[pen(random snow)circle(random 99x99)2]blur]box 1x1 rate 9 effect[draw[(b/image: to-image b)]]]

Summary: A surface is filled with colored drops. Author(s): Vincent Ecuyer Length: 132 characters Related: #15

32 - Picture Viewer

view l: layout[origin 0x0 b: box "Load" [error? try [b/image: i: load first request-file b/text: "" l/size: b/size: i/size show l]]]

Summary: Displays a picture in a window. Click on it to select another one. Author(s): Vincent Ecuyer Length: 132 characters

33 - Daytime Server

s: open tcp://:13 forever [close insert first s to-idate now]

Summary: Runs a Daytime server daemon. Use: read daytime://localhost to connect to the server and retrieve the current server date and time. Author(s): DocKimbel Length: 61 characters

34 - Send emails with a graphic interface

view layout [e: field "Email" s: field "Subject" m: area "Body" btn "Send"[send/subject to-email e/text m/text s/text alert "ok"]]

Summary: Simple GUI for email sending. Displays a small "ok" window when email is sent. Author(s): DocKimbel Length: 130 characters

35 - Morse encoder

foreach c ask{Message:}[l: index? find{ etinamsdrgukwohblzfcpövxäqüyj}c while[l >= 2][prin pick{-.}odd? l l: l / 2]prin{ }]print{}

Summary: Encodes a sentence into morse code. This version is a bit suboptimal so that the html generator won't destroy it. Author(s): Johan Rönnblom Length: 130 characters

36 - Chess Board

g:[style b box black 50x50 style w b white space 0x0]loop 8[append g head reverse/part [b w b w b w b w return]8]view layout g

Summary: Displays an empty chess board Author(s): Sunanda Length: 126 characters

37 - Prime factors

f: func[n][m: 2 s: 1 w: :append a: copy[]until[either n // m = 0[n: n / m w a m][m: m + s s: 2]if 1. * m *m > n[w a n n: 1]n = 1]a]

Summary: Defines a function, f, that returns a block of the prime factors of any integer up to about 9'999'999'999'999'999 (after than decimal rounding starts to made things a bit arbitrary) -- Exampl f

Returns: [???3] in oneliners.txt -- Bugs: f 0 and f 2 are a bit dodgy. Author(s): Sunanda Length: 132 characters

38 - Convert all .bmp files in a directory to .png

foreach file load %. [if find file %.bmp [save/png replace copy file %.bmp %.png load file]]

Summary: Reads all .bmp files in a directory and saves them out as .png files. Author(s): Bohdan "Bo" Lechnowsky Length: 92 characters

39 - Convert Images to REBOL Script

foreach f load %./ [if find [%.png %.jpg %.bmp] suffix? f [write/append %images.r reform [mold f mold read/binary f newline]]]

Summary: This line converts all your PNG, JPG, and BMP image files into a REBOL formatted file for including them in a script. (For base64 encoding, set system/options/binary-base: 64) Author(s): Anonymous Length: 126 characters

40 - Rule 110 Cellular Automaton

s:{..1..}loop 38[t: copy{..}forall s[if s/3[append t pick{.1}none? find{..1.11.}copy/part s 3]]print s: append t{..}]

Summary: Shows some steps in the evolution of a Turing complete cellular automaton from a single marked cell. Replace {..1.11.} with some other expression, like {..11..} or {.1..11}, and what do you have? Rule 90, and rule 30, respectively. Author(s): Errru Length: 117 characters

41 - get decimal length of a number

nfrac: func [d][length? second parse join d ".." "."]

Summary: nfrac 33 => 0 ; nfrac "456" => 0 ; nfrac 0.2104 => 4 ; nfrac "1256.63" => 2 ; nfrac 1.0 => 0 ; Author(s): collective contribution on www.codeur.org/forum/forum.php?theme=17 Length: 53 characters

42 - Print links from Web Page

parse read http://www.rebol.com [any [thru "A HREF=" copy link to ">" (print link)] to end]

Summary: Load a web page, parse it to extract and print all links found Author(s): Christophe Coussement Length: 91 characters

43 - DO history

dohist: does [d: copy system/console/history forall d [print rejoin [index? d " : " first d]] do pick head d to-integer ask "->"]

Summary: Allow you to use the history just by index. Author(s): Fabrice Vado Length: 129 characters Related: #19 #23

44 - diapo.r

REBOL [] d: read %. foreach f d [if find f ".jpg" [unview/all view/new/title layout [image f [q]] to-string f wait 4]]

Summary: shows all jpg of the script's directory Author(s): Jean-Nicolas MERVILLE Length: 118 characters Related: #32 #38

45 - pi.r

rebol[] s: 1 i: 0 forever [i: i + 2 s: s * (i / (i - 1) * i / (i + 1)) print 2 * S]

Summary: Approximate PI with John Wallis formula. Precision limited to 15 digits due to REBOL. Just press ESC when fed up ;-) Formula from http://www.chez.com/algor/math/pi.htm Author(s): Jean-Nicolas MERVILLE Length: 83 characters

46 - One Line Paint Program

view layout[s: box black 400x400 feel[engage: func[f a e][if find[down over]a[append s/effect/draw e/offset show s]if a = 'up[append s/effect/draw 'line]]]effect[draw[line]]]

Summary: Author of the REBOL Absolute Beginners tutorial at musiclessonz.com submitted this single line script that lets you draw lines in a window. Author(s): Nick Antonaccio

47 - Web site editor

view layout[f: field btn"Edit"[editor to-url f/text]]