Hot answers tagged

98

The reason people say functional languages are better for parallel processing is due to the fact that they usually avoid mutable state. Mutable state is the "root of all evil" in the context of parallel processing; they make it really easy to run into race conditions when they are shared between concurrent processes. The solution to the race conditions then ...


71

This should not be equality because it breaks transitivity. Consider these two expressions: new Point3D(10, 20, 50).equals(new Point2D(10, 20)) // true new Point2D(10, 20).equals(new Point3D(10, 20, 60)) // true Since equality is transitive, this should mean that the following expression is also true: new Point3D(10, 20, 50).equals(new Point3D(10, 20, 60))...


59

It's because it's important for humans to recognize that functions are not just "another named entity". Sometimes it makes sense to manipulate them as such, but they are still able to be recognized at a glance. It doesn't really matter what the computer thinks about the syntax, as an incomprehensible blob of characters is fine for a machine to interpret, ...


55

I'll talk about Akka/Scala, because I'm not familiar with Gpars nor with Akka/Java. In Scala 2.10, which includes the relevant part of Akka in the standard distribution, a Future is essentially a read-only reference to a yet-to-be-computed value. A Promise is a pretty much the same except that you can write to it as well. In other words, you can read from ...


51

Here are a few reasons, which might be more or less compelling for you, depending on your own preferences: Do not simply discount it for being "syntactic sugar". While you may say that something is just syntactic sugar, it is after all the sugar that sweetens your life - as a programmer just as well as a coffee or tea drinker. Singletons - every Scala ...


48

I think the reason is that most popular languages either come from or were influenced by the C family of languages as opposed to functional languages and their root, the lambda calculus. And in these languages, functions are not just another value: In C++, C# and Java, you can overload functions: you can have two functions with the same name, but different ...


43

