Long running switch cases or if-else-if constructs are avoided in OOP using polymorphism wherever it is applicable.
instead of branching by matching a value, branching is done at class-level itself.
How can similar approach be applied in Functional Programming paradigm, Clojure specifically ?
3
They don’t avoid them, they embrace them using the pattern match syntax.
But functional programming is largely orthogonal to object oriented programming, so absolute majority of “functional” languages are also object oriented¹, including clojure. In fact clojure’s multi-methods are even better than plain virtual methods of Java, because they can dynamically dispatch on types of multiple arguments, not just the first one.
There is one purely functional language that does not have dynamic polymorphism, Haskell. In Haskell you can define multi-methods via type classes, but the types are resolved at compile time. To have varying types at runtime, you have to create a union type and you either have to write the function with pattern match (which is like the if chain, but with more convenient syntax) or ask the compiler to derive the method by composing the methods of the constituent types. Or use the GHC forall
extension.
¹ By object-oriented I mean that the language has some form of dynamic polymorphism with dispatch based on actual runtime type. Many new languages only have “trait-based” polymorphism where only interfaces can be inherited; I count that as object-oriented and for purpose of this answer it is sufficient.
7
This is a very old question, but I feel like the answers were lacking.
As you mentioned, in OO branching is frequently moved to the class level. Let’s think about what that means:
- Factory method handles branching, returning a derived class.
- The derived class is a collection of streamlined methods.
- So the factory method is a method that returns streamlined methods.
That is exactly how you would handle it: a higher order function. Your higher order function handles branching, returning streamlined functions for consumption.
In the context of your question, polymorphism is a bit of an abstraction for that–with added type safety.
In functional programming language, we can use functions and key params to get rid of conditional branches. That mean use functions with condition param instead of “if esle”. See example 3.
As computeSphereArea({radius:25.55})
Example 1: OOP
// in OOP(use java for example(sourceCode from: http: //developer.51cto.com/art/200907/136506.htm)):
public abstract class Shape {
// ...
public abstract void computeArea();
public abstract void computeVolume();
public abstract double getArea();
public abstract double getVolume();
}
public class Circle extends CircleShape2 {
// ...
double volume = 0.0; //
public void computeArea() { //
area = Math.PI * radius * radius;
}
public double getArea() {
return area;
}
public void computeVolume() {} //
public double getVolume() {
return volume;
}
}
public class Sphere extends Circle {
// ...
public void computeArea() { //
super.computeArea(); //
area = 4 * area;
}
public void computeVolume() { //
super.computeArea(); //
volume = 4.0 / 3 * radius * area;
}
}
public class CircleShapeApp {
public static void main(String[] args) {
Circle circle = new Circle(12.98);
Sphere sphere = new Sphere(25.55);
Shape shape = circle; //
//
shape.computeArea();
shape.computeVolume();
System.out.println("circle area: " + shape.getArea());
System.out.println("circle volume: " + shape.getVolume());
//
shape = sphere;
shape.computeArea();
shape.computeVolume();
System.out.println("Sphere area: " + shape.getArea());
System.out.println("Sphere volume: " + shape.getVolume());
}
}
Example 2: functional like oop.
//in functional programming(use javascript for example):
function initShape(v) {
var shape = {};
v = v || {};
if (typeOf(v, 'object') === true) {
shape.volumne = v.volumne || 0.0;
shape.computeArea = v.computeArea || function() {};
shape.computeVolume = v.computeVolume || function() {};
shape.getArea = v.getArea || function() {};
shape.getVolume = v.getVolume || function() {};
}
return shape;
}
function initCircle(v) {
var circle = {};
v = v || {};
if (typeOf(v, 'object') === true) {
circle.volume = 0.0;
circle.radius = v.radius || 0.0;
circle.computeArea = v.computeArea || function() {
this.area = Math.PI * this.radius * this.radius;
};
circle.computeVolume = function() {};
circle.getArea = v.getArea || function() {
return this.area
};
circle.getVolume = v.getVolume || function() {
return this.volume
};
}
return initShape(circle);
}
function initSphere(v) {
var sphere = {}
v = v || {};
if (typeOf(v, 'object') === true) {
var circle = initCircle(v);
sphere = circle;
sphere.volume = v.volume;
sphere.computeArea = function() {
circle.computeArea();
this.area = 4 * circle.area;
}
sphere.computeVolume = function() {
circle.computeArea();
this.volume = 4.0 / 3 * this.radius * circle.area;
}
}
return initShape(sphere);
}
var circle = initCircle(12.98);
circle.computeArea();
circle.computeVolume();
console.log("circle area: " + circle.getArea());
console.log("circle volume: " + circle.getVolume());
var sphere = initShpere(25.55);
sphere.computeArea();
sphere.computeVolume();
console.log("sphere area: " + sphere.getArea());
console.log("sphere volume: " + sphere.getVolume());
// Although, this is not pure functional program example, but with functional interface, like initCircle() initSphere().You can create more functions like computeCircleArea() computeSphereArea() makes it more functional.
// PS:typeOf() is here:https://github.com/will-v-king/javascript-showMe
Example3:
Ok, let’s make it more funcitonal:
/** in functional code shape became meaningless.
function initShape(v) {
var shape = {};
v = v || {};
if (typeOf(v, 'object') === true) {
shape = v.object || v.shape || shape;
shape.volumne = v.volumne || 0.0;
}
return shape;
}
function computeShapeArea(v){
}
function computeShapeVolume(v){
}
*/
function initCircle(v) {
var circle = {};
v = v || {};
if (typeOf(v, 'object') === true) {
circle = v.object || v.circle || circle;
circle.volume = 0.0;
circle.radius = v.radius || 0.0;
}
return initShape(circle);
}
function computeCircleArea(v){
var area;
v = v || {};
if(typeOf(v) === 'Object'){
var radius = v.radius || v.object.radius || v.circle.radius;
if(!typeOf(v,'undefined')){
area = Math.PI * radius * radius;
}
}
return area;
}
function computeCircleVolume(v){
return 0.0;
}
/**function initCircle and initSphere are not necessary. why? see the last line.*/
function initSphere(v) {
var sphere = {}
v = v || {};
if (typeOf(v, 'object') === true) {
var circle = initCircle(v);
sphere = circle;
sphere.volume = v.volume;
}
return initShape(sphere);
}
function computeSphereArea(v){
var area;
v = v || {};
if(typeOf(v) === 'Object'){
var radius = v.radius || v.object.radius || v.sphere.radius;
if(!typeOf(v,'undefined')){
area = 4 * computeCircleArea({radius:radius}); // **POINT** the same as :circle.computeArea(); this.area = 4 * circle.area;
}
}
return area;
}
function computeSphereVolume(v){
var volume;
v = v || {};
if(typeOf(v,'object') === ture){
radius = v.radius || typeOf(v.object, 'object') === true ? v.object.radius : typeOf(v.sphere, 'Object') === true ? v.sphere.radius : 0.0;
var circleArea = computeCircleArea({radius:radius});
if(typeOf(circleArea,'number')=== true){
volume = 4.0 / 3 * radius * computeCircleArea({radius:radius}); // **POINT** the same as: circle.computeArea(); this.volume = 4.0 / 3 * this.radius * circle.area;
}
}
return volume;
}
var circle = initCircle({radius:12.98});
console.log("circle area: " + computeCircleArea(circle) );
console.log("circle volume: " + computeCircleVolume(circle) );
var sphere = initShpere(25.55);
console.log("sphere area: " + computeSphereArea({radius:25.55}) );
console.log("sphere volume: " + computeSphereVolume({radius:25.55}) );
console.log("sphere object is unused.That means initSphere is also not necessary as initShape()");
1