;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; LL2 Talk ;; --- Ken Anderson and Tim Hickey ;; Saturday 11/9/2002 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;(define starttime (Date.)) (let ( (ses (.getSession request)) (page (.getParameter request "page")) ) ; (define starttime (Date.)) ; (tryCatch starttime (lambda(e) (Date.)))) (define (trs C rows) (map (lambda (row) {
[x]
}) (define default-style " ") (define print-style " ") (define the-style (let ((t (.getAttribute ses "the-style"))) (if (equal? t #null) default-style t))) (define (td class) (lambda(x) {Small Powerful Lightweight Language (JScheme) + Huge Industrial-strength Libraries (jdk1.4 libraries) = The Ultimate Lightweight Scripting Language
quote, begin, if, or, and, set!, define, lambda,
cond, let, let*, letrec, case, quasiquote,
tryCatch, define-macro, define-method, ...
| (.println X Y) | "." at beginning | ==> instance method | (Math.sin 0.0) | "." in middle only | ==> static method | (JButton. S) | "." at end | ==> constructor | System.out$ | "$" at end | ==> static field |
| (.first$ x 5) | initial ".", terminal "$" | instance field |
// Java
System.out.println(new JButton("Hello").toString());
;; JScheme (.println System.out$ (.toString (JButton. "Hello")))
//Java
public static ResultSet readData (String url) \{
try \{
return (ResultSet)(new SaxHandler().read(
new BufferedReader
(new InputStreamReader
((new URL(url)).openStream()))));
\} catch (IOException e2) \{
e2.printStackTrace();
return null; \} \}
//Java
public static ResultSet readData (String url) \{
try \{
return (ResultSet)(new SaxHandler().read(
new BufferedReader
(new InputStreamReader
((new URL(url)).openStream()))));
\} catch (IOException e2) \{
e2.printStackTrace();
return null; \} \}
;; JScheme (define (readData url) (.read (SaxHandler.) (BufferedReader.(InputStreamReader.(.openStream(URL. url))))))
//Java
public static ResultSet readData (String url) \{
try \{
return (ResultSet)(new SaxHandler().read(
new BufferedReader
(new InputStreamReader
((new URL(url)).openStream()))));
\} catch (IOException e2) \{
e2.printStackTrace();
return null; \} \}
;; JScheme (define (readData url) (.read (SaxHandler.) (BufferedReader.(InputStreamReader.(.openStream(URL. url))))))no static typing
;; Java
final JFrame w = new JFrame("Test");
JButton eb = new JButton("Exit");
eb.setBackground(Color.red);
eb.addActionListener(new ActionListener()\{
public void actionPerformed(ActionEvent e) \{
System.exit(0);\}\});
w.getContentPane().add(eb);
w.pack(); w.show();
;; JScheme (import "javax.swing.*") (define w (JFrame. "Test")) (define eb (JButton. "Exit")) (.setBackground eb Color.red$) (.addActionListener eb (Listener. (lambda(e) (System.exit 0)))) (.add (.getContentPane w) eb) (.pack w) (.show w)
(load "jlib/Swing.scm") ;a little functional language for GUI creation
(define w
(window "Test"
(button "exit" red
(action(lambda(e)(exit))))))
(.pack w)
(.show w)
Resulting code is much simpler than straight JScheme + javadot
(import "javax.swing.*") (define w (JFrame. "Test")) (define eb (JButton. "Exit")) (.setBackground eb Color.red$) (.addActionListener eb (Listener. (lambda(e) (System.exit 0)))) (.add (.getContentPane w) eb) (.pack w) (.show w)
# in Jython
import javax
from javax import swing
def exit(e)
java.lang.System.exit(0)
w=javax.swing.JFrame(title="Test")
p=top_frame.getContentPane()
eb = swing.JButton(label="Exit", background=Color.red, actionperformed=exit)
p.add(eb)
w.pack()
w.visible=1
#! /usr/bin/perl5 -w
# from http://pmwww.cs.vu.nl/documentation/Tk/UserGuide.html
use strict;
use Tk;
my $main = MainWindow->new;
$main->Button(-text => 'Exit',
-command => \[$main => 'destroy'\]
)->pack;
MainLoop;
Small Powerful Lightweight Language (Scheme) + Huge Industrial-strength Libraries (javadot+jdk1.4 libraries) !== the Ultimate Scripting Language
Small Powerful Lightweight Language (Scheme) + Huge Industrial-strength Libraries (javadot+ jdk1.4 libraries) + Little Lightweight Languages interfacing to libraries = The Ultimate Lightweight Scripting Language (???)
What makes a good lightweight scripting library? What do various scripting libraries have in common? Where does lightness come from?
JLIB for building GUIs SERVLET for writing simple servlets RUN for simple shell scripts
;; JLIB
(define w
(window "Test"
(button "exit" red
(action(lambda(e)(exit))))))
(.pack w) (.show w)
instead of
;; JScheme (define w (JFrame. "Test")) (define eb (JButton. "Exit")) (.setBackground eb Color.red$) (.addActionListener eb (Listener. (lambda(e) (System.exit 0)))) (.add (.getContentPane w) eb) (.pack w) (.show w)
(define b
(button "abc" ;; string is button label
(color 255 80 10) ;; background color
(action (lambda(e) (.hide w))) ;; action function
(HelveticaBold 24) ;; font
(lambda(t) (.setForeground t blue)) ;; general property setting
)
variable=value pairs in arg list-property ==> value pairs in arg list
(load "jlib/Swing.scm")
(define w
(window "sorry"
(menubar
(menu "File"
(menuitem "quit" red
(action (lambda(e) (.hide w))))))
(border
(north
(label "Join Request Refused" (HelveticaBold 24)))
(center
(scrollpane
(textarea 5 30 (TimesRoman 18) yellow
"Your request to join the group\\nhas been denied")))
(south
(button "OK"
(action (lambda(e) (.hide w))))))))
(.pack w) (.show w)
\{7 * 8 = \[(* 7 8)\]\}
; generate HTML histogram table from results of DB query
(define (histogram field table)
(define results
(dbquery
\{select \[field\],count(*) from \[table\]
group by \[field\], order by \[field\] asc\}))
\{<html><body>
<h1>Query results</h1>
The result of query <pre>\[s\]</pre> is
<table> \[(trs results)\]</table>
</body></html>\})
(define (trs rows) (map (lambda (row) \{<tr> \[(tds row)\]</tr>\\n\}) rows))
(define (tds row) (map (lambda (col) \{<td> \[ col \]</td>\}) row))
Similar Scheme approaches: SCRIBE (Serrano), BRL (Lewis), ...
....Ruby example, from pixel@mandrakesoft.com
Dir\['**/*.c'\].each\{|c|
o = c.sub('\.c$', '.o')
if !FileTest.exist?(o) || File.stat(o) <= File.stat(c) then
puts "compiling #\{c\} to #\{o\}"
system("gcc", "-c", "-o", o, c)
end
\}
Ruby provides support for iteration (.each\{|c|...\}),
pattern matching/replacement (c.sub(P1,P2)),
system calls (system(Command,A1,A2,...)),
recursive file filtering (Dir\['**/*.c'\]),
file property access (File.stat, FileTest).
| Library | Source | Lightweight access |
|---|
(load "ll2.scm") ;; JScheme version with lightweight library
(for-each
(lambda(f)
(define o ((/// "\\\\.c$" "\\\\.o") f))
(if (or (not (.exists o)) (<= (.lastModified o) (.lastModified f))))
(begin (display \{compiling \[f\] to \[o\]\\n\})
(run gcc -c -o ,o ,f)))
(files** dir (// "\\\\.c$")))
This version uses RegEx pattern matching(//) and replacement(///),
recursive file filtering(file**), and System access(run).
;; Perl wanna-be version
(@ f (**/* Dir(// "\\\\.c$"))
(<- o((/// "\\\\.c$" "\\\\.o")f))
(? ((||(!(Fis? o))(<=(Fmod o)(Fmod f)))
(d \{compiling \[f\] to \[o\]\\n\})
(run gcc -c -o ,o ,f))))
Using
(define-macro (@ f fs . body) `(for-each (lambda(,f) ,@body) ,fs)) (define-macro (? . R) `(cond ,@R)) (define-macro (<- . R) `(define ,@R)) (define-macro (|| . R) `(or ,@R)) (define **/* files**) (define d display) (define ! not) (define Fmod .lastModified) (define Fis? .exists)[(link "Run2" "back")] }) (("Run2a") {
;; JScheme version
(load "ll2.scm") ;; provide same support as Ruby example in 70 lines
(define (compile dir)
(for-each (lambda(c)
(display \{compiling \[c\] to \[(c->o c)\].\\n\})
(run gcc -c -o ,(c->o c) ,c))
(files** dir needsUpdate?)))
(define isCFile? (// "\\\\.c$"))
(define c->o (/// "\\\\.c$" "\\\\.o"))
(define (needsUpdate? f) (and (isCFile? f) (needsRecompile f (c->o f))))
(define (needsRecompile f o)
(or (not (.exists o)) (<= (.lastModified o) (.lastModified f))))
(dbquery STRING))
Shell Scripting lib = 70 lines
(define (// pattern)
(let ((p (Pattern.compile pattern)))
(lambda (object)
(.find (.matcher p (.toString object))))))
Similarly, file filtering is implemented in 6 lines using java.io.File methods
and takes a function argument for filtering.
}) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (("set-default-style") (.setAttribute ses "the-style" default-style) { "Default Style has been stored"
}) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (("set-print-style") (.setAttribute ses "the-style" print-style) { "Print Style has been set"
}) (else {