10 ms·
Rust is not Scala
by mtanski 10y ago
Rust is not Scala
- lmm 10y agoIt's not, but they're both ML-family languages. I had hoped that Rust would be able to offer a similar level of elegance to Scala.
- bluejekyll 10y agoScala and Rust are not trying to solve problems in the same way though. Rust is a zero-overhead language. Scala has never tried to make that claim.
- nercury 10y agoLooking at the example, I can't find a thing in there that is not required (except the obvious `:Vec<_>` type that is not required). We need `.iter` to tell that iterator is imutable (there is mutable version), we need `.collect` to actually run the iteration. I also don't think objects should have default constructor functions. It may be possible to implement `.zipped` on tuples though.
- steveklabnik 10y ago> (except the obvious `:Vec<_>` type that is not required) It is required, otherwise, `collect()` doesn't know what type of collection to collect into. > It may be possible to implement `.zipped` on tuples though. This is impossible without varargs, no?
- pcwalton 10y ago> This is impossible without varargs, no? Nah, you'd just implement it as an impl over and over on tuples of reasonable size (up to 16 or so). It wouldn't be elegant, but it'd work in practice.
- steveklabnik 10y agoOh right, I see now.
- __s 10y agoThis technique is used a lot by specs: https://github.com/slide-rs/specs/blob/master/src/join.rs#L50 https://github.com/slide-rs/specs/blob/master/src/join.rs#L5...
- cbreeden 10y agoWhich is what itertools does I believe https://bluss.github.io/rust-itertools/doc/itertools/struct.Zip.html https://bluss.github.io/rust-itertools/doc/itertools/struct....
- nercury 10y ago> `collect()` doesn't know what type of collection to collect into. Yes, unless there is some other mention of Vec, i.e. it is returned from the function. > This is impossible without varargs, no? Varargs would definitely help, and would allow Rust to borrow more ideas without workarounds. But one can do it manually for each tuple variant.
- nimish 10y agoHaskell just brute force implements zipWith3, zipWith4 etc Not elegant, but it works.
- tome 10y agoThe un brute force way is to use the Applicative instance of ZipList.
- epidemian 10y agoYep, but it's still an unfortunately big difference in expressiveness between the two languages, where one might think that they would be more or less on par, given the languages' similarities (both are more or less contemporary in their design, statically typed, with similarly powerful type systems, etc). My question is then: is there something fundamentally preventing Rust from achieving similar levels of expressiveness to this Scala example without incurring in unnecessary runtime overhead? Given that both languages are statically typed, my inclination would be to say no: the information is there, the compiler should be able to figure it out. But, alas, i know very little about these things, hence my question :) Update: pcwalton gave some nice insight on why Rust needs some of these constructs on an uncle comment.
- Veedrac 10y agoActually Rust already has izip and IntoIterator that covers the early differences. The type annotation `Vec<_>` is also optional since it can be inferred from the context. The `map` is uglier in Rust because * the comparison was against a constructor with positional, rather than named, arguments * the Scala code didn't need to dereference any arguments, and * and Scala's `Zipped` is a special type with a special `map` function that takes three arguments, unlike a normal iterator. The first and last points could be easily copied in Rust: you'd build a constructor for HuffmanCode and augment iterators of tuples with with a starmap method (that can be done in a library). The middle point can be done before the zipping. The result would be let codes = izip!(data_table.iter().cloned(), code_lengths, code_table) .starmap(HuffmanCode::new) .collect(); Rust's collect is never implicit, like Scala's CanBuildFrom. This prevents accidental collects, which helps writing fast code, but in principle I don't see why it couldn't be implicit - it would just require the whole standard library to be overhauled.
- Veedrac 10y agoActually the last two `.iter()` aren't needed at all because of IntoIter. All three can all be removed if you use Itertools' izip! let codes: Vec<_> = izip!(&data_table, code_lengths, code_table) .map(|(&value, length, code)| { HuffmanCode { length: length, code: code, value: value, } }) .collect(); (If HuffmanCode was a tuple type, this could even be #![feature(fn_traits)] let codes: Vec<_> = izip!(data_table.iter().cloned(), code_lengths, code_table) .map(|args| (&HuffmanCode).call(args)) .collect(); but now I'm just playing around.)
- gmfawcett 10y agoIt's a bit of a stretch to call Scala and Rust "ML-family languages".
- cfallin 10y agoNot at all -- there's a lot of commonality in the language designs. Core language features: both have algebraic datatypes like ML ("case classes" in Scala, enums in Rust), and a 'match' statement that makes working with them easy. Both have pervasive destructuring that works with match arms, 'let' bindings, etc. Data structures and mutation: both encourage a pragmatic version of immutability -- use values and provide functional interfaces primarily, but support mutation where needed. In Rust there are 'Cell' and 'RefCell' types, just like ML's cell-based interior mutability. Library idioms: all three have the standard set of functional-style higher-order transformation functions ('map', 'filter', etc. -- Rust in particular has a very rich Iterator trait API), and it's idiomatic to use these. FWIW, Rust's first/bootstrap compiler was written in OCaml and there's a lot of obvious influence.
- gmfawcett 10y agoI'm aware that Rust's bootstrap compiler was written in Ocaml. And yes, there are lots of ML inspirations in the language -- but not enough to make an ML language. Where are the module functors, for example? Many would consider that an essential element of an ML language, and it is a stretch to call a language an ML family member without them. I don't want this to devolve into a No True Scotsman debate, I just feel that the parent post -- which decried a lack of similarity between Rust and Scala because they were both in the ML family -- was founded on a weak premise.
- premium-concern 10y agoI think ML-family is fine. Scala is much closer to ML than what people sort into the Algol-family or C-family groups.
- saosebastiao 10y agoA module functor is nothing more than a higher order module...one that takes as a parameter some data structure that conforms to an interface (signature). Rust may not have constructs called module functors, but it definitely has constructs that are capably equivalent, as well as some that are even more powerful (a claim that applies to Scala as well) For me, type inference, strong typing, and ADTs with pattern matching puts a language into the ML family quite easily.
- pcwalton 10y agoThe biggest difference between Scala and Rust here is ".iter()", which is necessary mainly because you need to specify whether the iterator iterates over references (".iter()") or passes results by value (".into_iter()"). Because Java doesn't have value types and has no concept of move semantics, it has no need for the distinction, while Rust does. In effect, the "elegance" of Scala here is really the product of its heavy GC dependence. So the comparison isn't as meaningful as it might seem.
- saosebastiao 10y agoWouldn't the type guarantees provided by rusts type system allow for different implementations based on whether it was called on value types vs reference types? I thought there was an auto specialization feature created a while ago that did something like that.
- foota 10y agoI think the into iterator version borrows the references (did I say that right?
- Tuna-Fish 10y agoIt allows changing implementation depending on what you pass it, but it cannot change how you pass things. The issue is that if you just call zip(code_lengths), then ownership of code_lengths is passed into the function and it ceases to work in it's parent.
- eddyb 10y agoThe example can be written like this though: let codes: Vec<HuffmanCode> = data_table.iter() .zip(&code_lengths) .zip(&code_table) .map(|((&value, &length), &code)| { HuffmanCode { length: length, code: code, value: value, } }) .collect();
- Veedrac 10y ago