Programming Paradigms

Prof. Dr. Mirco Schoenfeld

what is a paradigm

Programming paradigm is a high-level way to structure and conceptualize the implementation of a computer program.

What is covered by a paradigm

Some paradigms are about the way code is organized, some about grammar and syntax, and some about implications of program execution.

purpose of paradigms

Originally, their purpose was to classify programming languages.

multi-paradigm programming languages

Main paradigms

Various classifications of programming paradigms

Imperative Programming
how to execute stuff, defines control flow
statements change a program state
Declarative Programming
what to execute, defines program logic

Imperative vs Declarative Programming Example

my_numbers = [1,2,3,4,5,6,7,8,9,10]

Imperative Style:

small = []
for i in my_numbers:
  if i < 5:
    small.append(i)

Languages like Java, C, C++, Python

Declarative Style:

small = [i for i in my_numbers if i < 5]

Languages like SQL, Regular Expression; in Spreadsheets

Main paradigms

Another major classification of programming paradigms

Procedural Programming
specifies the steps a program must take to reach a desired state
subset of imperative programming; main difference is that programs can be split into smaller parts
Functional Programming
running programs equals evaluating mathematical functions; avoids states and mutable data
expressions map values to other values
Object-oriented Programming
organizes programs as objects which have attributes and methods. objects interact with each other

Procedural vs Functional Programming Example

Procedural Style:

const numList = [1,2,3,4,5,6,7,8,9,10];
let result = 0;
for (let i = 0; i < numList.length; i++) {
  if (numList[i] % 2 === 0) {
    result += numList[i] * 10;
  }
}

Functional Style:

const result = [1,2,3,4,5,6,7,8,9,10]
          .filter(n => n % 2 === 0)
          .map(a => a * 10)
          .reduce((a, b) => a + b, 0);

It’s your turn

Where is functional programming superior to imperative or object-oriented programming and why?

Object Oriented Programming

Object Oriented Programming (OOP):

  • based on the concept of objects
  • objects contain attributes or properties
  • objects can take actions
  • programs are designed by making objects interact with one another

Languages like C++, Java, Python, R

OOP Example

OOP Inheritance

Typically, OOP languages allow inheritance.

Great concept to allow for code reuse and extensibility.

It’s your turn

Where is OOP beneficial to use and why?

For something completely different

Scripting Languages

Scripting languages.

Scripting Languages

Scripting languages are used to manipulate, customize, and automate facilities of an existing sytem.

Scripting languages are usually interpreted at runtime rather than compiled.

Why Scripting Languages

Scripting languages can make your life so much easier!

…automation, remember?

Great Scripting Languages

  • AWK
  • Perl
  • Python
  • sed

AWK Example

Why AWK is so great… Consider the amazon review dataset:

http://jmcauley.ucsd.edu/data/amazon/index_2014.html

or an updated version

https://nijianmo.github.io/amazon/index.html

The total number of reviews is 233.1 million (142.8 million in 2014).

AWK Example

An excerpt from a book review

marketplace customer_id review_id product_id product_parent product_title product_category star_rating helpful_votes total_votes vine verified_purchase review_headline review_body review_date
US 22480053 R28HBXXO1UEVJT 0843952016 34858117 The Rising Books 5 0 0 N N Great Twist on Zombie Mythos I've known about this one for a long time, but just finally got around to reading it for the first time. I enjoyed it a lot!  What I liked the most was how it took a tired premise and breathed new life into it by creating an entirely new twist on the zombie mythos. A definite must read! 2012-05-03

AWK Example

Data Columns of Amazon Review dataset (excerpt):

01  marketplace       - 2 letter country code [...]
02  customer_id       - Random customer identifier [...]
03  review_id         - The unique ID of the review.
04  product_id        - The unique Product ID the review pertains to.
05  product_parent    - Random product identifier [...]
06  product_title     - Title of the product.
07  product_category  - Broad product category [...]
08  star_rating       - The 1-5 star rating of the review.
09  helpful_votes     - Number of helpful votes.
10  total_votes       - Number of total votes the review received.
11  vine              - Review was written as part of the Vine program.
12  verified_purchase - The review is on a verified purchase.
13  review_headline   - The title of the review.
14  review_body       - The review text.
15  review_date       - The date the review was written.

AWK Example

Obtain all star ratings:

awk '{ print $8 }' reviews.tsv

AWK Example

Obtain the average star rating over the entire dataset:

awk -F '\t' '
   { total = total + $8 }
   END { print "Average book review is", total/NR, "stars" }
' reviews.tsv

Is AWK fast enough?

Is AWK even fast enough? An example with ~1.7GB of data.

Adam Drake reported a 47x speedup compared to a Hadoop cluster using standard awk.

(Drake, n.d.)

Is AWK fast enough?

Using an optimized version of awk gained a 235x speedup compared to Hadoop on ~1.7GB of data.

(12 seconds vs. 26 minutes)

On a Laptop.

(Drake, n.d.)

Why you should care

Try it yourself

Scripting languages can help you with your daily routines or repetetive tasks

Try it yourself

Try it yourself

Try it yourself

rename.py:

# importing os module
import os

folder = "xyz"
for count, filename in enumerate(os.listdir(folder)):
    dst = f"Hostel {str(count)}.jpg"
    src =f"{folder}/{filename}"  # foldername/filename, if .py file is outside folder
    dst =f"{folder}/{dst}"

    os.rename(src, dst)

> python3 rename.py

(Joshi 2022)

Conclusion

Concluding the scripting languages:

You should learn at least one of them.

References

Drake, Adam. n.d. “Command-Line Tools Can Be 235x Faster Than Your Hadoop Cluster.” https://adamdrake.com/command-line-tools-can-be-235x-faster-than-your-hadoop-cluster.html.
Joshi, Vineet. 2022. “Rename Multiple Files Using Python.” https://www.geeksforgeeks.org/rename-multiple-files-using-python/.