r/functionalprogramming • u/kaizoku156 • Mar 24 '23
Question How to implement something simmilar to a factory in functional programming.
Hi all, sorry if this isn't the right forum for posting this but here I go.
I want to implement something simmilar to a factory, in typescript but wanted to see if there is any functional way of doing this instead of doing it the regular way.
So my use case is I have 3 different types of message senders (they would have different types of sending messages for each sender like a template a generic send a way to send interactive messages and so on) it might increae to 5-6 in the short term, there would be a place in my code where I know which sender Ill have to use depending a set config, if it was oop I would use a factory to create object of the specific sender and execute the type of function I need, how do we go about doing this in functional programming.
Providing some pseudo code for flow
handle_message():
//do some calculations and db calls here
// get config based on the above db calls and have a type of sender to use, let's say for now whatsapp, sms, email
// exectute a send operations here for that type
handle_message_2():
//do some calculations and db calls here
// get config based on the above db calls and have a type of sender to use, let's say for now whatsapp, sms, email
// exectute a change config operations here for that type and also schedule a future comm here
11
u/Mishkun Mar 24 '23
You should watch an awesome talk by Scott Wlaschin on this topic of transitioning from oop patterns to functional ones https://youtu.be/srQt1NAHYC0
5
u/andrewcooke Mar 24 '23
I haven't thought about your specific details, but generally you would use a higher order function - a function that returns a function.
4
u/ndgnuh Mar 24 '23
You could use a curry function, or apply the function partially.
like
In your case, g is the factory and x is the information on how to handle each sender types. x is not necessarily data, it can be function too.
IMO, factory is just buzz word for "function with some default parameters".
Oh and I recommend reading about higher order functions. I don't use them everywhere, but they are super useful in certain cases.
2
u/crlsh Mar 24 '23
https://dev.to/qgabe/functional-programming-design-patterns-part-2-factory-method-pattern-e19
2
u/chimb0w Mar 24 '23 edited Mar 24 '23
In Scala you could use typeclasses and implicits to do it, something like: