Cover Venkatesh Prasad Ranganath Story Researcher at Microsoft Research, India Exploring the Current Landscape of Programming Paradigm and Models Until few years ago, imperative programming of Programming Paradigms[44]” page assignments (e.g. i = i + 1). This is evident and object-oriented programming have at Wikipedia states that “None of the by the explicitly use and maintenance been two prevalent programming main programming paradigms have a of a counter variable i, dependence on paradigms in the industry. However, this precise, globally unanimous defi nition, let the length of nums, and direct access of has started to change over the past few alone an offi cial international standard.” elements of nums (via indexing syntax, years with the community exploring and Hence, instead of chasing defi nitions of i.e. nums[i]). slowly adopting functional programming. programming paradigms, let us try to In object-oriented style, the Similarly, there has been a renewed understand programming paradigms via program does not depend on the interest in exploring existing programming examples. internal details of list type. Instead, languages or creating new programming We shall start with three prevalent list type encapsulates its elements languages that support either tried-and- programming paradigms: imperative, (implementation details) and provides forgotten paradigms or a combination of object-oriented, and functional. an iterator abstraction (interface) to paradigms. Further, there has been a surge For this purpose, let us consider iterate over its elements. Consequently, in languages and programming systems to the following programs that identical in the program depends only on the support development of loosely coupled functionality but written in imperative, iterator abstraction. This style of applications such as web applications and object-oriented and functional using encapsulation to hide information distributed applications. programming styles. These programs and abstraction to provide appropriate To a large extent, these changes have accept a list of integers (nums) and views of information (decouple been fueled by two factors. The fi rst factor output a list of squares of these integers implementation from interface) are is the decision of the computer hardware (output). To simplify comparison, all the distinct features of object-oriented industry to move from faster single- of these programs are written in Python programming. core processors to slower multi-core programming language[2]. list (an As for the part of specifying how to processors (to compensate for the failure ordered sequence of objects) is a primitive iterate through the elements, observe of Moore’s law[1]n).The second factor data type in Python. that the boilerplate code required to is the rise of internet-enabled mobile Imperative: use an abstraction such as iterators devices, cloud computing, and big data. output = list() is dependent only on the abstraction As a result of the fi rst factor, i = 0 and is parameterized only by the object applications cannot be scaled by merely while i <len(nums): providing the abstraction. Combining this switching to faster processors. Instead, output.append(square(nums[i])) observation with language level syntactic applications need to be reengineered i = i + 1 sugar and compile-time transformation, to leverage multiple cores. Similarly, Object-Oriented(1) [Pseudo-Python]: object-oriented style of programming the second factor has forced more and output = list() can be further simplifi ed as illustrated in more applications to be composed of i = nums.iterator() the third example program titled “Object- communicating components that are while i.hasNext(): Oriented (2)”. In this form, with the distributed across diff erent computers. output.append(square(i.next()) exception of using for looping statement, the program does not specify how to This has prompted the programming Object-Oriented (2): iterate through the elements of nums. community to explore, extend, and even output = list() create programming languages to enable for n in nums: In functional style, the program and simplify programming in the presence output.append(square(n)) specifi es that function square should of concurrency, distribution, and web be applied to every element of nums and Functional: the results should be returned in order as technologies. output = map(square, nums) In this context, this article attempts a list. This is accomplished using ahigh to explore the current landscape of While the above programs obviously level function map(f,l) that applies the given function f to every element programming paradigms and models. diff er in length (succinctness), they also diff er in the level of detail at which of the given sequencel(that provides Old Timers computation is specifi ed. iterator abstraction) and returns What is a programming paradigm? The In imperative style, the program the results in order as a list. So, unlike “Programming Paradigm[43]” page at explicitly specifi es how to iterate through in imperative style and object-oriented Wikipedia defi nes programming paradigm the elements of nums, depends on the style, the program does not specify how as a fundamental style of programming. internal details of list type (the type of to iterate through numso. Instead, the Digging a bit deeper, the “Comparison nums), and modifi es program state via program relies on map to leverage the 1Readers interested in concurrency and hardware changes may want to read Herb Sutter’s “Welcome to the Jungle” blog post[38]. 2As a result, the decisions regarding how to iterate and the order of iteration are deferred to the compiler or the runtime system. CSI Communications | February 2013 | 6 www.csi-india.org iterator abstraction provided by list Where’s that list of languages? type to iterate through nums. Hence, as There is a reason this article has been in mathematics, functional programming language agnostic thus far. Most existing is all about applying and composing main stream languages (such as C#, functions to specify computationp. JavaScript, Python, and Ruby[3,4,2,5]) and Another distinct aspect of functional new up-and-coming languages (such programming is immutability of data – as Clojure, F#, and Scala[6,7,8]) support function applications and expression various aspects of multiple programming evaluations create values as opposed to paradigms. This is achieved by off ering modifying existing values. To understand language features that embody a this aspect in terms of its ramifi cation combination of aspects of multiple on programming, consider a function paradigms, e.g. F# supports object- append(l,e) that appends element e oriented programming by supporting Fi g. 2: Summing network to list l. In terms of exposing the result of classes. On the other hand, there are program. So, square function can be append, there are two options: 1) modify languages that are pure in supporting a applied to the elements of nums in random list l in place (imperative) or 2) create a single paradigm, e.g. Haskell and SML/NJ order. This implies square function can be new list containing the elements of land are pure functional languages[9,10]. concurrently applied to elements of nums the element e (functional). The following While language support for a provided square and Seq.map functions pseudo code snippets exercise these programming paradigm certainly helps are side-eff ect free. Since these functions options to achieve the same goal – v1 programming in that paradigm, one could are indeed side-eff ect free, we can trivially should refer to the list[7,8,9]. program in a paradigm that is not directly parallelize the above programas follows supported by a language, e.g. one could v1 = [7,8] v2 = [7,8] by using PSeq.map, a parallel version of v2 = v1 v1 = append (v2,9) do object-oriented style programming in Seq.map. append(v1,9) a language (e.g. C) that is not ideal for object-oriented programming. Remember With the fi rst option (on the left), the nums |> PSeq.map square |> that programming paradigm is about the List.ofSeq change to listv1 will be visible via v2 style of programming (and not about any (an alias to v1) referring to v1’s list. This This example was a simple case specifi c programming language). phenomenon is known as side-eff ect. of data parallelism – same operation is With the second option (on the right), Rookies performed on various data items and due to the absence of side-eff ect, the list Besides prevalent programming these operations and their results are resulting from append is assigned to v1 paradigms, there are new language mutually independent – and functional to achieve the desired goal (as, without features (or paradigms?) that are inspired programming was ideal. the assignment, v1 will refer to list[7,8]). by specifi c aspects of problems. In the rest Now, consider the task of summing Fig. 1 illustrates eff ect of executing these of this article, we shall explore few such the squares of a given list of integers programs. problem aspects and related language (nums) with the following sequential F# While presence and absence of side- features and paradigms. program. In this program, Seq.reduce eff ect have their benefi ts and drawbacks, Concurrency / Parallelism reduces the input sequence to a single integer by applying sum to each element the absence of side-eff ect is a desirable Continuing with functional programming, of the sequence along with the result of aspect to program concurrency, and let us try to understand why side eff ect applying sum to the previous element; at we shall discuss this aspect in the next freedom and immutable data benefi ts the start, sum is applied to the fi rst two section. concurrent programming. Consider the elements of the sequence. following F# program to square a list of integers. nums |>Seq.map square |>Seq. As with the functional reduce sum |> List.ofSeq Python program, square To parallelize this program, observe function is applied to each that summation is dependent on each element of nums using input element.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages4 Page
-
File Size-