Programming Paradigms Flashcards
Which programming paradigm emphasizes the use of pure functions and immutable data ?
FP
Which programming paradigm emphasizes the use of objects
OOP
How are programs constructed in FP ?
by composing pure functions that transform data without changing it.
What are the two characteristics of pure functions ?
pure functions have no side effects and always return the same output given the same input.
What are the consequences of coding using FP ?
code that is easy to reason about and test,
What are higher order functions ?
functions that take other functions as inputs or return functions as outputs.
Name 3 main characters in regards to the story of the FP paradigm
- pure functions
- immutable data
- higher order functions
what are objects ?
instances of classes that encapsulate data and behavior.
Name 3 main concepts highlighted via OOP
- abstraction
- inheritance
- polymorphism
what is abstraction ?
process of hiding complexity by exposing only relevant information to the user.
what is inheritance ?
process of classes inheriting properties and methods from other classes.
what is polymorphism ?
objects of different classes to be used interchangeably, as long as they have a common interface.
how is data and state managed in FP ?
data is immutable and state changes are managed through the creation of new data structures.
how is data and state managed in OOP ?
data is encapsulated within objects and state changes are managed through methods that mutate the object’s internal state.
how is control flow managed in FP?
control flow is managed through the composition of functions using higher-order functions.
how is control flow managed in OOP ?
control flow is managed through method calls and object interactions.
how is encapsulation managed in FP ?
functions are pure and have no side effects, so there is no need for encapsulation.
What is encapsulation ?
Encapsulation provides a way to control access to the internal state of an object and helps prevent external code from directly manipulating its data
how is inheritance managed in OOP ?
inheritance is used to create new classes that inherit properties and methods from other classes.
how are complex programs created in FP ?
composition is used to combine functions and data structures to create more complex programs.
what is a HOF ?
A higher-order function is a function that takes one or more functions as arguments or returns a function as its result.
how can you build complex functions via HOFs ?
Higher-order functions are used to build complex functions by combining simpler functions.
why is immutability important in FP ?
because it makes it easier to reason about the behavior of the program and helps to prevent bugs.
what is recursion ?
Recursion is a technique where a function calls itself.
why is recursion often used in FP ?
Recursion is often used in functional programming because it is a natural way to express computations that involve repeated calculations.
what is a monad ?
A monad is a design pattern used to manage side effects in functional programming.
why is a monad is a way to encapsulate a computation that has side effects ?
so that the side effects can be managed and controlled.
Name two alternative approaches to code reuse and structuring code that can be used instead of inheritance
Object composition and function composition
how does object composition work ?
by creating a new object that contains references to other objects, and delegating behavior to those objects.
List two advantages of object composition
- allows for greater flexibility and modularity
- avoid the problems of tight coupling and inheritance hierarchies that can arise with inheritance.
how does function chaining work in function composition ?
by chaining functions together so that the output of one function becomes the input of the next function.
List 2 disadvantages of inheritance
lead to :-
* tight coupling and
* complex inheritance hierarchies that are difficult to understand and maintain.
Name two techniques used in functional programming to transform a function with multiple arguments into a series of functions that each take a single argument.
currying and partial currying
const numbers = [1, 2, 3, 4, 5]; const doubleNumbers = numbers.map(num => num * 2); console.log(doubleNumbers); // [2, 4, 6, 8, 10]
FP using map
class Person { constructor(name, age) { this.name = name; this.age = age; } greet() { console.log(`Hello, my name is ${this.name} and I'm ${this.age} years old.`); } } const john = new Person('John', 30); john.greet(); // "Hello, my name is John and I'm 30 years old."
OOP
how can you achieve encapsulation in JS ?
In JavaScript, encapsulation can be achieved using closures or ES6 classes.
class Shape { constructor(color) { this.color = color; } draw() { console.log("Drawing a generic shape"); } } class Circle extends Shape { constructor(color, radius) { super(color); this.radius = radius; } draw() { console.log(`Drawing a ${this.color} circle with radius ${this.radius}`); } } class Square extends Shape { constructor(color, sideLength) { super(color); this.sideLength = sideLength; } draw() { console.log(`Drawing a ${this.color} square with side length ${this.sideLength}`); } } const circle = new Circle('blue', 10); const square = new Square('red', 5); circle.draw(); // "Drawing a blue circle with radius 10" square.draw(); // "Drawing a red square with side length 5"
polymorphism
how can you achieve polymorphism in JS ?
In JavaScript, we can achieve polymorphism through inheritance and method overriding.
How can you achieve abstraction in JS ?
In JavaScript, we can achieve abstraction through encapsulation and inheritance.
Which method below is abstract and why ?
class Vehicle { constructor(color) { this.color = color; } start() { console.log("Starting the vehicle..."); } stop() { console.log("Stopping the vehicle..."); } move() { throw new Error("Abstract method, must be implemented in subclass"); } } class Car extends Vehicle { constructor(color) { super(color); } move() { console.log(`Driving the ${this.color} car...`); } } class Boat extends Vehicle { constructor(color) { super(color); } move() { console.log(`Sailing the ${this.color} boat...`); } } const car = new Car('blue'); const boat = new Boat('red'); car.start(); car.move(); car.stop(); boat.start(); boat.move(); boat.stop();
The move method is declared as an abstract method, which means it must be implemented in subclasses.
what will be the console log output ? which one is the HOF ?
function multiplyBy(factor) { return function (number) { return number * factor; }; } const double = multiplyBy(2); const triple = multiplyBy(3); console.log(double(5)); console.log(triple(5));
output is 10 and 15
mutliplyBy is HOF
List one or more ways via which we can achieve more concise and modular code in JS?
HOF
how can you implement monads in JS ?
In JavaScript, we can implement monads using objects with a flatMap or bind method.
class Maybe { constructor(value) { this.value = value; } static of(value) { return new Maybe(value); } map(fn) { if (this.value === null || this.value === undefined) { return Maybe.of(null); } return Maybe.of(fn(this.value)); } flatMap(fn) { if (this.value === null || this.value === undefined) { return Maybe.of(null); } return fn(this.value); } } // Example usage const user = { name: "John Doe", address: { street: "123 Main St", city: "Anytown", state: "CA", }, }; const maybeUser = Maybe.of(user); const maybeStreet = maybeUser.flatMap((u) => Maybe.of(u.address)).flatMap((a) => Maybe.of(a.street)); console.log(maybeStreet.value); // Output: "123 Main St" const maybeZip = maybeUser.flatMap((u) => Maybe.of(u.address)).flatMap((a) => Maybe.of(a.zip)); console.log(maybeZip.value); // Output: null
monads
List out two advantages of currying and partial currying
currying and partial currying can make code more flexible and modular, and can simplify function composition in functional programming.
Define currying
Currying is the process of transforming a function with multiple arguments into a series of functions that each take a single argument.
Define partial currying
Partial currying is a technique that allows us to partially apply a function with multiple arguments, meaning we can provide some of the arguments and get back a new function that takes the remaining arguments.
function multiply(x, y, z) {
return x * y * z;
}
var multiplyByTwo = multiply.bind(null, 2);
console.log(multiplyByTwo(3, 4));
output = 24
partial currying
What is DI ?
Dependency Injection is a design pattern that allows you to inject dependencies into an object rather than having the object create its own dependencies.
Name one specific implementation of IOC
DI
List out the advantages of using DI
makes your code more modular, testable, and easier to maintain.
What happens in IOC ?
instead of your code controlling the creation and flow of objects, the control is handed over to a framework or container that handles the creation and management of objects.
Tell me what happens in Reactive Programming
data streams are represented as Observable sequences, and transformations are applied to these sequences using operators.
List out two features of FRP
- emphasizes the use of pure functions to handle data transformations, and
- makes use of functional programming concepts such as composition, mapping, and folding to create more declarative and composable code.
Name 2 libraries via which you can implement FRP
Bacon.js and Most.js
What is a Rest API ?
A REST API (Representational State Transfer Application Programming Interface) is a type of web service that uses HTTP requests to access and manipulate resources or data.
Full form of REST API
Representational State Transfer Application Programming Interface
What are SOLID principles ?
SOLID principles are a set of design principles that aim to make software more maintainable, scalable, and robust.
What is SRP ?
A class or function should have only one reason to change.
In JavaScript, this can be applied by breaking down large functions into smaller, more focused functions that each handle a specific responsibility.
Full form of SRP ?
Single Responsibility Principle (SRP)
full form of OCP ?
Open/Closed Principle (OCP)
Explain OCP
A class or function should be open for extension but closed for modification.
In JavaScript, this can be achieved by using object composition instead of inheritance, and by using interfaces to define contracts between components.
LSP full form ?
Liskov Substitution Principle (LSP)
Explain LSP
Liskov Substitution Principle (LSP)
Subtypes must be substitutable for their base types.
In JavaScript, this means that any object that implements a certain interface or inherits from a certain class must be able to be used interchangeably with any other object that implements the same interface or inherits from the same class.
ISP full form ?
Interface Segregation Principle (ISP)
Explain ISP
A client should not be forced to depend on methods it does not use.
In JavaScript, this means that interfaces should be kept small and focused, with only the methods and properties that are actually needed by the client.
DIP full form ?
Dependency Inversion Principle (DIP)
Explain DIP
Dependency Inversion Principle (DIP)
High-level modules should not depend on low-level modules; both should depend on abstractions. Abstractions should not depend on details; details should depend on abstractions.
In JavaScript, this means that code should depend on interfaces or abstractions instead of concrete implementations, and that dependencies should be injected into classes or functions instead of being hard-coded.
State 7 key features of REST API
- Resource identification: Resources are identified by a unique URI (Uniform Resource Identifier).
- HTTP verbs: REST APIs use standard HTTP verbs like GET, POST, PUT, DELETE, etc. to perform specific operations on resources.
- Stateless: Each request from the client to the server must contain all the necessary information to complete the request. The server does not maintain any state or session information about the client.
- Representation-oriented: Resources can be represented in different formats such as JSON, XML, or plain text.
- Hypermedia-driven: REST APIs use hyperlinks to connect resources and allow clients to navigate through the API.
- Cacheable: Responses from the server can be cached to improve performance.
- Layered architecture: REST APIs can be layered to improve scalability and security.