1

Let say for example we have some framework in Nodejs like laravel. You have something like this:

class Model {
  getTable(){}
  edit(){let table = this.getTable();//edit...}
  insert(){}
  delete(){//some code}
}

And another child class:

class User extends Model {
  getTable() { return 'users' }
}

This is OOP version. I reuse edit, insert and delete functionalities.

How can I achieve reusing functionality with functional programming.

I prefer JavaScript for demo despide it is not fully FP language.

Thank you

  • 2
    This q is too broad. Anyway, you can reuse functions in FP because they are global, generalized, first class and often polymorphic. I'd say decoupling functionality from data increases the reusability even more, compared to classes/subtyping. –  Apr 25 '18 at 06:09
  • Yes but how can i override methods. For example in OOP is :class User{ edit(){...}}. In FP i should write all different behaviour in one function: function edit(model) { if (model is User){} else if (model is Picture)..}} ? Isnt that bad? –  Apr 25 '18 at 06:13
  • Usually you would write a function for each behavior/type and rely on a mechanism that either dispatches the right function for a given type at runtime or resolves function/type mappings statically. In Javascript you can either use the prototype system or build your own overloading by utilizing Javascript's introspection capabilities. But again, your q is too broad to get the right answer. –  Apr 25 '18 at 06:25
  • Thanks, but isnt prototpe object oriented way? I want to use functional programming like Haskell. Can you give example in Javascript without classes. You have basic Model with method "save" and in User model "save" method you save the model in the file system instead of the database. How can you override that in fp. Thanks –  Apr 25 '18 at 06:30
  • Your example is about modeling interaction with surrounding world. This is not functional by definition. Haskell uses monads to abstract that fact out, but in the end, it's imperative. You need a different example. – Frax Apr 25 '18 at 08:54
  • @Frax FP also has to deal with the real world. So this may be the wrong example for a beginner, but not the wrong example per se. Haskell uses the special `IO` type to handle IO and this type happens to implement the monad instance. Monads are only needed to sequence IO actions, so the only thing they "abstract from" in the IO context is the missing evaluation order in Haskell. –  Apr 25 '18 at 09:29
  • 2
    Possible duplicate of [Achieving polymorphism in functional programming](https://stackoverflow.com/questions/4969593/achieving-polymorphism-in-functional-programming) – Jared Smith Apr 25 '18 at 11:36
  • You seem to be asking how to do polymorphism in functional programming. Functional languages have rich support for it, but it's typically predicated on things JavaScript has poor support for: overloading functions on argument type(s), arity, etc. If you want polymorphism in JS, you can either build the relevant mechanisms yourself or use prototypes. – Jared Smith Apr 25 '18 at 11:38

2 Answers2

0

It seems like another reasoning is that you get reusability using partial application.

const createPerson = ( { create } ) => ( { name, age } ) => create ( {
  name: name.toLowerCase (),
  age: age <= 0 ? 1 : age
} )

const db = new Map()

const inc = ( () => {
   let i = 0
   
   return () => ++i

} ) ()

const createOps = db => ( {
    getById: id => db.get ( id ),
    create: data => ( db.set ( inc(), data ), data )
} )

// _createPerson is bound to a specific 
// data mapper so this function is already 
// agnostic to underlying tech if it's spread
// across the codebase
const _createPerson = createPerson ( createOps ( db ) )

const person = _createPerson ( { name: 'Matías', age: 33 } )

console.log ( person )
Matías Fidemraizer
  • 63,804
  • 18
  • 124
  • 206
-1

The example you gave is a little bit unfortunate, because all the methods in that class are side effect ones, related to IO. All those methods are precisely what you would leave to the end of the processing in order to isolate them from the side-effect-free functions.

If you are new to functional programming, I would recommend you to check this tutorial about FP in JS.

If you aren't new, I would recommend to check the Ramda and RxJS projects, so you can have a working idea of how to achieve reusability. Hint: Authentic reusability comes from the use of monads and doing the majority of the processing side-effect-free.

amedina
  • 2,838
  • 3
  • 19
  • 40
  • I know, but in reality 90% of the methods have IO(side effects). Imagine "edit" method hasnt side effect, how can i translate the code to FP –  Apr 25 '18 at 07:30
  • @user2693928 If the majority the methods you want to reuse are ones that cause side effects, like `edit`, I'm sorry, but you are out of luck. In your case, I would opt for making all the modifications (the maximum possible) in a functional way, and then, use the *repository pattern* to save the modified object to a DB. In any case, you would have exactly the same problem in Haskell too, it's the same situation. – amedina Apr 25 '18 at 07:39
  • Thank your, but isnt repository pattern OOP oriented? –  Apr 25 '18 at 07:42
  • @user2693928 It's a pattern. Many of the OOP patterns have their equivalent in the FP world. But in any case, I gave you the option I would choose. If instead of using the repository pattern you want to implement a side-effect function to save your data into a DB, go for it. The problem is that you can't scape from the side-effect part, and that mines you possibilities to reuse your code (at least, in the way you want). – amedina Apr 25 '18 at 07:49
  • @user2693928 I'm sorry I can't help you more :'( – amedina Apr 25 '18 at 07:53
  • 2
    90% of functions having IO is unusual kind of reality, even for JavaScript (unless it's a very simple JS). In FP you don't edit objects, you create altered copies of them. – Frax Apr 25 '18 at 08:49