There is an easy, but boilerplate heavy way to seal classes in Java. You put a private constructor in the base class then make subclasses inner classes of it. public abstract class List<A> { // private constructor is uncallable by any sublclasses except inner classes private List() { } public static final class Nil<A> extends List&...


39

There's one thing that you can do concisely and efficiently in Java that you can't in Scala: enumerations. For everything else, even for constructs that are slow in Scala's library, you can get efficient versions working in Scala. So, for the most part, you don't need to add Java to your code. Even for code that uses enumeration in Java, there's often a ...


37

This has nothing to do with functional programming - you can find this kind of situation in context of any other programming language - developers who love the advanced constructs of "their" language so much that they ignore any common sense about readability and keeping things simple. I have encountered such a situation in C, C++, Perl, Java, C#, Basic, and ...


35

One more benefit is that objects can implement interfaces/traits, unlike static methods.


34

Memory management is utterly relevant since it governs how fast something appears even if that something has a great deal of memory. The best and most canonical example are AAA-title games like Call of Duty or Bioshock. These are effectively real-time applications that require massive amounts of control in terms of optimization and usage. It's not the usage ...


34

I'll give a short answer due to my lack of time at the moment, but I'm currently working on two big projects (> 100.000 LOC in Haskell) - flowbox.io and luna-lang.org. We use Haskell for all the parts, including the backend, compiler of our programming language and even the webGL based GUI. I have to admit that the strong type system and the "dependent type"-...


32

"All" programming languages run on x86, so how can they be much different from each other? Brainfuck and Haskell are both Turing complete, so they can both do the exact same tasks. There's a bit of room for syntax changes, syntax sugar and compiler magic in between. You can do quite a lot in there, but there is always a limit. In your case, it's JVM byte ...


30

The real brick was the memory allocation - at worst, Java allocated a whopping 52x more memory than C, and 25x more than C++. Do you understand the numbers you base your question upon? How much memory was allocated? What were the programs doing? When there's a big disparity between those Java and C programs, it's mostly the default JVM memory ...


30

Well, one thing that's important to do whenever we have a discussion like this is to clearly distinguish between object relational mappers ("ORM") and database abstraction layers. An ORM is a kind of database abstraction layer, but not all database abstraction layers are ORMs. One good tool to study to grasp this is Python's popular SQLAlchemy library, ...


29

Checked exceptions are mostly considered a failure. Note that no languages created after Java adopted them. See http://www.artima.com/intv/handcuffs2.html, http://googletesting.blogspot.ru/2009/09/checked-exceptions-i-love-you-but-you.html, http://www.mindview.net/Etc/Discussions/CheckedExceptions, etc. In particular, they are uncomposable (except by ...


28

Subtyping and inheritance are two different things! Nothing doesn't extend everything, it's a subtype, it only extends Any. The specification[§3.5.2] has a special case governing the subtyping-relationship of Nothing: §3.5.2 Conformance [...] For every value type T , scala.Nothing <: T <:scala.Any For every type constructor T (with ...


25

According the Great Benchmarks Game, ATS is faster than the rest with Haskell, Scala, and one of the variants of Common Lisp in a rough tie for speed close behind that. After that Ocaml and F# are in roughly the same speed category with Racket and Clojure lagging behind... However, almost none of this means anything at all really. It's all a question of ...


24

I'll expand my comment a bit. The List[T] data structure, from scala.collection.immutable is optimized to work the way an immutable list in a more purely functional programming language works. It has very fast prepend times, and it is assumed that you will be working on the head for almost all of your access. Immutable lists get to have very fast prepend ...


24

To address the specific issue that you raise, of reified generics . . . In many contexts, type parameters are actually saved in class-files and exploitable via reflection, even despite erasure. For example, the following program prints class java.lang.String: import java.lang.reflect.Field; import java.lang.reflect.ParameterizedType; import java.util....


23

Scala is also inspired by Ocaml, which uses Option. Options are an Ocaml standard type that can be either None (undefined) or Some x where x can be any value. Options are widely used in Ocaml to represent undefined values (a little like NULL in C, but in a type and memory safe way)... I think the name chosen is a matter of taste.


23

This is a question I am interested in and I have been doing some research on. For other viewpoints, see this blog post by Noel Walsh or this question on Stack Overflow. I have some opinions I would like to offer: I think Akka, because it works with messages, encourages a "push mindset". Often, for concurrency, I would argue this is not what you want. Pull ...


22

What you are really asking about here is how to do Polymorphism in functional languages, i.e. how to create functions that behave differently based on their arguments. Note the first argument to a function is typically equivalent to the "object" in OOP, but in functional languages you usually want to separate functions from data, so the "object" is likely ...


22

The diamond problem is the inability to decide which implementation of the method to choose. Scala solves this by defining which implementation to choose as part of the language specifications(read the part about Scala in this Wikipedia article). Ofcourse, same order definition could also be used in class multiple inheritance, so why bother with traits? ...


22

I am not familiar with the Flow API. The term “lifting” comes from category theory. In programming languages such as Haskell or Scala, a lift function takes a function A => B, and somehow performs magic so that the lifted function F[A] => F[B] can be applied to a functor or monad F[A]. A concrete example using Scala's Seq container: Assume we have a ...


21

According to the Benchmarks Game for a single core, 32 bit system, Scala is at a median 80% as fast as Java. The performance is approximately the same for a Quad Core x64 computer. Even memory usage and code density are very similar in most cases. I would say based on these (rather unscientific) analyses that you are correct in asserting that Scala adds some ...


21

It's worth considering what the actor model is used for: the actor model is a concurrency model that avoids concurrent access to mutable state using asynchronous communications mechanisms to provide concurrency. This is valuable because using shared state from multiple threads gets really hard, especially when there are relationships among different ...


21

In this video I watched recently, Rich Hickey comments that he likes the destructuring part of languages like Scala, but not so much the pattern matching part, and he designed Clojure accordingly. That probably explains why the pattern matching is in a library and not as robust, although the kind of problems seen in the post you mentioned are clearly bugs. ...


Only top voted, non community-wiki answers of a minimum length are eligible