(Cont'd) Current Trends
Total Page:16
File Type:pdf, Size:1020Kb
Scripting vs Systems Programming Languages (cont’d) Designed for gluing Designed for building Does application implement complex algorithms and data applications : flexibility applications : efficiency structures? Does application process large data sets (>10,000 items)? Interpreted Compiled Are application functions well-defined, fixed? Dynamic variable creation Variable declaration If yes, consider a system programming language. Data and code integrated : Data and code separated : meta-programming cannot create/run code on Is the main task to connect components, legacy apps? supported the fly Does the application manipulate a variety of things? Does the application have a GUI? Dynamic typing (typeless) Static typing Are the application's functions evolving rapidly? Examples: PERL, Tcl, Examples: PL/1, Ada, Must the application be extensible? Python, Ruby, Scheme, Java, C/C++, C#, etc Does the application do a lot of string manipulation? Visual Basic, etc If yes, consider a scripting language. cs480 (Prasad) LSysVsScipt 1 cs480 (Prasad) LSysVsScipt 2 Current Trends Jython (for convenient access to Java APIs) Hybrid Languages : Scripting + Systems Programming I:\tkprasad\cs480>jython – Recent JVM-based Scripting Languages Jython 2.1 on java1.4.1_02 (JIT: null) Type "copyright", "credits" or "license" for more information. »Jython : Python dialect >>> import javax.swing as swing >>> win = swing.JFrame("Welcome to Jython") »Clojure : LISP dialect >>> win.size = (200, 200) »Scala : OOP +Functional Hybrid >>> win.show() >>> ^Z cs480 (Prasad) LSysVsScipt 3 cs480 (Prasad) LSysVsScipt 4 1 1 Java vs Jython (cont’d) map = new HashMap(); for (Iterator i; i.hasNext();) map.put("one",new Integer(1)); map = for i in list: map.put("two",new Integer(2)); {"one":1,"two":2,"three":3} { map.put("three",new Integer(3)); i.next(); (* iterator *) } System.out .println(map .get( "one " )); print map ["one"] List newList = ArrayList() for (Iterator i; i.hasNext();) newList = { list = new LinkedList(); [function(i) for i in oldList] list.add(new Integer(1)); Object obj = i.next(); newList.add(function(obj)) list.add(new Integer(2)); list = [1, 2, 3] (* list comprehension *) list.add(new Integer(3)); } cs480 (Prasad) LSysVsScipt 5 cs480 (Prasad) LSysVsScipt 6 Defining functions and expressions in Jython (file: eg.py) More Jython code examples list = def fac(x) : [pow(i,i) for i in [1,2]] Dynamic code evaluation “factorial function” if x <= 1: return 1 from math import * print eval (“[1,3] + range(6,10,3)”) return long(x)*fac(x-1) print “PI=%f, e=%f” % (pi,e) # [1, ,3, 6, 9] fac(()5) Y = range(1, 9, 1) x = 2 + 3j # 120L # [1,2,3,4,5,6,7,8] print fac(5), fac(20) Y[1:3] exec “x = 5, x + x” print fac.__doc__ #[2, 3] #(5, (4+6j) #factorial function Y[::2] #[1, 3, 5, 7] X = 2 + 3j x.imag + x.conjugate() # (5 – 3j) cs480 (Prasad) LSysVsScipt 7 cs480 (Prasad) LSysVsScipt 8 2 2 Cont’d Cont’d Exceptions Functional Programming def readfile (name): apply(lambda x,y : x*y, (10, 20)) “return lines in file or None if unreadable” # 200 try: map(lambda x,y: x + y, [[1,2],[“a”]], [[“3”],[“b”]]) file = open(name, ‘rr)’) # [[1, 2, ‘’‘3’], [‘’‘a’, ‘’‘b’]] try: # raise IOError reduce(lambda x,y: x + y, [1,2,3], 100) return file.readlines() finally: # 106 file.close() filter(lambda x: x > 0, range(10,-5,-3)) except IOError, ioe: # [10, 7 , 4, 1] print “Exception –”, ioe cs480 (Prasad) LSysVsScipt 9 cs480 (Prasad) LSysVsScipt 10 Java functionality through Jython win = swing.JFrame("Welcome to Jython", size=(200, 200),windowClosing=exit) import java.lang as lang win.contentPane.layout = awt.FlowLayout( ) import javax.swing as swing field = swing.JTextField(preferredSize=(200,20)) import java.awt as awt win.contentPane.add(field) names = ["Groucho", "Chico", "Harpo"] quotes = {"Groucho": "Say the secret word", "Chico": "Viaduct?", buttons = [createButton(each) for each in names] "Harpo": "HONK!"} def buttonPressed(event): for eachButton in buttons: field.text = quotes[event.source.text] win.contentPane.add(eachButton) def exit(event): win.pack( ) lang.System.exit(0) win.show( ) def createButton(name): return swing.JButton(name, preferredSize=(100,20), actionPerformed=buttonPressed) cs480 (Prasad) LSysVsScipt 11 cs480 (Prasad) LSysVsScipt 12 3 3.