Elixir Blog Posts

Great article about pros and cons of Elixir, in comparison to Ruby. https://www.netguru.co/blog/elixir-vs-ruby-which-one-is-the-language-for-your-next-project and how to choose the right one depends on your next project. :slight_smile:

1 Like

Reading well-written Ruby code is also a pleasure.

Isnā€™t this the case for about any language? A good question is how often does the ā€˜well-writtenā€™ code pop up? :wink:

Ruby on Rails, created back in 2005, was a really disruptive piece of technology. It changed how we write web applications - it was just mind blowing how much you could achieve by writing so little code. It also made prototyping applications faster.

Iā€™m guessing they werenā€™t Perl users. ^.^

Ruby is a really expressive language. A couple of lines in Ruby can do the same job as whole chunks of code in other languages.

Iā€™veā€¦ not actually seen this yet for Ruby. I keep hearing that such-n-such languages do this, but it is surprising how often they some how end up more verbose than ā€˜big-fastā€™ languages such as C++ or OCamlā€¦ Anyone have some code that examples this that also fulfills the well-written part (please no perl-golfing horrors)?

Also, if they are really going to say FP is a Con for Elixir, then they really badly need to say that OOP is a Con for Ruby. ^.^;

1 Like

PHP

class Ball
{
    private $_colour;
    
    public function __construct($colour)
    {
        $this->_colour = $colour;
    }
    
    public function is_red()
    {
        return ($this->_colour == 'red' ? true : false);        
    }
}

$the_ball = new Ball('green');
echo ($the_ball->is_red() ? '' : 'The ball is not red');

Ruby

class Ball
  def initialize(colour)
    @colour = colour    
  end
  
  def is_red?
    @colour == "red"
  end
end

the_ball = Ball.new("green")
puts "The ball is not red" unless the_ball.is_red?

:heart:


Can even be written like this:

class Ball
  def initialize(colour); @colour = colour; end
  def is_red?; @colour == "red"; end
end

the_ball = Ball.new("green")
puts "The ball is not red" unless the_ball.is_red?

Or this:

class Ball
  def initialize colour; @colour = colour; end
  def is_red?; @colour == "red"; end
end

the_ball = Ball.new "green"
puts "The ball is not red" unless the_ball.is_red?

:003:

I donā€™t think your example shows that ruby gains a lot compared to php. The php and ruby are almost exactly the same when written in the full format. Only small syntactic differences. And for your shorter version, they can be written shorter in php as well.

