FP Flashcards

1
Q

def assertAllPosS <: IntSet: S = …

Here what [S <: IntSet] is ?

And what does that mean ?

A

S is a subtype of Intset.We can also say that type S is upperbounded by type IntSet

It means that S can be instantiated only to types that conform to IntSet.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is Liskov Substituion Principle

A

When one can to do with a value of type B
one should also be able to do with a value of type A.

Eg: Everything we can do with a fruit, we can do with a banana. Everything we can do with an animal, we can do with a Cat and so on…

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How is Covariance and Contravariance denoted in Scala

A

With a “+” or a “-“ next to the Parametric Type.

Example:

class Box+T

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is Covariance ?

A

Covariance allows a generic type to preserve the subtype relationship. In other words, if type B is a subtype of type A, then Container[B] is a subtype of Container[A].

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Why is Covariance useful ?

A

This is useful in situations where you want to create collections or structures that can hold more specific types but still be treated as holding more general types.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is Contravariant ?

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Why is Contravariant useful ?

A

This is useful when you’re consuming values but not producing them. For example, let’s say you have a function that can accept any type of Animal, but you want to pass in a more general handler that can accept Dogs or Cats.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

what is type parameterization ?

A

When we say a class, trait, or method is type parameterized, it means that it can accept a type as a parameter (called a type parameter) that gets specified later, when you create an instance of the class or call the method.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

In the contexte of Polymorphisme what are bounds ?

A

Bounds are constraints from subtypes applied to types parameters.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

in the context of Polymorphisme what is Variance ?

A

How type parameters behaves when subtyping occurs.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What a is a generic type ?

A

A generic type (or type parameterization) is a type that is defined with one or more type parameters, allowing it to work with different types without specifying the exact types in advance. It provides a way to write reusable and flexible code that can operate on various data types while maintaining type safety at compile-time.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is a generic class ?

A

A class that is defined with one or more type parameters, allowing it to work with different types without having to specify those types exactly in advance

Eg: A List

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is the difference and the relation between a class and a type

A

Une classe est une définition ou une description d’un objet. Elle décrit comment un objet est construit et comment il se comporte. Elle est souvent associée à la création d’instances (objets) et contient des méthodes et des champs.

Un type est une catégorie de données qui décrit les valeurs que peut prendre une variable. Il peut être défini par une classe (comme Person), mais il peut également être autre chose (comme un type primitif ou un type abstrait).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Among Arrays and list which one are covariant which one isn’t and why ?

A

Arrays aren’t covariant because they are mutable while list are not mutable. Since you cannot modify the list, you can’t accidentally add a Cat to a list of Dogs.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What are the typing rules for function ?

A

if A2> A1 and B>B2 then
A1 => B1 < A2 => B2

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

how functions behaves in terms of variance regarding their type arguments and their return type

A

function are contravariant in their argument and covariant in their result type

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Can Objects have parameters. If yes or no why ?

A

They can’t because there’s only one instance of an object

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What is Nothing in Scala ?

A

It’s a universal subtype of all types

19
Q

What is a trait ?

A

A trait is like an interface in other languages (such as Java), but it can also include method implementations. It is primarily used to define behavior that can be shared across different classes. Traits are commonly used for multiple inheritance of behavior.

trait CanFly {
def fly(): Unit = println(“Flying”)
}

trait CanSwim {
def swim(): Unit = println(“Swimming”)
}

class Duck extends Animal with CanFly with CanSwim

20
Q

What is a coefficient

A

A number that mutiplies an other. That is a subtype of a factor ( number only)

21
Q

What is a polynomial ?

A

A mathematical expression composed of variables (unknowen) and coefficient that are linked by operations ( somme, soubrstration, etc.). Les variables dans un polynôme sont élevées à des puissances non négatives (entiers naturels).

22
Q

What is factorize ?

A

Factoriser consiste à réécrire une expression mathématique sous la forme d’un produit de plusieurs facteurs

23
Q

What is a factor

A

Term or expression mutiplied by a term or a an expression

24
Q

What are the steps of solving a recursion ?

A

Step1: Find the base case
Step2: Visualize
Step 3: Relate (bigger example with smaller example )
Step 4: Generalize
Step 5: Write code
step 6: substitution method

25
Q

Quelle expression logique, je peux utiliser pour trouver les nombres impairs d’une liste ?

A

(l.head % 2 != 0)

26
Q

Comment appelle-t-on le fait que “tous les éléments d’un ensemble respectent une condition” est considéré comme vrai même si l’ensemble est vide ?

A

Réponse : Vérité triviale (ou quantificateur universel sur un ensemble vide).

27
Q

What function function check if an element is present in a list? What argument does it take, what does it return ?

A

The contains function checks if a given element exists in a list. If the element is found, it returns true; otherwise, it returns false.

28
Q

What is the difference between a set and a list

How can you find stuff in a list vs in a set

A

Set: Ideal for collections where the uniqueness of elements is important and frequent lookups are necessary.
List: Used when the order of elements or the possibility of duplicates is important, or for sequential processing operations.
list use a Linkedlist method, while set use a hash method where an inex represent one element

29
Q

How can you find stuff

A
30
Q

What a function return an expression of this form:

x || y

What appens

A

L’évaluation s’arrête dès que True et retourner pour la première partie gauche de l’opérateur car si c’est true cela sera toujours true.

31
Q

What is a literal ?

A

A literal is a fix value without a name

32
Q

What is a constant ?

A

A constant is a fix value with a name

33
Q

What is the opposite of a literal ?

A

The opposite is a calculated variable or a dynamic variable that are generetated at the execution of the program.

34
Q

Enum

A

Un enum en Scala est un type qui permet de définir un ensemble fixe et limité de valeurs nommées (ou cas), souvent utilisé pour représenter des choix ou des états constants dans un programme.

35
Q

What are algebraic Data Structures

A

Les types de données algébriques (ou ADTs) sont des types composites qui combinent des valeurs simples en structures complexes, utilisant deux opérations principales : les types produits (ET) pour combiner plusieurs valeurs obligatoires, et les types sommes (OU) pour représenter des choix exclusifs parmi plusieurs options.

36
Q

What is a product type in the context of aglbraic data structures ?

A

An agebraic data types where all values are obligatory eg:
case class Adresse(rue: String, codePostal: String, ville: String)

37
Q

What is a sum type in the context of agebraic data structures ?

A

A data structure that can be either on or an other:

Eg:

sealed trait Animal
case class Chien(nom: String) extends Animal
case class Chat(nom: String) extends Animal

38
Q

Quel est ce symble ? &

A

C’est un bitwise AND.

Il compare chaque bit de deux nombres binaires et revoit uniquement des 1 si c’est 1 des dans les deux nombres.

39
Q

What is final class

A

it’s a class that can not be extended to other class ( not inherited)

40
Q

Que fait la fonction count ?

A

a fonction count retourne le nombre d’éléments dans une collection qui satisfont une condition donnée (un prédicat).

a fonction count retourne le nombre d’éléments dans une collection qui satisfont une condition donnée (un prédicat).

41
Q

Que fait lenght

A

La fonction length retourne le nombre total d’éléments dans une collection.

val words = List(“apple”, “banana”, “cherry”)
words.length // Résultat : 3 (nombre total de mots)

42
Q

When can Reduce been used in parralele

A

When the op is associative.
When we say that an operator op must be associative, we mean that it should yield the same result regardless of how the elements are grouped

43
Q

l.scanLeft(z)(op) == l.reverse.scanRight(z)((a, b) ⇒ op(b, a)).reverse

A
44
Q
A