This is How FP Made Me Understand Design Patterns Better

  Рет қаралды 5,393

Zoran Horvat

Zoran Horvat

Күн бұрын

Пікірлер: 50
@nanny07
@nanny07 2 күн бұрын
This is also a perfect example to show why use a delegate instead of explicit "Func"
@echteschweissarbeit
@echteschweissarbeit 2 күн бұрын
I would even go so far as to say that the use of explicit delegate types is an absolute must. Otherwise, readability suffers so much that your colleagues will end up demonizing functional programming (FP) before they have even tried using it. @Zoran Great examples, thanks for sharing!
@zoran-horvat
@zoran-horvat 2 күн бұрын
@@nanny07 I always define delegate types for domain concepts and Func delegates foe ephemeral functions where it is likely that the caller will pass a lambda and forget about reuse. Delegate types correspond to interfaces/base classes in OOP. They are strongly typed on assignment, injectable via DI, etc.
@damianradinoiu4314
@damianradinoiu4314 2 күн бұрын
It would be nice if you went through a series of showcasing a comparison between most popular OOP pattern and the corresponding FP pattern. I can already think of a few, but this would probably encourage more people to adopt FP. Also I believe a combination of both is the best approach in most situations.
@KenB782
@KenB782 2 күн бұрын
Please do this, I still can't wrap my head what's happening in FP
@camoman1000
@camoman1000 Күн бұрын
I think one power that is underestimated by functional programming is less naming of things that are just anonymous actions anyways. But reusable things should have names that qualify the usage but one reason naming is so hard is somethings just don't need to be labeled, not everything should be a reusable reference
@anibaldk
@anibaldk Күн бұрын
8:15 You got me with the Haskell style pattern matching for lists. I did not know C# had that one. Thank you!
@zoran-horvat
@zoran-horvat Күн бұрын
@@anibaldk It is a recent addition to C#.
@anibaldk
@anibaldk Күн бұрын
@@zoran-horvat A brilliant one, I might add. Again, thanks for the content 👏
@arialdomartini
@arialdomartini Күн бұрын
Cool. Thank you. Observation: in both FP and OOP encapsulation is an important topic. One should refrain from using public visibility when not needed.
@_-Skeptic-_
@_-Skeptic-_ 19 сағат бұрын
I'm not a professional programmer, I do write some personal applications in C#, this 10 min. video made me learn many things that took me hours. I know I could do it with basic statements, but it is nice to learn his amazing use of many features that I never used.
@shmupful
@shmupful Күн бұрын
Zoran, I just love watching you code, it is always a sheer pleasure. I especially like this kind of examples, choosing the right use-case for a programming style can really showcase it's benefits This demo of using functional programming was top class.
@alexsherzhukov6747
@alexsherzhukov6747 Күн бұрын
This actually wrapped my head around some FP concepts, 10/10
@shmupful
@shmupful Күн бұрын
same!
@arialdomartini
@arialdomartini Күн бұрын
Congrats. One of the very very few videos of live coding which is not using the mouse. Really appreciated.
@zoran-horvat
@zoran-horvat Күн бұрын
@@arialdomartini No mouse policy.
@user-dc9zo7ek5j
@user-dc9zo7ek5j 14 сағат бұрын
The problem with classes is that someone thought they need to be in separate files in order to operate. The world will be a better place if you remove those 4 primitives and introduce a type, even if it is within the same file. In TS and F# they usually keep all definitions for a certain scope inside a single file, and it is encouraged to make new types that fit you and most importantly, name them as you need, not to avoid the complications of clashing type names. When you ask for specific things, then you can delegate the work onto the callers. This also kinda resembles dep. inversion in a way by demanding instead of receiving and wondering how to handle the parameters.
@AslamNazeerShaikh
@AslamNazeerShaikh Күн бұрын
Thanks for making great content in C#
@rustamhajiyev
@rustamhajiyev Күн бұрын
Mama Mia, so delicious 😋 FP - is another world, It's straightforward, simple to read and minimum boilerplate. I really enjoy writing code In FP style every day. Thank, Zoran, for another great video🔥Looking forward for more content about FP 🤩
@oscarfriberg7661
@oscarfriberg7661 Күн бұрын
For the strategy pattern, there’s a benefit of going with object oriented style: it’s easier to find all possible implementations. Func doesn’t really say that much about all the possible implementations. All I know is that it’s a function accepting a char and returning char, which can be any function in the program. The IDE won’t necessarily help much either. ICharacterTransform can be more descriptive in its restriction. With an IDE, I can easily find all possible variations of this interface. Either way, it all depends. Go for either functional or object oriented where it makes sense. It’s a common misunderstanding that functional programming and object oriented are mutually exclusive. They go great together!
@zoran-horvat
@zoran-horvat Күн бұрын
@@oscarfriberg7661 It's not a Func delegate, but a proper strongly typed delegate. You can find all assignments as easy as interface implementations. Therefore, using a delegate is equally comfortable as using a single-method interface, plus the added benefits of assigning an impromptu lambda where convenient, which is not possible with interfaces in C#, though possible in Java, for example.
@diadetediotedio6918
@diadetediotedio6918 21 сағат бұрын
OOP and FP are only compatible insofar you violate some principles of eachother. A pure FP paradigm is incompatible with many OOP architectural patterns, for example. Dependency injection is an example of a thing that only makes sense when you taint your codebase with side-effects and want to have good controlability on them, a pure functional language would rather use monads or effect systems, and they would integrate very differently in how you would architecture an OOP project "by default". But yeah, many features of OOP and FP can be imported without clashing.
@zoran-horvat
@zoran-horvat 20 сағат бұрын
@diadetediotedio6918 That is a bad definition of DI. DI exists in FP in an almost identical form, implemented via the partial function application and closures. How is that tainted with side effects?
@zoran-horvat
@zoran-horvat 20 сағат бұрын
@diadetediotedio6918 That is a bad definition of DI. DI exists in FP in an almost identical form, implemented via the partial function application and closures. How is that tainted with side effects?
@diadetediotedio6918
@diadetediotedio6918 20 сағат бұрын
​@@zoran-horvat I'm getting those notions from Mark Seemann (some articles he wrote about functional programming in the context of dependency injection, and the replacements for the own concept of dependency injection in FP) and some thoughts I have on the topic before. I think dependency injection is a consequence of side effects management being a problem. The question of side effects arises even in face of the partial application, as you are now embedding the side effects inside your functions. You can visualize the problem in the following way: define a class X with a private field "Counter" set to 0 on it, and a method named GetCounterAndIncrease() that will return the current value of Counter and increase it by 1 each time it is called. This is a simple example of a class and a method that performs side effects. Then make partial application with that class in your function, and use the result of that GetCounterAndIncrease() in the computations of the function. Your function will now be tainted with side effects, every time you call it the results would be different (which breaks the basic notion of purity).
@marcobaccaro
@marcobaccaro Күн бұрын
Welcome to the delegates, feature released in C# 1 back then in 2002.
@zoran-horvat
@zoran-horvat Күн бұрын
@@marcobaccaro Precisely. And so many developers never used them!
@sotsch9280
@sotsch9280 Күн бұрын
@@zoran-horvat but indirectly all of them ;) Microsoft inveted them, as well as Extensions, firsthand to improve their own libraries. Back then functional programming was "imperformant" and "unreadable".
@krccmsitp2884
@krccmsitp2884 56 минут бұрын
Yeah, they're sadly very underrated nowadays.
@AndreMantas
@AndreMantas 22 сағат бұрын
This is assuming the strategy does not need any dependency. It is pretty common for strategies to need dependencies (logging, configuration, external data access to make the decision, etc). Would be nice to see the full picture with examples on how to setup the DI in this case. Still very good intro video as always which keeps pushing me more and more to FP.
@zoran-horvat
@zoran-horvat 21 сағат бұрын
@@AndreMantas Any function can capture dependencies in its closure. There is nothing you can do with an object that you cannot repeat with a delegate or lambda.
@Tesfamichael.G
@Tesfamichael.G Күн бұрын
Thank you Zoran!
@nickbarton3191
@nickbarton3191 2 күн бұрын
Nice work Zoran. I particularly like the creation of a list of succinct rules, say password verification, in a chain of functions. I'd be interested to see how a chain of responsibility is implemented with it. I've been injecting func lambdas with delegate for years. Turns out that I'm a functional style programmer and I didn't know it ! It's not always appropriate to use func, especially when the is many of them. An Interface is the right thing in this case, otherwise the number of parameters gets out-of-hand.
@aboimpinto
@aboimpinto Күн бұрын
Hello, I've been looking for something like this because I use a log of strategy pattern. I used because II will have a piece of code that will process something and the caller don't know who it is. Could even be a add-in that is loaded dynamic. That invalidate the array that you create with all the strategies (that for you are functions). In my Workflow, only one strategy process one ObjectType and let's define each object with the correspond ObjectType. After all my DLLs are loaded (inclusive the add-ins) I will have the full list of strategies available in instance, therefore, How can I inject these methods when I have the list of objects and I want to process them by the correct strategy? Thanks
@EmperorFool
@EmperorFool 8 сағат бұрын
Listening to your soothing voice, I only just now realized I need someone to narrate my life. Are you available?
@деменция-н4п
@деменция-н4п Күн бұрын
is it good idea to learn haskell?
@zoran-horvat
@zoran-horvat Күн бұрын
@@деменция-н4п Always.
@Olaff_
@Olaff_ 20 сағат бұрын
Specifications can be injected in the composition root either manually or through reflection and using DI, you'll have full blown centralized specifications to be used across your whole system. Am I correct in assuming this?
@zoran-horvat
@zoran-horvat 19 сағат бұрын
@@Olaff_ That is correct.
@kantagara
@kantagara 2 күн бұрын
Sjajno kao i uvek!
@DavidSmith-ef4eh
@DavidSmith-ef4eh 2 күн бұрын
yes, but how do you prevent code duplication. If you used an interface for a strategy. I could click on the interface and find all implementations. Then I could just see if someone did that thing that I wanted. Is it possible with delegates to find all the method that adhere to the delegate definition?
@zoran-horvat
@zoran-horvat 2 күн бұрын
@@DavidSmith-ef4eh What is an implementation of an interface, that is an assignment to a delegate type. It works the same.
@DavidSmith-ef4eh
@DavidSmith-ef4eh 2 күн бұрын
@@zoran-horvat yes, then it can work I guess in languages that support delegate type definitions. not in php though, we will have to use the old ways
@stevenstone307
@stevenstone307 2 күн бұрын
This is cool but at the end of the day, shouldn't you just be using a functional language at this point?
@zoran-horvat
@zoran-horvat 2 күн бұрын
@@stevenstone307 Why? C# is quite functional already. You can combine classes with delegates seamlessly.
@adambickford8720
@adambickford8720 Күн бұрын
These solutions are native to the SDK, not some 3rd party bytecode hack. This IS C#.
@GIRARDINF
@GIRARDINF Күн бұрын
I made a causality engine by using dynamic delegate read from text file. The purpose is : stop coding project specification : no more IF ($Condition) {$Actions} So the text in the file are code like we wrote Conditions but considered as delegate ! The DLL read it, add it, compile it, get the elementary parameters, put elementary parameters under survey by interception mechanism. So when one elementary parameters change, i get it. I launch delegate evaluation, and if the result is True, i invoke the actions. This is a part of my Flexibility FrameWork, to create a coding world, by coding less and less again. No more classification, no more data access, no more cultural gap ... just saying 3 things : - what is your data (or where, to reverse and get what) ! - choose the way to store (SQL MERISE, XML MERISE , SQL Metadata, ...) ! - choose the language to code and the one to show !
How Far is Your Object-Oriented Code From Functional Programming?
13:10
"BEST C++ CODE ever written" // Code Review
27:38
The Cherno
Рет қаралды 89 М.
Леон киллер и Оля Полякова 😹
00:42
Канал Смеха
Рет қаралды 4,7 МЛН
My scorpion was taken away from me 😢
00:55
TyphoonFast 5
Рет қаралды 2,7 МЛН
What is the Strategy Pattern? (Software Design Patterns)
13:18
Be A Better Dev
Рет қаралды 61 М.
Why Use Design Patterns When Python Has Functions?
23:23
ArjanCodes
Рет қаралды 114 М.
Make Domain Rules Explicit In Any Business Application
9:23
Zoran Horvat
Рет қаралды 14 М.
Is Functional Programming DEAD Already?
21:07
Continuous Delivery
Рет қаралды 78 М.
Trump Started Negotiations / Is the War Ending?
12:18
NEXTA Live
Рет қаралды 416 М.
The Only Database Abstraction You Need | Prime Reacts
21:42
ThePrimeTime
Рет қаралды 234 М.
The Complete Backend Developer Roadmap
8:24
Programming with Mosh
Рет қаралды 340 М.
7 Design Patterns EVERY Developer Should Know
23:09
ForrestKnight
Рет қаралды 219 М.