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
while? Get ready for some pure OOP-madness.
nilis not available.
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.
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. …
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.
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. …
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 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.
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²). …
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.
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:
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 ;)
Let’s establish some ground rules.
map, reduce, find, select, reject
mergefor modifying them (no mutations!) …
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?
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:
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. …