19 ms·
The kinds of algorithms you can express very cleanly in a functional language, like Haskell, for example, are a lot more awkward when you have to worry about ma
by cageface 16y ago
The kinds of algorithms you can express very cleanly in a functional language, like Haskell, for example, are a lot more awkward when you have to worry about managing memory at each intermediate step. Look at all the monkeying around you have to do with __block declarations etc. For typical Obj-C code, where you're just gluing together bits of the Cocoa API, I agree that Obj-C's memory management isn't too bad. When you're trying to work at a higher level of abstraction, it's a distraction.
Writing complex algorithms in Obj-C is a pain because of it's incredibly clumsy syntax for collections and the manual coercion of elemental types back and forth to objects. Look at an implementation of a neural net or something as simple as k-means clustering in Obj-C, then C++, then Scala or Haskell to see how poor Obj-C is for this kind of thing.
For example, imagine how much worse this would be in Obj-C:
https://github.com/cageface/brains/blob/scala/src/main/scala/brains/clustering/KMeans.scala https://github.com/cageface/brains/blob/scala/src/main/scala...
Admittedly, Scala is particularly nice for this kind of thing, and Java is no better than Obj-C, but even C++ gives you much better tools for abstracting this kind of code.
- schrototo 16y agoSo? Then don't write those kinds of things in Objective-C. I wouldn't want to write UI code in Haskell either...
- cageface 16y agoSo what do you do when you want to implement a neural net or Bayesian filter, for example, in your iPhone app then? I drop into C++ for this but that's hardly ideal.
- schrototo 16y agoI don't have any experience with this, but shouldn't it be possible to compile Haskell code into a static library?
- cageface 16y agoI think it would be difficult. You'd have to get both runtime libraries to play nicely together, but the point is that Obj-C, while quite nice for UI work, isn't good at this kind of thing.
- zoul 16y agoThank you, that’s helpful. The algorithm is a bit above my head to rewrite in Objective-C on Sunday, but from my superficial understanding of it the Objective-C version wouldn’t be really ugly. Yes, it would be wordier and clumsier, but tolerably so (for me). After all, it’s precisely these things where functional languages shine. I seldom find I have to monkey around with __block. Manual boxing/unboxing sucks, yes. It can be alleviated by a simple macro where this really matters, but I can see such and argument is like pouring petrol into the fire when compared to functional languages :) Now I understand what was meant by “sophisticated algorithms”. Yes, Objective-C is not perfect for those – first class collections are a great convenience here. But I certainly do not see that as a future “liability for Apple” or a huge drawback for the language ecosystem. It’s a niche.
- Dn_Ab 16y agoIts a niche for now. But local intelligence in mobile software is going to matter more and more as ad hoc and sensor networks become more common - otherwise the explosion of data might as well not have happened.
- cageface 16y agoAnd this is just one domain in which Obj-C falls short. For another example, compare doing linear algebra for graphics in Obj-C vs C++ with a nice matrix library.