Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I have a polyline "class" in my Clojure program, which is represented by a vector of points. (It's not really a class or anything.)

The polyline's length (in the geometric sense) is something that is often needed for computations. It's also fairly slow to calculate every time it's needed.

In OO code, I'd just have the length as a property of the Polyline class.

But in functional code, I'm lost. Do I duplicate the OO pattern and make the polyline a map of {:points points, :length (length points)}? Do I make a length function and memoize it? Do I just depend on the compiler to cache the results?

share|improve this question
1  
Just write a length function and memoize it. Calculating its length is just one of an infinitude of things you might choose to do with your polyline. If you added every polyline-related function to the polyline data type, when would it end? It's not an abstract type from what I understand so it inherently has no "operations" associated with it. –  Doval Jul 16 '14 at 13:49
1  
Beware of premature optimization. That aside, keep in mind that Clojure is not purely functional, and is quite flexible in facilitating multiple approaches to solving a problem. Therefore, without additional context, the first two of your proposed solutions sound reasonable to me. I doubt that the compiler would automatically cache your results for you, though (but I'm not sure). –  user39685 Jul 16 '14 at 14:30
1  
@Doval please read up on the slippery slope and appeal to consequences fallacies. –  user39685 Jul 16 '14 at 14:37
1  
@MattFenwick An object implies abstraction and the point of methods is that they're the only things allowed to look at implementation details. From what I understand Leonid's polylines are concrete; he hasn't hidden the fact that they're vectors of points. Therefore, it doesn't make sense to think of a polyline as an object, nor length as a method. That was my point; if you're going to consider length a method because it returns some info about a polyline, then so is removeEveryThirdSegmentAndCalculateHalfTheLength. What makes length special? –  Doval Jul 16 '14 at 15:03
    
What makes length special is its general usefulness: the frequency with which that calculation is needed and valuable. The putative rETSACHTL method, in contrast, would essentially never be used. –  Jonathan Eunice Aug 19 '14 at 21:23

1 Answer 1

The most literal and direct answer to the asked question is "you don't."

More helpful but still not quite sufficient as an answer is "as function results."

Neither is completely enough when just starting out looking at things from a functional viewpoint though so let me share some things that helped me.

  • Functions are not methods — Functions operate on whatever is passed to them, not the 'attributes' of their 'object'. They are not bound to a thing. This is easy to grasp but equally easy to forget. In OO methods and functions are treated as synonyms.

  • The OO concept 'object' isn't helpful — data is a thing of its own and functions are things of their own. They are not bound together into a conceptual unit.

  • Data doesn't have 'properties' in the OO sense — Since your data isn't an instance it doesn't carry around details within that instance. And you probably don't want to subvert this. Doval and user39685 touch on why in their comments. I do also, below.

Looked at this way you don't really have a polyline 'class'. You have polylines as a concept and you have chosen to represent this concept as a vector of points. Since it is a vector you use it like any other vector!

If it was a vector of names and you needed how many, you'd call length. If it was a vector of airplanes and you needed how many, you'd call length, etc. all the same!

If performance is a problem then you start looking at changing the algorithm, datatype, or both. After profiling.

As user39685 said in his comment "beware of premature optimization." If it turns out you spend all your time calculating the length of points then you memoize or wrap the polyline up in a map with a :length key like you have (but think twice because now you have a special snowflake to support, see below).

If you don't rush to bundle your data up into ad-hoc objects you don't have to un-bundle to use all the primitive functions already written for you. Your functions then also tend to become smaller, simpler, and more general because you don't have to wrap and unwrap or parse and bundle data before operating on it.

share|improve this answer
    
> It it was a vector of airplanes and you needed how many, you'd call length, etc. all the same! The author of the question stated that he wants to compute length of a polyline in the geometrical sense. –  M-x Mar 10 at 11:59
    
@M-x: Which doesn't at all change the point I was, perhaps poorly, trying to make. The function changes but the concept remains. –  A Sammich Mar 10 at 14:22

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.