Showing posts with label object. Show all posts
Showing posts with label object. Show all posts

Sunday, September 1, 2024

Plato's Theory of Forms and Object Oriented Programming

When I first heard about Plato's Theory of Forms I immedately had the thought that this is very similar to the basic concepts of Object Oriented Programming (OOP). So let's see what are the similarities between the 2 concepts.

Abstract Concepts

  • Plato's Theory of Forms: Plato posited that non-material abstract forms (or ideas) represent the most accurate reality. For example, the concept of a "circle" exists perfectly in the realm of forms, while any physical circle is just an imperfect representation.
  • OOP: In OOP, classes are abstract blueprints for creating objects. A class defines the properties and behaviors that the objects created from it will have, similar to how forms define the essence of things.

Instances and Representations

  • Plato's Theory of Forms: Physical objects are mere shadows or instances of these perfect forms. They participate in the form but are not the form itself.
  • OOP: Objects are instances of classes. They are concrete representations of the abstract class, embodying the properties and behaviors defined by the class.

Hierarchy and Inheritance

  • Plato's Theory of Forms: Forms can have hierarchical relationships. For example, the form of "animal" can encompass the forms of "dog," "cat," etc.
  • OOP: Inheritance allows classes to derive from other classes, creating a hierarchy. A base class (like "Animal") can have derived classes (like "Dog" and "Cat") that inherit its properties and methods.

Universality and Reusability

  • Plato's Theory of Forms: Forms are universal and can be applied to many instances. The form of "beauty" can be found in various beautiful objects.
  • OOP: Classes are designed to be reusable. A class can be instantiated multiple times to create different objects that share the same structure and behavior.


More links on the topic:

Thursday, September 15, 2022

Jest test array with objects without order

Hello internet!

As I was doing my regular weekday I met a surprisingly unsolved problem in Jest, namely what if I wanted to test an array's _content without order_?

So e.g. these 2 arrays should equal

[{cat: 'Takira'}, {cat: 'Inari'}]

and

[{cat: 'Inari'}, {cat: 'Takira'}].

There is an SO post about this, but it doesn't care about objects, but only primitives. Also it points to Jasmine (which does have this as a native feature), but not Jest.

So here is my generic solution to the problem

for (obj in array) {

    expect(obj).toEqual(

        expect.arrayContaining([

            expect.objectContaining(obj)

        ])

    );

}

Let's go through this step by step!

So we are looping through the array (so far so good) and then we simply expect that object to be in the array.

That's literally it.

Thank You For Coming to My TED Talk.