1 Problem, 16 Programming Languages (C++ vs Rust vs Haskell vs Python vs APL...)

preview_player
Показать описание
A video taking a look at 16 programming language solutions (C++, Rust, D, Clojure, Ruby, Elixir, Raku [Perl 6], Haskell, Racket, Julia, Python, APL, J, BQN, Pharo Smalltalk & Fortran) to one problem.

Contest: 255
Problem Name: Find GCD of Array
Problem Type: Greedy

Github Links / Chapters:
0:00 Intro
1:47 Problem Statement
11:23 Fork / S' Combinator / Starling' / Phoenix
20:19 Languages not covered
21:30 Rankings
26:00 Summary
26:56 Podcasts and Outro

Рекомендации по теме
Комментарии
Автор

In 30 years programming professionally I have never used gcd in any production code, ever. But good to know it's in the global namespace in so many of these languages.

Spongman
Автор

I find it interesting that Rust is the only language where you have to explicitly say that this function will crash if you pass in an empty list, I like that.

nilstrieb
Автор

The Rust code surprised me a bit, because:
- The Rust stdlib doesn't have a gcd function, nor a `integers` submodule - this is part of the `num` crate instead, which is an external dependency but has the added benefit that it makes the calculation generic.
- passing the whole Vec by value is inefficient and leads to the ceremony that you have there on top of being an obvious performance issue - instead, accepting a slice reference allows you to get to `.min()` and `.max()` directly.
- Rust is a safe language and incentives keeping safety. Making the return value optional leads to a cleaner solution.

All in all, the better solution (with the num crate) is this:
fn find_gcd(arr: &[i32]) -> Option<i32> {
Some(num::integers::gcd(arr.min()?, arr.max()?))
}

SolarLiner
Автор

4:36 Ruby is expression based, the last line will be implicitly returned if you leave off 'return'

johndowson
Автор

liftM2 takes a function (a -> b -> c) and returns a function (f a -> f b -> f c), (f : Monad)
note that you don't need the monad constraint since maximum does not rely on the result of minimum or vice versa - and could just use liftA2 from Control.Applicative - not that it matters in this case but Monad is sequential where as solution using Applicative could be made to be parallell.

or: gcd <$> minimum <*> maximum

ebn__
Автор

For julia, it is common to use one-line function defs:

findgcd(nums) = gcd(min(nums...), max(nums...))

BoyoWhoGoesPoyo
Автор

17:33 "And last, but not least, Fortran"
Last place in actual rankings: Fortran

EnDeRBeaT
Автор

One would almost think there will be a comparison of some legitimate code and not literal one-liners using built-in functions...

rndszrvaltas
Автор

I'd be interested in seeing a Cobol solution in one of those videos. We all hear about how horrible Cobol is, but most of us never seen examples as to why.

johndowson
Автор

"Languages should have GCD in the core library" - why? How often does the average programmer use this?

Fortran is great for programming array programs across multiple cores and servers - Julia is the modern alternative.

francisking
Автор

The real test of a language is how well it can do things that the language designer wasn't thinking about. Arrays of words and gcd are way too common. Something like shortest path on a graph represented as a matrix would be better. Or something that requires less trivial datatypes like rational numbers or large numbers.

CarrotCakeMake
Автор

I'm so glad you were a good sport and gave Fortran a go, it gives a nice contrast to the other languages.

remyclarke
Автор

At this point I think you are cherry picking these challenges so they are elegantly implementable in apl xD
I would love to see a medium level leetcode exercise (even if its fewer languages then)

flyingsquirrel
Автор

@3:26 A better Julia solution IMHO is

find_gcd(nums) = gcd(extrema(nums)...)

NB: The splat operator (...) goes after the call to extrema, not on the nums array itself.

glynnec
Автор

You can simplify the C++20 a tiny bit further by using "minmax" instead of "minmax_element", which directly returns the two values instead of their iterators, sparing you from doing the dereferencing. The old non-ranges minmax only works with two values (or an initializer list) unfortunately, so this only work with C++20 ranges.

Possseidon
Автор

Your opinion of each language and its solution seems to be based entirely on brevity and elegance. If I saw any of these solutions in a production code review I would ask for them to be heavily commented to explain their function and to aid readability.

There is nothing wrong with judging a language subjectively but it would be helpful to explain your criteria for judgement somewhere in the video.

pak-
Автор

Why Fortran is so popular? Because it is still a thing if you are number crunching :-) using Fortran with OpenMP and MPI on big clusters is a thing e.g. for climate models, aerodynamics, optimization, and other science and engineering stuff.

And wrt your question of how to pass an array without passing a the size first: Assumed shape arrays

Declare it as:
INTEGER, DIMENSION(:) :: nums

Like this the subroutine expects a 1-dimensional array which has been allocated before. For 2D it would be DIMENSION(:, :) etc.

Btw: the IMPLICIT NONE would usually be put on top of you MODULE and thus you do not repeat it in all functions or subroutines.

ingenium
Автор

you can use the java interop with clojure and remove the import

(defn find-gcd [nums]
(.gcd (biginteger (apply min nums))
(biginteger (apply max nums))))

but tbh this problem is trivial, judging a language on the fact that it doesn't have gcd in its stdlib and "too many parens" is a bit silly.
also the above solution took me 20 seconds of experimenting in the REPL. The haskell solution was beautifully elegant, APL was a bit too cryptic for me.

gagansrai
Автор

I really like your videos, I learned of APL's existence from content you create (here and the podcasts).

But in most of the examples you choose, APL seems to excel because they're usually in the domain area that fits APL/J/BQN: transforming a list, or reducing it to a response.

I'd like to see you address the other side. Problems for which APL is not a good choice.

edok
Автор

It's nice to learn what it looks like to program the same simple problem using different languages, although I really don't think it's necessary to put a ranking part in the video, I've no idea what that part is for.

RunningBugs