Rust vs 7 Other Languages You Probably Haven't Tried

  Переглядів 73,140

Code to the Moon

Code to the Moon

Рік тому

Take a quick tour of 7 languages that most people probably haven't tried. We write a simple program in Rust, then write the same program in each of those 7 languages in an effort to get a feel for them.
Check out Sidekick, an incredible debugging tool: www.runsidekick.com/
-
Stuff I use to make these videos - I absolutely love all of these products. Using these links is an easy way to support the channel, thank you so much if you do so!!!
Camera: Canon EOS R5 amzn.to/3CCrxzl
Monitor: Dell U4914DW 49in amzn.to/3MJV1jx
Keyboard: Keychron Q1 amzn.to/3YkJNrB
SSD for Video Editing: VectoTech Rapid 8TB amzn.to/3hXz9TM
Microphone 1: Rode NT1-A amzn.to/3vWM4gL
Microphone 2: Seinheiser 416 amzn.to/3Fkti60
Microphone Interface: Focusrite Clarett+ 2Pre amzn.to/3J5dy7S
Tripod: JOBY GorillaPod 5K amzn.to/3JaPxMA
Mouse: Razer DeathAdder amzn.to/3J9fYCf
Computer: 2021 Macbook Pro amzn.to/3J7FXtW
Lens: Canon RF24mm F1.8 Macro is STM Lens amzn.to/3UUs1bB
Caffeine: High Brew Cold Brew Coffee amzn.to/3hXyx0q
More Caffeine: Monster Energy Juice, Pipeline Punch amzn.to/3Czmfox
Building A Second Brain book: amzn.to/3cIShWf