This link has a research paper comparing erlang and C++ (https://www.researchgate.net/publication/221211369_Comparing_C_and_ERLANG_for_motorola_telecoms_software)

The C++ code us 7.8 times larger for one of the components and 3 times larger for another component. An interesting quote from the paper (which I think is a summary of Ericssons report regarding the AXD301 ATM switch (which is based on 1M SLOC))

Moreover, the systems developed in Erlang and in conventional languages exhibit similar error rates per unit SLOC, and similar SLOC per unit time programmer productivity. Hence, the Erlang system shows at least a fourfold gain in productivity and reduction in errors

Perhaps modern C++/java are not as verbose as they once were and the gain is no longer as big but many years ago a ruby/python/perl project would see similar gains in reduction in SLOC compared to C++.

Thatā€™s fine :slight_smile: different strokes for different folks.

Languages like PHP always made me want to scream, yet, I know many people who love it. Thatā€™s totally cool - the world would be a boring place if we all thought the same.

(Initially, PHP I find abhorrant after near a decade working with it a decade ago, so not a great comparison language ^.^; )

Iā€™d find it far more succinct and safe both to do it in OCaml but I think Ruby is older than OCaml (mid 1990ā€™s or something?), so bad example, but in perl that would be like:

package Ball;
sub new {bless({colour => $_[1]}, $_[0])};
sub is_red {shift->colour == "red"}

Used like:

my $ball = new Ball("green");
print("The ball is not red") if(!$ball->is_red());

If I remember right (been yearsā€¦ >.>)

Or C++:

struct Ball {
  string colour;
  Ball(string colour_) : colour(colour_) {}
  bool is_red() { return colour == "red"; }
}

Etcā€¦ etcā€¦

A class example is a pretty bad example overall. Iā€™m guessing from the ā€˜can express more in less codeā€™ that usually means algorithms, not syntax, hence why a class example was a poor example?

Yeah they really canā€¦

Modern ā€˜stylesā€™ are significantly shorter yes, but even ā€˜back in the dayā€™ they could have been shorter as well, just most people had longer ideas in mind because fortran and so forth. Iā€™d argue that even ā€˜back thenā€™ C++ could still be about as succinct as Python in any non-trivial code, while still being substantially safer than the 3 listed languages as well.

PHP is a mishmash of decisions, itā€™s very Perlā€™ish in many ways, but probably better, except itā€™s ecosystem, which is worse. ^.^;

I may hate on PHP but itā€™s not because of the language itself, which actually has some really cool design decisions, but mostly in that the naming conventions are inconsistent, the ecosystem knows no concept of security (though it seems Ruby is similar on that as well from what Iā€™ve read), etcā€¦ etcā€¦ PHP7+ is actually rather blazing fast as well and on benchmarks Iā€™ve seen it absolutely blowing Ruby away in speed, and it easily can be more succinct than ruby (you can do some rather impressive perl-style-golfing in PHP).

Even then, Iā€™m unsure why Ruby would have been picked over, say, Python, and Iā€™ve not seen anything that says that it would have if Rails was not built on it. Iā€™m unsure what it has over Python as of yet, especially as itā€™s slower and has a slightly worse and more verbose syntaxā€¦

/me thinks this may should go in another threadā€¦

But still, Iā€™m curious how does Ruby do more in given lines of code than, say, C++? Sure I know C++ has #includeā€™s and other such noise, but they are collapsed by any decent IDE, and even then Ruby was not competing with C++, it was competing with the likes of Perl, which yes Perlā€™s syntax is a bit of a horror aberration but it still seems far more ā€˜expressiveā€™ than Ruby? And Iā€™m still curious what Ruby gains over Python as well?

Unfortunately i think that not too often :confused:

PSA: Be careful using Elixirā€™s ā€œwithā€ special form in tests.

2 Likes

Check out how I used Wireshark to help reduce the tedium of testing my Elixir-based serializers and parses for Bitcoinā€™s binary peer-to-peer protocol:

Generating Test Fixtures with Wireshark

FWIW, the tests resulted in an almost immediate positive return on investment. I immediately noticed an issue with one of my serializers that was causing all kinds of mysterious behavior within my node.

2 Likes

Bring your variables back from the dead! https://engineering.tripping.com/bring-your-variables-back-from-the-dead-ea250cf58ba4

Resurrect your variables after you restart an iex session.

5 Likes

I have recently published a short blog post about supervision strategies and DynamicSupervisor.

https://www.pompecki.com/post/supervision-strategies/

6 Likes

I just published a short blog post about me customizing the parameters of my contoller actions in phoenix:

https://medium.com/@lostkobrakai/alternative-parameters-for-phoenix-controller-actions-edd2b8a4362a

5 Likes

4 posts were merged into an existing topic: Blog Posts: Evangelising Elixir

Composing Elixir Plugs in a Phoenix application
23 Mar 18

Elixir is a functional language, so itā€™s no surprise that one of the main building blocks of the request-response cycle is the humble Plug. A Plug will take connection struct (see Plug.Conn) and return a new struct of the same type. It is this concept that allows you to join multiple plugs together, each with their own transformation on a Conn struct. A plug can be either a function or a module that implements the Plug behaviour.

http://www.jackmarchant.com/articles/composing-elixir-plugs-in-a-phoenix-application

1 Like

7 ways of doing Fizz Buzz in Elixir and other clickbaity stuff https://minhajuddin.com/2018/07/06/7-ways-of-doing-fizz-buzz-in-elixir-and-other-clickbaity-stuff/

3 Likes

Composing Ecto queries

1 Like

Hi folks!

I just published a new blog post: A dive into database multi-tenancy in Elixir with Ecto, and I wanted to share with you all. Itā€™s a blog post with some tips and experiences about the migration of an application with a regular database to a multi-tenancy one in Elixir with Ecto.

http://dreamingecho.es/blog/a-dive-into-database-multi-tenancy-in-elixir-with-ecto

2 Likes

https://embedded-elixir.com/ Embedded Elixir has lots of Nerves related posts

5 Likes

How to use private hex dependencies in your docker images while building your releases https://engineering.tripping.com/how-to-use-private-hex-dependencies-in-your-docker-images-42b2d01d8d70

1 Like

Nice. Itā€™s always good to remind people that thereā€™s many ways of accomplishing something. Iā€™ve got a post sitting in my backlog somewhere about golfing down a riff on FizzBuzz to follow the style of a Clojure implementation. I might have to post that soon.

1 Like