Skip to content

Instantly share code, notes, and snippets.

@tonysm
Last active November 23, 2019 18:54
Show Gist options
  • Select an option

  • Save tonysm/ee19c2db34f3fd66d764492f73ef5efc to your computer and use it in GitHub Desktop.

Select an option

Save tonysm/ee19c2db34f3fd66d764492f73ef5efc to your computer and use it in GitHub Desktop.
oop and fp article examples
class Deployment
def mark_as_finished
# ...
end
def mark_as_timedout
# ...
end
def cancel
# ...
end
# ...
end
# One can run the code by sending messages to an object of Deployment, like so:
deploy = Deployment.new
deploy.mark_as_finished
# This is mainly sending the "mark_as_finished" message to the deployment object.
# Ruby has another syntax that makes it more clear that method calls are in fact messages.
# You can achieve the same by using the "send" method available in all Objects:
deploy.send(:mark_as_finished)
# This will get the same method invoked as before, but you can clearly see the message now.
# I should point out that in Ruby this will also get private methods invoked by the outside wourld
# and you should consider using public_send instead, for safety:
deploy.public_send(:mark_as_finished)
# In this case, this is safer because it allows the Deployment class to change its mind on how it's internals
# are structured (private methods) while commiting to the same messages publicly.
<?php
// PHP also offers a way to call methods on objects in a way that ressembles the message style:
class Deployment
{
public function markAsFinished()
{
// ...
}
public function markAsTimedout()
{
// ...
}
public function cancel()
{
// ...
}
}
// You can invoke the code by doing a method call in the object:
$deploy = new Deployment();
$deploy->markAsFinished();
// Or using the functions to send messages to them, like so:
call_user_func([$deploy, 'markAsFinished']);
// Ruby's syntax is much more expressive, IMO, but the idea is the same: you can run code by sending messages to things (objects).
defmodule Deployment do
def start_link do
Task.start_link(fn -> loop() end)
end
defp loop() do
receive do
{:mark_as_finished, deploy_id, caller} ->
# Handle the message.
send caller, :ok
loop
{:mark_as_timedout, deploy_id, caller} ->
send caller, :ok
loop
{:cancel, deploy_id, caller} ->
send caller, :ok
loop
end
end
end
# You can send a message to the Deployment process like so:
{:ok, deploy} = Deployment.start_link
fake_deploy_id = 123
send deploy, {:mark_as_finished, fake_deploy_id, self()}
# The self() function should return the PID of the current iex session, which will
# be used by the deployment process to send back a response. We can see all messages
# sent to the current session process with the flush function:
flush
# outputs ":ok"
import java.util.*;
class Collection<T> {
private ArrayList<T> items = new ArrayList<T>();
public void add(T item) {
items.add(item);
}
public Integer count() {
return items.size();
}
public static void main(String args[]) {
Collection<Integer> numbers = new Collection<Integer>();
numbers.add(1);
numbers.add(2);
System.out.println(numbers.count());
Collection<String> letters = new Collection<String>();
letters.add("a");
letters.add("b");
letters.add("c");
System.out.println(letters.count());
// This should output: "2" and "3" for each respective collection.
// To run this code:
// 1. paste it in a file named Collection.java;
// 2. Compile it running: javac Collection.java; and
// 3. Run it with "java Collection". You should see the outputs I mentioned.
}
}
class Collection
def initialize
@items = []
end
def add(item)
@items << item
end
def count
@items.length
end
end
numbers = Collection.new
numbers.add(1)
numbers.add(2)
puts numbers.count
letters = Collection.new
letters.add("a")
letters.add("b")
letters.add("c")
puts letters.count
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment