Image for post
Image for post

In my post Implementing numbers in “pure” Ruby I established some ground rules that allowed us to use some basic ruby stuff like equality operator, booleans, nil, blocks and so on.

But what if we had absolutely nothing, even basic operators like if and while? Get ready for some pure OOP-madness.

  • We can define classes and methods.
  • We should assume that ruby doesn’t have any classes predefined. Imagine we start from scratch. Even stuff like nil is not available.
  • The only operator we can use is the assignment operator (x = something).

Conditionals are important. They are the very essence of logic for our programs. So how do we do without them? I’ve come up with a solution: we can incorporate boolean logic inside EVERY object. …


I enjoy experimenting with programming paradigms and trying out some interesting (to me) ideas (some things become posts, like this and that). Recently I decided to see if I can write object-oriented code in a functional language.

Idea

I was trying to get some inspiration from Alan Kay, the creator of object-oriented programming.

OOP to me means only messaging, local retention and protection and hiding of state-process, and extreme late-binding of all things.

I decided that I will be happy if I can implement message sending and inner state.

So here comes the main problem of the whole concept — the state. …


Image for post
Image for post

Last time I told you a bit about bubble sort. Bubble sort is cool, however, you will never use it. This time I am going to show you a sorting algorithm faster than the one implemented in your language — counting sort. True story.

Performance

The most popular sorting algorithm — quicksort has a varying complexity between O(log(N) * N) and O(N²). Counting sort works for O(N + M) where M is the difference between the smallest and the biggest elements in your data. …


Image for post
Image for post

In the first post, we’ve taken a look at the binary search. Let’s continue with some simple sorting algorithms. I will start with my favourite one — bubble sort.

The algorithm

The algorithm goes through the array and compares each element with the next one. If the latter is smaller, it swaps them.

If you go from the beginning to the end, the biggest element will move to the end like a bubble upwards in the water. This means, in order to put all N elements at their places we need to go through all elements of the array N times.

Performance

To “bubble up” one element we need to go from the beginning to the end, which is N iterations. However, we still need to sort other N-1 elements, so we need to do N iterations N times which is N². There’re some ways to optimize it tho, but in terms of O-notation, the algorithm’s complexity is O(N²). …


Image for post
Image for post

I decided to refresh my memory on basic data structures and algorithms. As an additional fun challenge, I decided to try and implement them in different programming languages/style.

Let’s give a good old binary search a go. The simplest of them all.

The algorithm

Binary search is a fast way of finding values in a sorted array.

It works very similarly to a person looking for a word in a dictionary (you know, from a bookshelf). Try imagining yourself searching for the word “help”.

In essence, it works like this:

  1. Compare the value you look for with the element at the middle
  2. If the element is less than the value you are looking for, continue searching the right side. …

Image for post
Image for post

I love Ruby. I also love object-oriented programming. However, nowadays functional programming is getting more and more attention. And that’s not surprising because the functional paradigm has a lot of advantages.

Ruby, being a multi-paradigm language, allows us to write programs in a functional style. Let’s see if we can write a web application this way. Maybe we even end up inventing a web framework ;)

Ground rules

Let’s establish some ground rules.

  1. Lambdas. Lambdas everywhere
  2. Arrays. We can use arrays and some methods from Enumerable like map, reduce, find, select, reject
  3. Hashes. We can access values via [key] and use merge for modifying them (no mutations!) …


Image for post
Image for post
Photo by Andrew Buchanan on Unsplash

UPD. check out my new similar post but even cooler: https://weird-programming.dev/oop/classes-only.html

Object-Oriented Programming to me means that the system is divided into objects. An object is just an entity that has some state and some behaviour. You can make your object do something by sending it a message, hoping that it will understand you.

For practical reasons, every language has some primitives; basic data types you can use to write your program. Even though Ruby is, supposedly, a pure OO language (everything is an object), it has some primitives nevertheless.

For instance, numbers. They look like objects, they have methods and stuff. However, what are they really? 2 and 3 are different instances of the Integer class, so they are supposed to have a different state. But what is that state? …


The Elm Architecture gives us a reactive framework for creating web applications. It’s built around three parts: Model, View, and Update. However, that means that your whole app logic has to be centralised in one place: all your data should be in the single state (model) and all events are going through the same update function. Having event handling in just one place is, arguably, great and convenient thing. However, it can become very frustrating to read when the number of possible events becomes so large it can’t fit into your screen.

Let’s start with a small application that has only one text input. The full code can be found here. …

About

Dmitry Non

Unemployed software engineer looking for job. I love my dog and Guinness ❤ https://nondv.wtf

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store