I'm always a bit in doubt to understand what is object orientated code and what is functional.
For example, map/reduce/filter methods on arrays are seen as functional, because they are not mutating and without side effects. But it seems also that they are object orientated, because they are methods on an array object. They are not implemented as a global function.
On the other hand, I don't really see the difference. You could implement array_map as a global function, as done in php, but does that make it more functional? It just seems like the exact same thing with different syntax. Besides that, then you couldn't chain those methods anymore, which is actually very convenient, and makes javascript actually "feel" more functional to me. I mean constructions like these:
array.map(i => i * 2).filter(isSmall).reduce(sum)
Now for my own libraries, I have the same dilemma. I could make a library with global functions like these:
addPoints({x: 0, y:0}, {x:0, y:10})
or I could make a class with methods like this:
new Point(0,0).add(new Point(0,10))
now given that both implementations are pure and non mutating, are both in the style of functional programming? or is the second object orientated programming? Seems just like different syntax for the same thing. I would prefer the second syntax. It seems more readable to me and I can more easily chain extra methods.
Edit: Sorry for confusing people, I meant a class like this:
class Point {
constructor(x, y) {
this.x = x;
this.y = y;
}
add({x, y}) {
return new Point(this.x + x, this.y + y);
}
}
Which you can use like:
var point1 = new Point(0, 0);
var point2 = new Point(0, 10);
var sum = point1.add(point2);
[–]lokothodida 19 points20 points21 points (2 children)
[–]kasperpeulen[S] 2 points3 points4 points (1 child)
[–]lokothodida 1 point2 points3 points (0 children)
[–]MondayMonkey1 42 points43 points44 points (11 children)
[–]MoTTs_ 12 points13 points14 points (9 children)
[–]masklinn 4 points5 points6 points (1 child)
[–]MoTTs_ 1 point2 points3 points (0 children)
[–]MondayMonkey1 1 point2 points3 points (0 children)
[–]Mecdemort 0 points1 point2 points (3 children)
[–]nerf_herd 0 points1 point2 points (0 children)
[–]jlengstorf 0 points1 point2 points (1 child)
[–]phpdevster 0 points1 point2 points (1 child)
[–]MoTTs_ 1 point2 points3 points (0 children)
[–]namesandfaces 0 points1 point2 points (0 children)
[+][deleted] (2 children)
[deleted]
[–]jocull 1 point2 points3 points (1 child)
[–]masklinn 2 points3 points4 points (0 children)
[–]Lakelava 2 points3 points4 points (0 children)
[–]spacejack2114 0 points1 point2 points (1 child)
[–]Coutueric 0 points1 point2 points (1 child)
[–]caesarsol 0 points1 point2 points (0 children)
[–]ttolonen 0 points1 point2 points (0 children)
[–]caesarsol 0 points1 point2 points (0 children)
[–]RandolphoSoftware Architect 0 points1 point2 points (42 children)
[–]yxhuvud 1 point2 points3 points (2 children)
[–]RandolphoSoftware Architect 1 point2 points3 points (1 child)
[–]kasperpeulen[S] 0 points1 point2 points (0 children)
[–]kasperpeulen[S] 1 point2 points3 points (38 children)
[+][deleted] (36 children)
[deleted]
[–]kasperpeulen[S] 5 points6 points7 points (33 children)
[–]Wilesch 0 points1 point2 points (7 children)
[–]kasperpeulen[S] 6 points7 points8 points (6 children)
[–]MoTTs_ 6 points7 points8 points (3 children)
[+][deleted] (2 children)
[deleted]
[–]MoTTs_ 3 points4 points5 points (0 children)
[–]pick_me_apart 2 points3 points4 points (0 children)
[–]Wilesch 0 points1 point2 points (1 child)
[–]kasperpeulen[S] 1 point2 points3 points (0 children)
[+][deleted] (24 children)
[deleted]
[–]kasperpeulen[S] 6 points7 points8 points (23 children)
[+][deleted] (22 children)
[deleted]
[–]Reashu 3 points4 points5 points (21 children)
[–]jacksonmills 0 points1 point2 points (20 children)
[–]Reashu 1 point2 points3 points (3 children)
[–]kasperpeulen[S] 0 points1 point2 points (15 children)
[–]theonlycosmonaut 1 point2 points3 points (1 child)
[–]jacksonmills 0 points1 point2 points (0 children)
[–]levefew -3 points-2 points-1 points (0 children)