Monads are not that exotic. People present it as "deep math" but for a mathematician for example it's kids stuff at the level you need to understand them for Haskell et al.
It's like saying differential equations are some "deep voodoo math" (only monads are even simpler).
Yes, they're much simpler than differential equations. But the difference is, differential equations are the simplest way to solve some difficult problems -- monads are often the gratuitously complex way to solve simple problems.
Consider the Maybe monad - it encodes optional values in a simple way. The approach before this is to have something called "null" that would wreck your programs. Maybe monad is one of the best improvements to day-to-day programming tasks that I've personally experienced in my lifetime.
But thinking monadically and abstracting monadically is extremely different from what programmers normally learn, for a start because important monads like state and exceptions are built-in features of most programming languages. Seeing that these things have a common pattern, and seeing that it may be worthwhile to abstract this common pattern takes a lot of time.
The mismatch between the utter simplicity of the concept of monads, and the complicated explanations one comes across doesn't help.
Once I finally saw some of the things you can do with a monad rather than those useless examples based around Maybe, I had an 'aha!' moment as everything went click.
No sodding idea what a monoid is, though.
For example, we can combine two integers by adding (+) with 0 as an identity. Or we could combine them by (*) with 1 as an identity.
Lists are a monoid because we can append two lists and the empty list is the identity.
The combining operator has to be associative and the identity has to be an identity for it. (Combining something with the identity gives you that original thing back.)
And that's all. There's just not much structure to them. In fact, there's so little structure that mathematicians don't care much about them. It's an exotic-sounding name for a fairly pedestrian concept.
But they're useful in programming. The main reason is that they're so ubiquitous: so many different things form monoids, often in different ways. This means we have a single interface applicable to almost every domain you can think of which is useful even if the interface doesn't tell us much.
They're also a good fit for parallelism. Because the combining function is associative, we can a bunch of combinations in any order we like, making it easy to spread them out over multiple threads. It very naturally captures the reduce in map reduce.
But mostly it's a convenient abstraction that's minimal and simple enough to pop up everywhere while still being useful.
They are HUGELY useful. One of the most useful is a "union" monoid definition for Maps. In Scala, it's written like this:
implicit def mapUnionMonoid[K, V: Semigroup]: Monoid[Map[K,V]]
So any map with Semigroup values is a monoid (Semigroups are monoids without the zero value). |+| under this definition will combine the Maps, but in hte case of collisions, |+| the colliding values together. This Monoid is the ABSOLUTE KING of aggregation. You can foldMap over lists and produce singleton Maps of the shape you want, and let the monoid instance aggregate for you.
monads is that they're way too low level t
Monads are neither low- nor high-level. Monads are orthogonal to this. A monad (in the context of programming languages) is a generalised from of function composition. Instead of composing f : A --> B with g : B --> C yielding a function g;f : A --> C, monads compose functions f : A --> FB with g : B --> FC, yielding g;;f : A --> FC. Here F is come transformation on types. Monads connect the types FB and B in a canonical way. That's all.E:
I shouldn't even call it an understanding of monads. I've read at least 15+ explanations of what a monad is and still don't grok it.
Firstly, saying "`X` is a monad" is the same as saying "class `X` implements the monad interface".
The monad interface is usually defined with `return` and `bind`, but I think it's more instructive to borrow the terminology from JavaScript's `Promise`...
* `Promise#resolve(value)` is the same as `return`, also known as `pure`. It just wraps the given value in a promise.
* `Promise#then(function)` combines both `bind` (when a new Promise is constructed and returned) and the functor method `map` (when a plain value is returned).
To provide a unified monad/functor interface for both `Promise` and `Array` (using `wrap` instead of `resolve):
// `then` provides both `bind` and `map` depending on the return type of the given function:
Promise.prototype.map = Promise.prototype.then
Promise.wrap = Promise.resolve
// f maps elements to arrays of elements:
Array.prototype.then = function (f) {
return [].concat.apply ([], this.map(f))
}
Array.wrap = function (x) {
return [x]
}
There's no intrinsic value beyond that shared interface, it just allows you to write abstractions without knowing specifically what kind of monad you're dealing with (like the "do" syntax).EDIT: BTW, I've purposely skipped a few things in this description, it's meant to be illustrative, not definitive...
(I'm not a Haskell expert, I dabble, done CIS194, half of RWH, and I have no idea what you said -- which is a common problem I run into in the Haskell world, lots of super helpful people that have forgotten what its like to not speak their language)
The operation of a monoid maps from pairs of things to things. So in terms of types:
<a,a> -> a
Or for some `twin` type constructor: twin a -> a
This is a bit more suggestive also in terms of F-algebras, where the operation has the following signature (f is a functor, or mappable container): f a -> aJames is one of the few people who don't make Monads into a mythical being.
http://james-iry.blogspot.de/2007/09/monads-are-elephants-pa...
I was making a jest to say that most scala programmers don't know anything about FP and just use it for the type inference and stick to OO practices, vars, etc.
Depends on how we define FP. Until 2010, when Haskell started to emerge as a HN fad, people were OK to define FP as what LISP programmers do, and didn't demand FP programmers know what a modad is, type theory or even use "immutability" everywhere...
If you read discussions from 2000-2005 for example, very few people define FP as "what Haskell does".
They just don't understand / have much interest in the intermediate/advanced parts. I've seen lots of beginners take to for comprehensions, maps, filters etc pretty quickly.
The problem with Scala is that at an advanced level the code can become pretty unreadable.
Scala has about 2% mind share on the JVM after ten years in existence, I would say that not only is it not winning but its time has passed.
Ceylon is one year old, Kotlin is not even out yet, there is plenty of time for either of these two to gain some solid mind share.
Or maybe not. Maybe Java is going to reign supreme for quite a while. Whatever the replacement of Java will eventually be, I'm pretty sure it won't be Scala.
I think so. By the time I heard of Kotlin, I mentally lumped it in the basket of "just another JVM language". I mean, there are so many now, and you already had Groovy, Clojure, Scala, JRuby, and Jython pretty well established, along with a couple of dozen other niche players. Then Kotlin comes along... I don't know about most people, but I never heard anything about Kotlin that was so compelling that I felt the need to go mess with it. I mean, why what instead of Nice, or Fantom, or Gosu, or Beanshell, or Mira, etc., etc...
Same thing for Ceylon. It looks like just another "also ran" to me. If the people behind these languages want people to use them, they are going to have to work hard to get the word out about whatever advantages (purported or real) they have.
As a result, it is also the non-Java JVM language with the best IDE support, best Android support and best build-tool support.
Truth be told, I don't really find that very compelling. I mean, do programming languages always need "big company" support to be successful? I don't know. So far Scala, Clojure and Groovy have done pretty well, with varying degrees of commercial support.
As a result, it is also the non-Java JVM language with the best IDE support, best Android support and best build-tool support.
I guess, but here's the thing... the languages I use now (mainly Groovy and Java) have at least good enough IDE support, Android support (ok, I don't really care about that) and build-tool support. So even if Kotlin is incrementally better, or hell, even dramatically better, that stuff doesn't do a lot to sway me to Kotlin.
That said, like most geeks, I like dabbling with new languages, and I am sure I'll try out Kotlin (and Ceylon) at some point. And maybe then one or the other will "wow" me. But for now, it isn't a high priority. And I expect a lot of other people are approaching it with a similar mindset.
History shows that the answer to that is a resounding yes. Aside from a couple of scripting languages (BASIC and Python), there have been exactly zero successful mainstream languages without a large company behind them. Scala, Clojure and Groovy are doing fine, but their adoption is one or two orders of magnitude less than the mainstream languages (Java, C, C++, C#).
I'm not necessarily the biggest fan of the TIOBE index, but in this case I'll cite it, as it is "close enough" I think. Ruby is #13 right now, which isn't bad. And while I think TIOBE has some warts, I think it's safe to say that anything in the top 20 is fairly successful, and anything in the top 50 is "successful" to a certain degree.
http://www.tiobe.com/index.php/content/paperinfo/tpci/index....
You do know VMware/Pivotal pulled their support for Groovy in March this year and no-one else stepped in, don't you?
I do, but I also know that Groovy is becoming an ASF project[1]. So we'll see how it goes with volunteer support, and perhaps a few paid people here and there from companies that use Groovy.
Groovy has lingering problems in migrating to builds.apache.org, see [1]. Some of the Groovy despots from Codehaus times (I wouldn't know which ones specifically) are keeping some computing machinery physically separated from the Apache infrastructure to run their own build processes, going against Apache guidelines. Some of them also have control over the groovy-lang.org domain (again, I don't know who). I suspect Groovy won't ever become an ASF project, but instead just sit it out in the incubation system until the former Codehaus Groovy despots get a better deal where they don't have to share their control democratically with the ASF. They managed to keep Groovy in the Java Community Process for 9 years before being booted out, all the while using the JSR-241 to promote themselves, so they'd think nothing of leeching on Apache's incubator for just as long before stirring up conflict later on to get booted out when it suits them.
BTW, that link [1] to Nabble's Groovy mailing list archive was recently redirected to an embedded view within the groovy-lang.org website where they can collect IP addresses, and all that implies -- another indictment of the Groovy despots longstanding intention to control and instead of sharing.
[1] http://groovy.329449.n5.nabble.com/Jenkins-Groovy-Apache-etc...
So? I'm not making a claim that a language needs adoption on that scale to be considered "successful". To me, Ruby, PHP, Python, Groovy, Scala, Clojure, etc. are all very much "successful". The languages I think of as being less so, would be things like Nice, Fantom, Frege, Forth, Modula-3, Rebol, etc.
There just doesn't seem to be a good point in using Kotlin, as you can tell from how fast the Kotlin proponent in this thread is changing the frame of reference.
- Higher-kinded types? Better compare with Haskell!
- Compilation speed? Let's pick Java, the language with the least useful typesystem!
- Popularity? Let's compare Scala with Java, but compare Kotlin to Scala!
- Commercial backing? Let's conveniently ignore that the language lives by the cross-subsidization of JetBrain's IDE business, instead of relying on the success of their language.
Point is, there is nothing that Kotlin does substantially better that would give it a niche between Java and Scala.
- Non-incremental compilation speed is not that much faster compared to Scala, and substantially slower than Java.
- Most of the things regularly used in Scala cannot be expressed in Kotlin, it only makes Java idioms slightly nicer to write down.
- Like in Scala, IDE support is ongoing work.
- Compared to both Java and Scala, the ecosystem isn't there.
Compared to Scala, Kotlin makes you pay 80% of the cost for 20% of the benefits.
I predict that future Java releases will keep cannibalizing Kotlin from the bottom, while Kotlin will fail to attract developers from more expressive languages.