КОМЕНТАРІ: 379
@codetothemoon
@codetothemoon Рік тому
ERRATA: Many of the examples are more verbose than necessary. This is partially due to my lack of expertise in many of the languages, and partially due to the desire to show equivalent constructs in each of the languages. In many cases, the most concise version of the program in that particular language would negate the need to use some of those constructs.
@adicide9070
@adicide9070 Рік тому
do you know if maybe someone has rewritten the Zig example more idiomatically and succinctly? I'd love to see that.
@kintrix007
@kintrix007 Рік тому
I am very surprised you didn't do map and fold in Elixir, and instead opted for a more verbose implementation, basically just doing a fold and a map by hand.
@adamhenriksson6007
@adamhenriksson6007 Рік тому
Yeah, figuring out all the fun and funky things one can do with fold is half the fun of functional programming.
@codetothemoon
@codetothemoon Рік тому
Thanks for pointing this out! Sadly, I don't think the Elixir implementation was the only one that was more verbose than necessary...
@zzzyyyxxx
@zzzyyyxxx Рік тому
@@codetothemoon you might wanna redo this video then
@andrewdunbar828
@andrewdunbar828 Рік тому
@@zzzyyyxxx A followup rather than a redo would be of interest.
@MegaSascuach
@MegaSascuach Рік тому
At least there was only one list enumeration in the elixir program. 😅 I’m sure other language experts are picking apart the other examples too. Either way it was fun to look at elixir code keep it up @Code to the Moon
@Circl3s
@Circl3s Рік тому
Your local Crystal fan here. Not only does it have a really comfy syntax, but the standard library is insane. More people should give Crystal a try!
@codetothemoon
@codetothemoon Рік тому
Nice! Crystal is definitely one of the languages I was most impressed with when preparing this video.
@tsimom
@tsimom Рік тому
yeah man been using crystal for quite some time and kinda gotten addicted... wait do you mean the programming language? /s
@moumous87
@moumous87 Рік тому
0:53 Rust 2:04 Nim 3:14 Julia 3:58 Elixir 4:01 - 5:05 sponsor break: Sidekick 5:06 Elixir resume 5:50 Zig 7:06 Haskal 8:02 Lua 8:35 Crystal
@joshbarghest7058
@joshbarghest7058 11 місяців тому
*Haskell
@woozy_deer
@woozy_deer Рік тому
I think a notable absence is Vale, it's more akin to Rust than any since it has it's own borrow checker and similar performance while claiming to be more concise and flexible.
@codetothemoon
@codetothemoon Рік тому
I agree, Vale looks super interesting! I was actually going to include it in the video, but I ran into a roadblock trying to read stdin to a String. I asked the lead about it on the Vale Discord, and they said it was something they were going to implement. So I figured I'd do a separate video about it - Vale is actually the language I'm hinting about at the very end
@swdev245
@swdev245 Рік тому
For Julia, you could also have replaced the body of the function 'sum_fn' with the line: parse.(Int, splits) |> sum The dot after parse lets the function operate on an array and it pipes the resulting array into the sum function
@codetothemoon
@codetothemoon Рік тому
thanks for pointing this out! several of the solutions in this video are suboptimal, Julia seems to be one of the more egregious cases of that
@mskaarupj
@mskaarupj Рік тому
@@codetothemoon I would also use Vector{
@bluedark7724
@bluedark7724 7 місяців тому
Ahh .. more language syntax that I have no idea what it does.
@karlwaugh30
@karlwaugh30 Рік тому
The pipe operator also appears in F# and it's such an amazing language feature. I miss it.
@codetothemoon
@codetothemoon Рік тому
Ahh I didn't know F# had it too, I do hope it becomes more commonly implemented in new languages
@Michal_Peterka
@Michal_Peterka Рік тому
♥|> F#
@metaltyphoon
@metaltyphoon Рік тому
@@codetothemoon believe it or not but c++ 20 has it 😂
@emoutraspalavras-marloncou4459
@emoutraspalavras-marloncou4459 Рік тому
@@codetothemoon If I am not mistaking the functional programming language OCaml introduced that pipe operator. That was both Elixir and F# have drawn inspiration from. There are very good video series by a professor from the UK on youtube, the series has more than 100 videos and he shows you have to try the code online on the browser, no need to install anything. I got to OCaml when I after falling in loved with F# I was disappointed to find out that F# is roughly totally based on OCml, they look the same when it comes to the functional style, since F# also accept the OOP approach with imperative style. So now I am sticking to OCaml, since it is where F# came partially from. OCml, Haskell, Erlang the Common Lisp are quite a challenge, but these are really interesting programming languages. I am very surprised how many implementation are so concise in these 4 languages.
@pinkorcyanbutlong5651
@pinkorcyanbutlong5651 Рік тому
@@emoutraspalavras-marloncou4459 as someone who is also learning some OCaml and F#, would still say F# is more 'usable' as it can use/be used alongside C# projects, with C# itself bing quite popular; While OCaml is quite niche and honestly just less practical, with it only recently being able to do async and multi-thread without hacks
@JStevenAR
@JStevenAR Рік тому
Nim is a really awesome language. I started learning it a few months ago but I keep finding cool stuff about this language, like the macro system, so easy to use and powerful to make very cool things with metaprogramming. If you haven't used yet, Nim is worth giving a try.
@codetothemoon
@codetothemoon Рік тому
I was impressed by nim as well! Seems like it has a bright future
@agustinpizarro
@agustinpizarro Рік тому
check "sugar" module!
@JStevenAR
@JStevenAR Рік тому
@@agustinpizarro That module always blow my mind!!! 🤯
@bluedark7724
@bluedark7724 7 місяців тому
Meta programming is above my mental ability. I had some excitement for Nim last year, nothing came off it.
@flashas
@flashas Рік тому
Haskell can be even more concise, but no need to (in production code). main = getLine >>= print . sum . map read . words >> main or use interact
@codetothemoon
@codetothemoon Рік тому
wow, nice! thanks for pointing this out!
@pdr.
@pdr. Рік тому
With interact: main = interact $ unlines . map (show . sum . map read . words) . lines This is longer but I think more understandable.
@opposite342
@opposite342 Рік тому
@@codetothemoon the >> and >>= are Haskell's monad binding operators, and from my research it is quite similar to elixir's pipe as well
@Axman6
@Axman6 Рік тому
You can also just use forever instead of the explicit recursion.
@steffahn
@steffahn Рік тому
@@Axman6 I want to second this. Using "forever $ do …" (or "forever do …" with {-# LANGUAGE BlockArguments #-}) reads very much just as well as "loop { … }" in Rust.
@mageprometheus
@mageprometheus Рік тому
Thanks, this was great. I love playing with Rust. After years of watching Jon Gjengset's videos I got his book, Rust for Rustaceans, It's so rare to find intermediate to advanced stuff. I learned erlang and Haskell long ago and have started looking at Elexir. I got into Lua to write pluging in Reaper DAW, it 's faster to knock out than C++, and then fell in love with the wonderful Lua metaprogramming. Take care.
@codetothemoon
@codetothemoon Рік тому
Thanks, I really enjoyed making it. I got Rust for Rustaceans recently as well, it's a fantastic book! I didn't realize Reaper supported Lua as well, seems like everyone is fixated on Neovim's support for it these days
@lardosian
@lardosian Рік тому
Reaper is a great DAW, love it!
@terryriley6410
@terryriley6410 9 місяців тому
lua 'end syntax' and 1 based indexing is annoying to me
@bluedark7724
@bluedark7724 7 місяців тому
Lau is a scripting language... I didn't mind that. It's another level of abstraction. There is a major difference between scripting and programming - one needs a run time, the other doesn't. I think Lua is great as a tool, if your Hong to learn lua, then python, which can be semi-compiled. I find that a language that has support everywhere is easier for me. Rust on everything for example, Python is everywhere... of course the C's are everything, but they are a pain.
@Kruglord
@Kruglord Рік тому
Julia supports the exact same pipe operator as Elixir! You also don't need the begin\end in the map of sum_fn, but I'm pretty sure you only included that as a demonstration of a begin block. There's also the `eachline()` function, which if not passed any args, creates an iterator over stdin. Thus, you can write `for line in eachline()` rather than `while true; readline()`
@codetothemoon
@codetothemoon Рік тому
oh nice I didn't realize this! I'm learning a ton hearing from practitioners of all of these languages - thanks for pointing these things out!
@sharos404
@sharos404 Рік тому
Edit: I just realized someone gave this comment before me. ​​@@codetothemoonIn addition you could do: ``` ints = parse.(Int, splits) sum(ints) ``` Instead of having sum_fn at all. Here the . (dot) is shorthand for broadcast, so it just creates a vector of Integers from the get-go.
@nullpointer1755
@nullpointer1755 Рік тому
The Julia version could be implemented like this: # No need to write type annotations since at runtime Julia will look at the type of the argument and compile the function for that type sum_fn(splits) = sum(parse(Int, x) for x in splits) # Putting the code inside a function makes more performant because of how the Julia compiler works function main() line = readline() splits = split(line, " ") result = sum_fn(splits) @show result end main()
@sashaabramowitz8440
@sashaabramowitz8440 Рік тому
Also wanted to add that Julia has the pipe operator too, not only Elixir
@Kruglord
@Kruglord Рік тому
Yes, I came here to say that, plus it supports the pipe operator too. I actually tend to write my code using a lot of `Base.Iterators.map` and `Base.Iterators.filter` so that they'll be lazy in their execution lol. ```julia parse_Int(x) = parse(Int, x) function main() # Rather than the infinite loop, you can use eachline() to create an iterator of the lines from stdin for line in eachline() line |> split .|> # Broadcast pipe, applies the subsequent function to the elements of the input, rather than the whole input parse_Int |> sum |> println end end main() ```
@bluedark7724
@bluedark7724 7 місяців тому
Rust you might want use the Buffer String type - it strips out the /n /r raw data from the keyboard entry, also your parsing() isn't capturing the Return type, which handles errors.
@tian5185
@tian5185 Рік тому
Would love to see a follow-up with idiomatic code from experts of these languages!
@jasongoldberger1
@jasongoldberger1 Рік тому
As an Elixir expert I deem this code to be not quite idiomatic, but it showcases pattern-match destructuring and recursion which is, IMO, a better show and tell than using the Enum module to map and sum.
@codetothemoon
@codetothemoon Рік тому
Thanks, I'm learning a ton from experts such as yourself in each of these languages. I believe there is representation for every single one in the comments, which is really cool
@NickWindham
@NickWindham Рік тому
Yay, somebody finally mentioning Julia! Might be the best kept secret in all of programming.
@PaulSebastianM
@PaulSebastianM Рік тому
There is no such thing. If something is good in programming, it won't stay a secret.
@MainEditor0
@MainEditor0 Рік тому
So young but beautiful lang with lua-like syntax and LuaJIT speed(and even faster). Who knows maybe if Julia may be older NeoVim devs use it instead of LuaJIT...
@codetothemoon
@codetothemoon Рік тому
It does seem under-appreciated!
@trulyUnAssuming
@trulyUnAssuming Рік тому
I have such a love-hate relationship with julia. But it is becoming my go-to language, so it is doing something right 😅
@valdemarkjaer
@valdemarkjaer 11 місяців тому
I’ve chosen Julia to be my general purpose / ML / DS language for the near future. Hope to work with both RUST and JULIA in health informatics. Let’s see what the future will show.
@XerosOfficial
@XerosOfficial Рік тому
Great video! I would love to see a video about Haxe. One of my favourite languages, with an incredible balance for utility and performance. It also has quite a mature ecosystem for how unknown it is.
@codetothemoon
@codetothemoon Рік тому
Thanks, I've put Haxe on my "to check out" list - which is swelling in size quite rapidly (I see that as a good thing)!
@SianaGearz
@SianaGearz Рік тому
I'm gearing up for a larger project in Rust, and depending how it goes, I might chose Nim for another medium size but much smaller project. Nim looks like SO much fun!
@codetothemoon
@codetothemoon Рік тому
nice!! yeah I was really impressed with nim. Somebody mentioned in the comments that they were working on a borrow checking option - now that would be a game changer
@aNotoriousPhD
@aNotoriousPhD Рік тому
Idk if anyone has mentioned it, but for elixir, one easier way of running that code could've been using a .exs file (elixir script) instead. And then just do `elixir .exs` to run it. It's pretty nice
@intelqa5334
@intelqa5334 Рік тому
Crystal program can be made even more compact. loop do line = gets || "0" puts line.split.map(&.to_i).sum end
@the_mastermage
@the_mastermage 10 місяців тому
Julia is not just optional type annotation, it is optional type checking. If you anotate a variable with a type you cannot assign it another type, the compiler will throw you an error there.
@codetothemoon
@codetothemoon 10 місяців тому
thanks for pointing this out! the distinction is subtle but probably important to be aware of
@juancarlospaco
@juancarlospaco Рік тому
Nim has memory safety with no garbage collector.
@codetothemoon
@codetothemoon Рік тому
I should have qualified that statement a bit more - all of its mechanisms for memory safety involve some kind of runtime overhead - ie reference counting or garbage collection
@jsonkody
@jsonkody Рік тому
Julia synax is inspired more by Ruby than Python. Thx for nice video :D
@codetothemoon
@codetothemoon Рік тому
ahh thanks for pointing this out, that makes more sense!
@astroid-ws4py
@astroid-ws4py Рік тому
And Lua too
@tintindb
@tintindb Рік тому
Zig? I keep learning even when I don't expect to. And btw the comments are also interesting. Oh yeah, there's also Pharo? Great post.
@codetothemoon
@codetothemoon Рік тому
nice! I agree the comments are really insightful - I think I've heard from at least one expert in each of the languages covered in the video which is amazing. Also many point out tech that I hadn't heard of before, this one is a good example. I've put Pharo on my "to look into" list!
@glennmiller394
@glennmiller394 Рік тому
Good subject matter. You used languages I've never heard of. Thanks for the video.
@codetothemoon
@codetothemoon Рік тому
Thanks, part of me was worried folks would say something to the effect of "we've already tried all of these", glad to hear that's not the case!
@ChristopheTroestler
@ChristopheTroestler Рік тому
Thanks for a fun comparison. Note that you do not need to collect the elements of split_whitespace() just to transform them back to an iterator after. Of course the signature of *sum_fn* then becomes slightly longer: fn sum_fn) -> i32 { n.map(|e| e.parse::().unwrap()).sum() } fn main() { loop { let mut buffer = String::new(); stdin().read_line(&mut buffer).unwrap(); println!("{}", sum_fn(buffer.split_whitespace())); } }
@codetothemoon
@codetothemoon Рік тому
Nice, thanks for pointing this out!
@cyanophage4351
@cyanophage4351 10 місяців тому
Crystal is the one I was most interested in here. Thanks for informing me about it. I will be checking it out
@codetothemoon
@codetothemoon 10 місяців тому
nice, yeah I was really impressed with Crystal. Definitely worth a look imo!
@Tablis0
@Tablis0 Рік тому
The Julia example is written in a very unJulia way which does not make good use of features of the language. All the reading and summing can be written very concisely: sum(s->parse(Int,s), split(readline(), " ")) or sum(parse.(Int,split(readline(), " "))) or sum(split(readline(), " ") do s parse(Int,s) end You can divide it somehow to make it more readable, but the version presented for me seem to only make the language look more ugly and confusing.
@BosonCollider
@BosonCollider Рік тому
Just a heads up that Elixir's pipe operator is from F# and that Julia has it as well My overall experience from delving deeper into the languages mentioned here is: 1) Julia, Elixir, and Crystal are both incredibly beautiful and well designed languages that deserve more attention, and by this I do not just mean having a short basic syntax but that their underlying semantics are really well designed. Also they have a great macro system. 2) Nim and especially Zig are incredibly disappointing and trying to use them without gc is a royal road to segfaults. With that said, Zig is easier to write than C or unsafe Rust in the sense that its compiler has less UB, but that also means the compiler is less free to optimize code. 3) Haskell is great, but apart from maybe being a bit further from mainstream languages, I really wish its standard library could be rewritten from scratch with the benefit of hindsight and new language features, and that texts teaching it took algorithms seriously and made data structures like finger trees more prominent 4) Lua could have been the perfect scripting language in the 90s instead of python/ruby/js/php, if its standard library was more batteries-included and if netscape could have picked it.
@astroid-ws4py
@astroid-ws4py Рік тому
And F# took that from OCaml, F# is basically "Microsoft's OCaml" or ".Net OCaml"
@batlin
@batlin Рік тому
Interesting insights. I used to use Elixir quite a lot and really liked it, although it's not the most performant language for compute-heavy work. We used it to replace a Python-based image-processing system in my old job and it not only produced a MUCH more efficient system, but we ended up with far less code to the same job better and with fewer bugs (immutability by default helped here too). I've used Haskell and Nim for fun, and did pretty much all of the 2020 and 2021 Advent of Code challenges in Haskell, and all the 2022 challenges in Nim (and a few in Crystal too). I'd have to disagree with your second point and say that Nim rarely got in my way or crashed, except when I tried to do some invalid casting of bytes to UTF-8, which did produce an unpleasant segfault. Apart from that it's been great. That said, I wasn't using it without a GC, and relied upon the newish "orc" system which is quite frugal. I really enjoyed working with Haskell too, although the purity of the type system started to get kinda heavy when I wanted to use dynamic programming and other mutation-heavy coding styles. Using mutable arrays with the ST monad was okay but occasionally confusing and very difficult to debug (you can't just throw in a print statement anywhere), and there doesn't seem to be a mutable hashmap type in the stdlib, so I had to do weird things like manually mapping keys into an integer space and using a vector/array instead.
@gabriellasso8808
@gabriellasso8808 Рік тому
Actually ORC, one of Nim's memory management strategies, is memory safe without a garbage collector, it has a deterministic performance due to its nature, so it can work like Rust if you annotate your data structures as acyclic and don't mess up (if you mess up, the compiler identifies that and insert cycle detection code to free the memory when needed)
@codetothemoon
@codetothemoon Рік тому
In this documentation I see "ORC" referred to as a "full blown garbage collector" nim-lang.org/blog/2020/10/15/introduction-to-arc-orc-in-nim.html
@deadvirgin428
@deadvirgin428 Рік тому
Reference counting IS garbage collection (even if very lightweight), that's what the "RC" in ORC stands for. Cycle collection is also garbage collection, which is what the "O" in ORC represents. Not to say that there is anything wrong with it, Nim is wonderful and so is ORC. ORC is great because it allows you to prototype your code very easily without being forced to think about memory, you can always come back and optimize later (which IMO is the optimal workflow), you can still use memory pools or whatever memory management scheme you would want to use in Rust, you are just not required to worry about such things from the get-go and just leave it to the GC until you require better performance.
@deadvirgin428
@deadvirgin428 Рік тому
Here is an idiomatic version of the sum procedure in the nim program: proc sum(arr: seq[string]): int = for x in arr: result += parseInt(x)
@BohdanTrotsenko
@BohdanTrotsenko Рік тому
That was very concise, thanks.
@codetothemoon
@codetothemoon Рік тому
you're welcome, thanks for watching - brevity is one of my core tenets 🏎️
@TeamPuzel
@TeamPuzel Рік тому
Swift implementation, with error handling :) while true { print("input: ", terminator: "") print((readLine() ?? "0") .split(separator: " ") .compactMap { Int($0) } .reduce(0, +) ) } It's a really cool language, has extremely good C and C++ interoperability too :) It's often seen as "the Apple language" sadly, but just ignore the obj-c classes and use structs, it's even adding Rust like ownership soon
@v01d_r34l1ty
@v01d_r34l1ty Рік тому
I won't even lie to you, I didn't know Swift worked on anything but Apple products until this comment. Guess it's in the same boat as C# now in the sense that it was originally for a proprietary system but expanded compatibility. Interesting.
@TeamPuzel
@TeamPuzel Рік тому
@@v01d_r34l1ty Yeah, the connection to Apple makes the language look bad; actual Swift code looks completely different from the decades old object oriented apis, but that's what most people will see when they look up Swift on google or UKposts. It also looks really high level, so at first I assumed it must be slower than Rust; there's no semicolons after all 🙂
@lawrencejob
@lawrencejob Рік тому
I would love to see Roc in the next one
@codetothemoon
@codetothemoon Рік тому
that's one i hadn't heard of, thanks for putting it on my radar!
@frittex
@frittex Рік тому
defo gonna try out crystal, thank you
@codetothemoon
@codetothemoon Рік тому
Crystal does seem like a great language - thanks for watching!
@VioletCowelicious
@VioletCowelicious Рік тому
Haskell has the pipe operator too :) It's called (&) and it's in Data.Function. Because Haskell lets you define your own operators, it's not built-in syntax, and you could define it yourself. You can even write `(|>) = (&)` to get the same syntax as elixir. It passes the LHS as to the RHS as its last parameter, instead of its first parameter. (Although of course all functions in Haskell only take one parameter because of currying).
@codetothemoon
@codetothemoon Рік тому
ahh thanks for pointing this out!
@raianmr2843
@raianmr2843 Рік тому
You should've covered Clojure, or any flavor of lisp tbh. Picking a slightly more interesting program next time would be much appreciated. The quicksort algorithm, for example, can have vastly different implementations depending on the paradigm. That said, thanks for the video, really enjoyed the comparisons.
@codetothemoon
@codetothemoon Рік тому
Thanks for the feedback, maybe in the next one!
@batlin
@batlin Рік тому
Nice intro -- it's great to see really nice lesser-known languages like Nim and Crystal get more of a spotlight. BTW, for this type of program in Haskell you can sometimes use "interact", although it can behave surprisingly due to laziness, so I had to use lines / unlines in this example: sum_fn = sum . map (\s -> read s :: Integer) main = interact $ unlines . map (show . sum_fn . words) . lines
@Gelo2000origami
@Gelo2000origami Рік тому
This should be made into a repo and take in PRs so we get the best of the best in each language
@codetothemoon
@codetothemoon Рік тому
it's all here! will gladly accept PRs. github.com/Me163/youtube/tree/main/RustVsSevenLanguages
@benitoe.4878
@benitoe.4878 4 місяці тому
Thanks for presenting an example of Julia code as well.
@Speykious
@Speykious Рік тому
For the Haskell solution, you can also write sum_fn like this: sumFn = sum . map (\s -> read s :: Integer) This is function composition and eliminates the need to write the argument that is being taken.
@Axman6
@Axman6 Рік тому
sum . map (read @Integer) The whole app can just be: main = forever (print @Integer . sum . map read . words =
Рік тому
other nim version import std/strutils import std/sugar # Syntactic sugar for several things, in this case anonymous functions "=>" import std/sequtils proc sum_fn(the_arr:seq[string]): int = the_arr .map(x => x.parseInt) .foldl(a+b) while true: stdin .readline .split .sum_fn .echo in nim, sum(mult(x)) is the same that x.mult().sum() or whitout paramters x.mult.sum
@matthiasschuster9505
@matthiasschuster9505 Рік тому
9:40 You declared yourself at the beginning of the video, that Nim has reference counting and manual memory management By the way: There you said actually something that could be considered different as you intended You said: Nim has manual memory management - like C. MM in Nim is actually a lot more concise and simple than in C I know you meant its manual in both ways, and this is true Its just not manual in the same way ;) P.S: Nim has also a borrow checker in the works
@mohanaggarwal4058
@mohanaggarwal4058 4 місяці тому
U can improve ur nim code a bit: import strutils, stdutils proc sumFn(theArr: seq[string]): int = theArr.mapIt(it.parseInt) .foldl(a+b) while true: let line = stdin.readline arr = line.split sum = sumFn(arr) echo sum # end of program Note: No need to use different lines to import different modules, nim compiler removes underscores from indentifiers so it is better to use camelCase instead of snake_case, use mapIt to improve readability and for writing concise code, no need to write return or result at the end of function, function will return that value automatically. Multiple let or var or const statements can be combined into one, thus making code more concise.
@DNA912
@DNA912 Рік тому
I used haskell in a universaty course about programming languages paradigms and general stuff. I haven't used in since, but I really do remember how beautiful and concise the code was
@codetothemoon
@codetothemoon Рік тому
yeah it sure is - i had to use ocaml for a course in university, and I remember being really frustrated with it. I was too shortsighted to appreciate the beautify of it back then, but I sure do now
@sp.n7401
@sp.n7401 Рік тому
Do Nim's ARC/ORC memory models compete with Rust's memory safety? My understanding is during compile time it will automatically insert memory de-allocations similar to how Rust does it.
@codetothemoon
@codetothemoon Рік тому
I'm definitely not a nim expert - but my understanding was that you can opt-in to reference counting or garbage collection if you want to guarantee memory safety, but if you opt out of that, you incur the same memory management risks that you would have with C/C++. Somebody jump in here if this is wrong!
@agustinpizarro
@agustinpizarro Рік тому
@@codetothemoon It is not "safe" as Rust, but ARC/ORC are deterministic memory handling as C++ but with some memory handling done automatically for you, like you'll do with shared_ptr in C++
@seanknowles9985
@seanknowles9985 Рік тому
Zig is next level, will take the crown very soon 👑
@codetothemoon
@codetothemoon Рік тому
interesting, why do you think that is?
@seanknowles9985
@seanknowles9985 Рік тому
@@codetothemoon Great question. So off the top, coming in hot. - Simple syntax: Zig has a simple syntax comparable to Go, maybe even easier due to it's clever error handling and that it launched with generics. - No garbabe collector: The issues with Nim as much of the standard library is already relient on the GC. - Easier to code in the Rust, my god, Rust is amazing but imagine coupling the safety of Rust with a Golang Syntax = Zig. - Complete compatibity with C libs and C++ - The cross compilation is super fast and is being used by other languages to hit those targets, other languages are leveraging Zig already for compliation work. - C speed, and faster than C with better compiler. Basically if your a C developer you're being advised to use Zig to compile your C code. Zig will replace C, that's without a doubt. I even speculate it replacing Golang down the line as the memory management is easy. So why comprimise on speed when you don't need to. Zig has about two years of catch up planned to reach 1.0 and for editor tooling to evolve.
@nicktreleaven4119
@nicktreleaven4119 Рік тому
@@seanknowles9985 does zig have a 100% memory safe subset? That's required by the US NSA guidelines.
@seanknowles9985
@seanknowles9985 Рік тому
@@nicktreleaven4119 good question! I'll throw it to the development team :)
@randomthings1553
@randomthings1553 Рік тому
I would also add clojure for how short code can look, also clojure has very interisting way oh hadling/embracing null/nil intead of avoiding it. (->> (s/split (read-line) #",") (map #(Integer/parseInt %)) (apply +) (println))
@codetothemoon
@codetothemoon Рік тому
oh nice that one is really concise too! I'm still wrapping my head around the Lisps, might have more coverage of them once I find my footing
@navi93243
@navi93243 11 місяців тому
Nice, For people who are interested in data science, Julia is a great language to explore! I moved part of my workflow from Python to Julia.
@codetothemoon
@codetothemoon 11 місяців тому
I was indeed pretty impressed with Julia. With Mojo coming along, it'll be interesting to see how the landscape evolves in the coming years
@frittex
@frittex Рік тому
love the typing sound
@codetothemoon
@codetothemoon Рік тому
Thanks, I actually mic'd up the keyboard for this one thinking folks might like it
@frittex
@frittex Рік тому
@@codetothemoon it adds a little more "depth" to the typing animation, and overall the sound you got is just nice to the ear
@hipertracker
@hipertracker 9 місяців тому
Elixir has loops and reduce (foldr). It does not need to use recursion. E.g you could use: def sum_fn(strs) do List.foldl(strs, 0, fn s, acc -> String.to_integer(s) + acc end) end or def sum_fn(strs) do Enum.reduce(strs, 0, fn s, acc -> String.to_integer(s) + acc end) end
@codetothemoon
@codetothemoon 9 місяців тому
thanks for pointing this out, this is much cleaner! I've been learning a ton from the reactions of the experts in each language showcased in this video.
@elau1004
@elau1004 7 місяців тому
Can do a review on the V language? Thanks.
@codetothemoon
@codetothemoon 7 місяців тому
actually already did! if you want the original version it's here: ukposts.info/have/v-deo/oqJieXiYg6Gbx5c.html BUT if you want the version with Primeagen commentary (recommended) you can find that here: ukposts.info/have/v-deo/omRofKFsqHtqoqc.html
@jsonkody
@jsonkody Рік тому
Julia synax is inspired by Ruby, not Python. Thx for nice video :D
@codetothemoon
@codetothemoon Рік тому
ahh thanks for pointing this out, that makes more sense!
@modolief
@modolief Рік тому
Superb video!!
@codetothemoon
@codetothemoon Рік тому
thank you!
@bocckoka
@bocckoka Рік тому
That Rust implementation made me screech autistically a bit. split_ws -> parse -> sum, no collect.
@codetothemoon
@codetothemoon Рік тому
Wow yes, thanks for pointing this out! Sadly I don't think it's the only version of this program that I implemented sub optimally...
@matroqueta6825
@matroqueta6825 Рік тому
Julia also has Elixir's |> operator, it does the same thing! On behalf of all Julians I demand that the Elixirites share their trophy with us... or give us a copy of the trophy (we respect that functional programmers dislike shared state)
@chocolate4212
@chocolate4212 Рік тому
More concise Crystal version: def get_sum(splits) splits.map(&.to_i).sum end loop do gets.try do |line| puts get_sum(line.split) end end You don't even need the get_sum method: loop do gets.try do |line| puts line.split.map(&.to_i).sum end end Overall, nice video.
@caddr
@caddr Рік тому
there is function composition operator (.) in haskell that works like pipe operator (|>), but in reverse
@codetothemoon
@codetothemoon Рік тому
Actually this one was on my radar, but for some reason I never really thought of it as the reverse of the pipe operator!
@rayanemesbah7815
@rayanemesbah7815 5 місяців тому
In rust I think you need to specify the type for collected because the compiler can't infere it
@thedeemon
@thedeemon Рік тому
|> is just function application (call), same as $ in Haskell just in opposite direction. Nothing to do with map.
@HalfMonty11
@HalfMonty11 Рік тому
Interesting video... but I like the end "Has my love for rust waned? It hasn't" Very good point. Other languages can have maybe better syntax changes or some ergonomics but that's not worth losing memory safety with no garbage collection. If some other language tried to implement memory safety with no garbage collection with better ergonomics... Rust would probably just adopt those ergonomics too because it's constantly improving and gaining ergonomics. It's hard to even fathom something better unless there's some massive rift/scandal in the main contributors and people who manage and use the language which would be surprising because the community is so good.
@kimdilotmane2708
@kimdilotmane2708 Рік тому
You have such a very good Doom Emacs config can we get the link to it please ?
@codetothemoon
@codetothemoon Рік тому
I'm not doing anything special, I just uncommented the relevant programming languages in init.el and switched to the monokai-pro theme. The only real config I had to add was some stuff to get Zig working, since I don't think DOOM supports it out of the box.
@agustinpizarro
@agustinpizarro Рік тому
nim callback can be made simpler with "sugar" module
@codetothemoon
@codetothemoon Рік тому
thanks, didn't know about this!
@jenreiss3107
@jenreiss3107 Рік тому
the haskell implementation would look nicer by using the `interact :: (String -> String) -> IO ()` function as well as using the point free style: `main = interact (print . sum . map read . words) >> main`. I'm surprised your editor didn't say "Hey, eta reduce!"
@chaddaifouche536
@chaddaifouche536 Рік тому
Good idea, though remember that interact want (String -> String) so no print in your function. You also don't need to recurse, if you want to do "f" line by line, you can always use `interact (unlines . map f . lines)`. Here, with TypeApplications to specify the Read type elegantly, you can do this : interact $ unlines . map (show . sum . map (read @Integer) . words) . lines
@jenreiss3107
@jenreiss3107 Рік тому
@@chaddaifouche536 amazing. love the type application! thanks for the correction too, I didn't think interact automatically recursed
@EdouardTavinor
@EdouardTavinor Рік тому
In lisps the pipe is called the threading macro btw. I get scared looking at that rust code.
@jpVoltDeveloper
@jpVoltDeveloper Рік тому
Thanks!
@codetothemoon
@codetothemoon Рік тому
thank you so much for your support!!!
@SchmidiAUT
@SchmidiAUT 2 місяці тому
I feel the Julia version is unnecessary verbose, one could just write: while true readline() |> split .|> (x -> parse(Int, x)) |> sum |> println end So in Julia this exercise takes only 3 lines of code.
@wlockuz4467
@wlockuz4467 Рік тому
Am I missing something or did you miss the split delimiter in Nim example and passed an empty delimiter instead of a space for Julia example?
@fabianthorsen8925
@fabianthorsen8925 Рік тому
Which font are you using? Amazing video, keep up the good content!
@sighawrylofi
@sighawrylofi Рік тому
I think it's monolisa, could be wrong
@kellywilson7585
@kellywilson7585 Рік тому
Zig solution in 15 lines. Use anytype for parameter type inference in sum_nums declaration. The one line while loop in sum_nums is idiomatic (as long as it isn't more than 80 chars): const std = @import("std"); fn sum_nums(it: anytype) !i32 { var sum: i32 = 0; while (it.next()) |string| sum += try std.fmt.parseInt(i32, string, 10); return sum; } pub fn main() !void { var buf: [10]u8 = undefined; while (try io.getStdIn().reader().readUntilDelimiterOrEof(buf[0..], ' ')) |user_input| { var splits = std.mem.split(u8, user_input, " "); std.debug.print("{} ", .{try sum_nums(&splits)}); } }
@adicide9070
@adicide9070 Рік тому
this "while" line is like the most dense matter in the universe :D
@recklessroges
@recklessroges 11 місяців тому
@@adicide9070 oh I thought it was just perl 😀
@lunalect
@lunalect Рік тому
Julia has the |> operator too…
@shaileshsundram
@shaileshsundram Рік тому
What app(s) are you using for screencasting, esp the terminal window?
@codetothemoon
@codetothemoon Рік тому
Just OBS for the screen recording!
@dalek6779
@dalek6779 Рік тому
Here's a surprise you didn't see coming. Let's not forget about functional programming's cousin, array programming. Trivial in APL +/⎕
@codetothemoon
@codetothemoon Рік тому
heard a little bit about APL, definately one I'd love to look into
@samifouad
@samifouad 28 днів тому
would love to see Gleam in the next edition of this video!
@codetothemoon
@codetothemoon 24 дні тому
yeah i've been itching to give Gleam a spin, it looks really nice!
@salvadoraugustovitko1396
@salvadoraugustovitko1396 9 місяців тому
Please talk more about crystal, I really love the language but it doesn't have much traction😢
@codetothemoon
@codetothemoon 9 місяців тому
I was really impressed by it too and was also surprised by its lack of popularity. i hope to be able to cover it more!
@smalltimer666
@smalltimer666 3 місяці тому
Julia also has an excellent pipe operator !
@matthiasschuster9505
@matthiasschuster9505 Рік тому
5:35 If you like that feature, you might love FSharp, who come up with this operator and is very cute overall Like Elixir, just statically typed with the most accurate type inference you had ever seen And dotnet as a platform
@terryriley6410
@terryriley6410 9 місяців тому
stop spreading this misinformation, the pipe operator has been around long before f#
@JaycenGiga
@JaycenGiga Рік тому
If you really want to see this problem solved concisely, you should look at APL. I’m not sure if my implementation is correct (I am a beginner at APL and I don’t have an interpreter available right now) but it should be something like the following: {⎕←+/⍎v ←⎕ ⋄ ∇} The braces denote an anonymous function, and ⋄ separates statements. The first statement does IO and all the logic, and the second one is the recursion. It should be possible to do be even more concise (without an anonymous function) but my APL is not good enough to know how to do it.
@dalek6779
@dalek6779 Рік тому
It's even easier than you think, but kudos for learning. Try this +/⎕ ⎕ (quad) by itself means read a line from standard input and evaluate as an APL expression. So if you type in a bunch of integers it'll turn it into an integer array. Then obviously +/ just sums it up and outputs.
@diktomat
@diktomat Рік тому
Hah! Actually tried all of them last month, when I did Advent of Code using a different language each day 😂
@kamertonaudiophileplayer847
@kamertonaudiophileplayer847 Рік тому
I also wrote a blog for the topic.
@kebien6020
@kebien6020 Рік тому
How to get the |> operator in Haskell: infixl 1 |> x |> f = f x Alternatively, it is already in the base library, you just have to import it. It's called & but you can alias it to |> if you want import Data.Function ((&)) (|>) = (&)
@MennoSmits
@MennoSmits 8 місяців тому
Pony would be another interesting one to compare
@francisking708
@francisking708 3 місяці тому
If you like the pipe operator in Elixir, you might also like F#
@codetothemoon
@codetothemoon 3 місяці тому
oh nice I'd like to check it out! definitely heard F# mentioned in the context of underrated/overlooked languages....
@fsharplove
@fsharplove Місяць тому
As of today, one language should be very special to make me ever consider using it. F# is awesome!
@fsharplove
@fsharplove Місяць тому
@@codetothemoon Yes, this is my takeaway from learning and using F#. It is a very underrated language and popular languages are not always the best ones.
@pikachu7748
@pikachu7748 Рік тому
I think Julia has the same pipe operator as Elixir
@codetothemoon
@codetothemoon Рік тому
thanks for pointing this out!
@AndrewBrownK
@AndrewBrownK Рік тому
excellent compact video that doesn't waste any time
@Borkolini
@Borkolini Рік тому
How about Clojure?
@kcvinu
@kcvinu Рік тому
There are Odin, C3 & D. Please try them also. Anyhow, I didn't understand a single line from your rust code.
@codetothemoon
@codetothemoon Рік тому
Thanks for pointing these languages out, I've heard Odin mentioned a few times. Would be happy to walk you through the Rust code if you like!
@kcvinu
@kcvinu Рік тому
@@codetothemoon Oh, that's great. I would like to use this opportunity to ask some questions. 1. You declared the buffer as mutable. So the compiler can understands that buffer is mutable. But why we need to use &mut with buffer in next line ? Didn't the compiler already understand that? 2. What does unwrap() does ?
@brdrnda3805
@brdrnda3805 Рік тому
@@kcvinu 1. Rust is sometimes quite explicit not because the compiler couldn't understand it but to have it explicitly mentioned in the code for the human reader. Something with references. In C++ you define whether something is passed by reference in the function header and you don't see it in the call at all. So it's more like: This is important information for the reader of the calling code / code that uses a function, so it has to be given explicitly. Rust is not very explicit in other cases (type inference is heavily used) Edit: That's not a Rust invention, if I remember correctly C# is also already more explicit than C/C++ with these things) Edit2: You could also immutably pass a mutable String to a function. So the let mut buffer says it can be mutated (in general) and the read_line(&mut buffer) says it can mutated within read_line. 2. There are two things, which are very commonly used in Rust: Option and Result. Option is used when you expect a value, but it could also be that there is no value - the "no-value" is called None. It's basically the replacement for Null. Result is you expect a result (from a function) but it could be that you get an error. It's Rust's error handling (instead of try..catch or the different variants of error return values in C). What both have in common is, that you Rust forces you to deal with the "extra" cases (None, Error). The "dirty" way is using unwrap(), which says: I know what I'm doing, there is a value inside this Option/Result, just unwrap it and give it to me. If you're wrong and there is a None or an Error the program will crash.
@Zaddish2
@Zaddish2 Рік тому
Hey! What theme are you using???
@matthewscott336
@matthewscott336 Рік тому
Julia is underrated
@codetothemoon
@codetothemoon Рік тому
I'd have to agree!
@user-dc9zo7ek5j
@user-dc9zo7ek5j Рік тому
Hey, would have been cool to actually show comparison between 'business' LOC and ones that glue it in-between. I never actually expected zig to be so cumbersome. Anyways here is the same thing in C#, I think it's a mix of both worlds in terms of expressiveness and fast-ness Console.WriteLine(Console.ReadLine().Split(' ').Sum(int.Parse));
@rafaelrosa3841
@rafaelrosa3841 Рік тому
Zig is a simple language as c is. So it can be more verbose but the learning is easier and the code is more predictable.
@terryriley6410
@terryriley6410 9 місяців тому
c# is just wrong, pointless overuse of capital letters and braces.
@user-dc9zo7ek5j
@user-dc9zo7ek5j 9 місяців тому
@@terryriley6410 cs is the other name that it has, but still has 2 characters :(
@debasishraychawdhuri
@debasishraychawdhuri Рік тому
All these languages are just a little collaj of existing language features. Rust is just new. The borrow checker is a completely new feature and it is the best way to statically manage memory.
@codetothemoon
@codetothemoon Рік тому
I agree that the borrow checker takes the cake for the most recent language feature that is truly innovative
@jedediah-fanuel
@jedediah-fanuel Рік тому
Crystal gang
@WhiterockFTP
@WhiterockFTP Рік тому
Nim certainly is trying hard to have the memory safety of Rust.
@Lambdaphile
@Lambdaphile Рік тому
Bo love for Lisp family languages? :( Wish there was Clojure also.
@sbditto85
@sbditto85 Рік тому
FWIW I believe Haskells & operator in Data.Function is the same as elixirs |> … doesn’t look as cool though.
@codetothemoon
@codetothemoon Рік тому
ohh I didn't know about this, I'll give it a spin - but yes I agree |> is much more aesthetically pleasing 🙃
@shawnlee6775
@shawnlee6775 Рік тому
I wanted to use Crystal for a long time but Windows support was no good at the time. I ended up downloading and installing V, it was easy and quickly to get going, I learnt most of the basic syntax in less than a week. Top G Video!
@UliTroyo
@UliTroyo Рік тому
Isn't V nice? It's so simple, but it's so fully featured (except for async). I love the UI and CLI modules.
@thedeemon
@thedeemon Рік тому
V has a long history of overpromising and underdelivering. Like promising Rust-like automatic memory management without lifetime annotations and GC and ending up with just leaking memory and using a conservative GC. V is for vapourware.
@MainEditor0
@MainEditor0 Рік тому
Will be there video about your DoomEmacs?
@codetothemoon
@codetothemoon Рік тому
Would you be interested in that? I'd love to make one, I've been unsure about whether folks find editor related videos valuable - especially because I believe most folks watching are using VSCode. Also I'm still on the fence on whether to move forward with Doom or Neovim...
@MainEditor0
@MainEditor0 Рік тому
@@codetothemoon I'm personally a NeoVim user but I'm would like to see what deal on the "other side". So I'm would very like to watch video about that
@guilherme5094
@guilherme5094 Рік тому
Really nice👍
@codetothemoon
@codetothemoon Рік тому
thank you!
@jedisct1
@jedisct1 Рік тому
Zig is so much fun to use.
@codetothemoon
@codetothemoon Рік тому
what makes it fun? I got a bit of exposure to it while making this video, but I still don't feel like I have the whole picture...
@mskiptr
@mskiptr 5 місяців тому
In Haskell instead of using map with a lambda you can use list comprehension. It's significantly more readable imo sum_fn ws = sum $ map (\s -> read s :: Integer) ws sum_fn ws = sum [read s :: Integer | s > main
@mskiptr
@mskiptr 5 місяців тому
After seeing Haskell's `interact` mentioned in the comments, I figured out how to make the Idris version even sleeker: import System.REPL import Data.String main : IO () main = repl " " (cast . sum . map cast . words)
Rust Powered Polymorphism ⚡️ With Traits
9:55
Code to the Moon
Переглядів 87 тис.
Vlang: The language of 2023?? | Prime React
35:53
ThePrimeTime
Переглядів 127 тис.
🔥 Україна виходить у ФІНАЛ ЄВРОБАЧЕННЯ-2024! Реакція alyona alyona та Jerry Heil #eurovision2024
00:10
Євробачення Україна | Eurovision Ukraine official
Переглядів 317 тис.
Не пей газировку у мамы в машине
00:28
Даша Боровик
Переглядів 1,6 млн
Gleam 1.0 Is Out! (I think I'm in love...)
33:26
Theo - t3․gg
Переглядів 176 тис.
The Vlang Drama
43:35
ThePrimeTime
Переглядів 93 тис.
OCaml in 90 Seconds
2:07
Carrio Code
Переглядів 5 тис.
Will Ada Replace C/C++?
44:57
Tsoding
Переглядів 66 тис.
Comparing 10 programming languages. I built the same app in all of them.
21:33
From $erverless To Elixir | Prime Reacts
22:34
ThePrimeTime
Переглядів 90 тис.
Why Rust is NOT a Passing Fad...
8:54
Travis Media
Переглядів 22 тис.
Rust is not a faster horse
11:37
No Boilerplate
Переглядів 314 тис.
Every Programming Language Ever Explained in 15 Minutes
15:29
Flash Bytes
Переглядів 228 тис.
Gleam for Impatient Devs
8:46
Isaac Harris-Holt
Переглядів 41 тис.
СЛОМАЛСЯ ПК ЗА 2000$🤬
0:59
Корнеич
Переглядів 2,2 млн
Я Создал Новый Айфон!
0:59
FLV
Переглядів 2,9 млн
ПОЛГОДА строгого режима для Lenovo Legion 5 pro. Чем может обернуться обычный ремонт ноутбука?
31:47
ААНТ КОНТАКТ Сервис по ремонту техники в ЕКБ, СПБ
Переглядів 32 тис.
Samsung UE40D5520RU перезагружается, замена nand памяти
0:46
Слава 100пудово!
Переглядів 3,8 млн
How much charging is in your phone right now? 📱➡️ 🔋VS 🪫
0:11