This gets back to the point that Swift is not a functional language. By default, the closure is capturing an immutable reference - the fact that the value might change is implicit to the way you must think about programming in a non-functional language. The mechanism to capture a constant value is to declare it in in a capture list:
var f = {[i] in print("hello I'm a callback and i =", i)}
> By default, the closure is capturing an immutable reference
I think some imperative languages still refer to those things as closures, which is unfortunate. Capturing a closure then having everything in there mutable kind of defeats the purpose in my opinion. But maybe I've been damaged/spoiled by functional programming and/or have spent to much time debugging issues related to concurrency and shared mutable state.
Wait, What? Immutable closures are way less useful than mutable closures. Immutable closures can pretty much only be used to create thunks. Mutable closures are basically objects.
On the other hand, if your closure depends on global mutable state, than you have a piece of GLOBAL state that is VARIABLE. Sometimes known as the root of all evil.
But maybe I've been damaged/spoiled by scheme and/or not done enough concurrency programming to get your point.
Anytime I see someone claim that immutability is "less useful" than mutability, I know that the individual has not spent a long time working in an immutable language. Because things are tremendously easier when mutation rarely enters the picture. But, it can take some experience to grasp this.
Meh, depends on the situation. Some problems are easily solved in a functional way, some problems aren't, and then there are problems that are best solved using a mixed approach. Sometimes a good old-fashioned for loop (or the equivalent) and stateful I/O is "tremendously easier" to work with than layers of tail recursion and I/O monads. Professional developers are expected to know which tool to use when, rather than religiously sticking to one approach in all circumstances.
I have yet to encounter any problem that would be better served with an imperative for loop or nested for loop than an immutable list comprehension in Clojure, for instance.
Do you have actual science (comparative studies --plural, not some single paper--, etc) behind the assertion of "better" for the functional approach or is it just blind faith?
An empirical observation says that the most important software in the world, including most of the internet infrastructure, OSes, databases, filesystems, office suites, embedded systems and such, powering 99% of the modern era is written in an imperative or OO language.
That's a fact. Whereas "there would be less issues if we had made them in a functional language" is mere conjecture, unless proven otherwise.
Note that common errors such as null pointer exceptions are not only avoided in functional languages for example, but in any imperative language with optionals, bounds checking etc too. So if one is gonna bring those up as something in favor of functional programming they're not giving the full picture.
I think swift really found the sweet spot in this regard by not thinking about mutability but value/reference semantics.
I've found that in swift the things that should be values are and the things that should be references are, rather than taking an all or nothing approach.
Except in swift, a truly value-oriented thought process is hampered by its copy-on-write semantics, which are a far cry from immutable structures found in functional languages.
Cocoa and Cocoa Touch were designed with imperative programming in mind though, so this is what the creators of Swift had to work with. I think the main use case for mutable captured values is mutable self, in particular the following, extremely common pattern:
dispatch_async(my_background_queue) {
do_some_io()
let result = do_expensive_computation()
dispatch_async(dispatch_get_main_queue()) {
self.value = result
update_ui()
}
}
Like, I'm not trying to be snarky, there's a lot of conversions going on there that aren't explicated. My takeaway is that if you can buy food and little else, that's poverty, right? Or no?
I was born and brought up in India. When I was going to school in 1990, my dad used to earn around 6,000 INR / month (120$/month, exchange rate was 50 back then). We ate out probably once every few months but even then, the salary was enough to feed family of 5 and send me and my sisters to school. I think you lack the perspective on how poor India and China is.
It was simply a correction to the following portion: "6,000 INR / month (120$/month, exchange rate was 50 back then)". Assuming the accuracy of the salary, it appears the following is more accurate: "6,000 INR / month (300$/month, exchange rate was 20 back then)".
Well, if you have a home and can cook food 400 Rs/day isn't too bad. (With 100 Rs you can get some vegetables more than a kilo of rice) Buying from a food stall is going to be somewhat more expensive than cooking food yourself. And food stall prices vary depending on quality of the food.
Also, poverty definitions vary.
EDIT: 400 Rs/day = 12000 Rs/month which isn't horrible.
I mean, Twitter only makes sense when a third party digests their API. The default web client is broken (sometimes intentionally) in many, many ways. Perhaps I'm misreading you?
Of course YMMV, but my go-to is the Mac native client that they themselves put out. Not the web client, but not third-party. I don't have trouble with it.
1. HN comment about some aspect of fizzbuzz or whatever. Not how to solve fizzbuzz or whatever; rather, some insight into how people solve problems, which is the interesting part of the question.
2. Two dozen oblivious nerds tell u how to do it in 20 characters of perl