|
|||||
|
|||||
Jaskell Shell
Jaskell Shell is an interactive command line interpreter for Jaskell. It is handy for experimenting simple jaskell expression or java statement real quick. Run Jaskell ShellIn order to use Jaskell Shell. You need to download Jaskell jar file. Make sure the jfuntuil.jar, jparsec.jar and jaskell.jar are all in classpath. Then run "java jfun.jaskell.shell.Shell" The shell will then show: Jaskell Shell version 0.6 > The version number may vary depending on the version you are running on. Play with SwingNow assume we want to try "javax.swing.JOptionPane.showInputDialog()", we can type in: > javax.swing.JOptionPane.showInputDialog["your age?"]
>
Hit the "Enter" key twice to evaluate this expression. (This is because shell allows you to enter expression in multiple lines, the first "enter" key may just start another line of the expression.) Note, we use square brackets instead of parenthesis for java method. This can be understood as a reflection call where an array of objects is passed to each method. The above code is straight-forward, but not handy enough. Suppose we need to run "showInputDialog", "showConfirmationDialog" etc repeatedly, we certainly don't want to keep typing in "javax.swing.JOptionPane" again and again. In order to do this, we can give "javax.swing.JOptionPane" an alias, kinda like typedef in C: > dialog = javax.swing.JOptionPane > => class javax.swing.JOptionPane When "Enter" key is hit twice, the value of the expression is shown after "=>". In this case, the expression value is the JOptionPane class. Now, we can use the shell command "?" to look at all the static methods supported by this "dialog": > ? dialog Since "?" is a shell command, not a jaskell function, we just need to hit "Enter" once. It then shows us all the static methods of JOptionPane. In order to run "showMessageDialog", we type in: > dialog.showMessageDialog[null, "hello world"] > A "hello world" message dialog will be popped up. To simplify the syntax even more, we can: > say msg = dialog.showMessageDialog[null, msg]
>
=> say()
The value of this expression is a function called "say". This function expects one parameter. Now we can "say" a lot of things: > say "how are you?" > > say "java sucks!" > blah blah blah. Threading for dummiesThe following expression starts a thread that prints "hello world": > Thread.new[(\_->System.out.println["hello world"]) `implements Runnable].start[] > hello world
We could use the prelude function "const" to simplify the expression so that it becomes: > Thread.new[const(System.out.println["hello world"]) `implements Runnable].start[] > hello world
As we have done already, we can simplify the println as: > println msg = System.out.println[msg]
>
=> println()
In fact, println function is already defined, you can use it without defining it. We define "println" here primarily as a demo of how functions can be defined to make syntax simpler. Using the same technique, we can define a "run" function that makes running task in thread easy: > run task = Thread.new[const task `implements Runnable].start[] > => run() Let's see how we can use the "run" function: > run(println "hello world") > hello world > run(println "Yuck!") > Yuck! > run (say "nice!") > The last "say nice" would have blocked the shell if it were not running in a thread. But calling it with "run", the shell is not blocked. There are of course many other uses. Any jaskell expression can be typed in this shell. Shell commands and special variables.To see what functions are available globally: > ? To see what functions are available in a certain namespace: > ? java.lang To get the value of the previously evaluated expression: > _0 > To define more than one names in one line: > a=1;b=2
>
=> {a=b, b=2}
To exit shell: > !exit To get help of shell commands: > !help |
|||||
|
Copyright 2003-2006 - The Codehaus. All rights reserved unless otherwise noted.
Powered by Atlassian Confluence
|
|||||