First of all, I know this question has been asked previously , but it wasn't clear for me.  Pattern matching is used to make a function react to different types of data. One would say that if my Pattern Matching case has 4 cases and one month later I need to add a 5th one, I'll be breaking the Open-Closed Principle. I agree to that.  In a worst case scenario: Let's suppose I'm using a closed library (I can't touch code inside it) and I need to extend its functionality. The functionality I want to extend is indeed a Pattern Matching function. What should I do?  I think pattern matching is OK if I'm totally sure my Object doesn't change very often and will never require to be extended by others.  What's your opinion about using this technique? This is more like a debate than a question.  Thanks,
121182
If I add a new case class, does that mean I need to search through all of the pattern matching code and find out where the new class needs to be handled?  I've been learning the language recently, and as I read about some of the arguments for and against pattern matching, I've been confused about where it should be used.  See the following:  Pro:  and    Con:   The comments are pretty good in each case, too.  So is pattern matching something to be excited about or something I should avoid using?  Actually, I imagine the answer is "it depends on when you use it," but what are some positive use cases for it and what are some negative ones?
121896
Since I started learning  and  last year, I've read a huge number of articles which insist that design patterns (especially in Java) are workarounds for the missing features in imperative languages. One article I found :     Most people I've met have read     by the Gang of   Four (GoF). Any self respecting programmer   will tell you that the book is   language agnostic and the patterns   apply to software engineering in   general, regardless of which language   you use. This is a noble claim.   Unfortunately it is far removed from   the truth.      Functional languages are extremely   expressive. In a functional language   one does not need design patterns   because the language is likely so high   level, you end up programming in   concepts that eliminate design   patterns all together.   The main features of functional programming (FP) include functions as first-class values, currying, immutable values, etc. It doesn't seem obvious to me that OO design patterns are approximating any of those features.  Additionally, in functional languages which support OOP (such as F# and OCaml), it seems obvious to me that programmers using these languages would use the same design patterns found available to every other OOP language. In fact, right now I use F# and OCaml every day, and there are no striking differences between the patterns I use in these languages vs. the patterns I use when I write in Java.  Is there any truth to the claim that functional programming eliminates the need for OOP design patterns? If so, could you post or link to an example of a typical OOP design pattern and its functional equivalent?
56863
In   asserted:     I would advice you not to use prototypes. They have their uses, but not for most cases and definitely not in this one.   Why might this be true (or otherwise)? I almost always supply prototypes for my Perl functions, and I've never before seen anyone else say anything bad about using them.
65193
Where X is any programming language (C#, Javascript, Lisp, Perl, Ruby, Scheme, etc) which supports some flavour of closures.    Some limitations are mentioned in the  (compared to Ruby's closures), but the article is old and many limitations do not exist in modern Python any more.  Seeing a code example for a concrete limitation would be great.  Related questions:
56815
Is there a way to specify a regular expression to match every 2nd occurrence of a pattern in a string?  Examples   searching for a against string abcdabcd should find one occurrence at position 5 searching for ab  against string abcdabcd should find one occurrence at position 5 searching for dab  against string abcdabcd should find no occurrences searching for a against string aaaa should find two occurrences at positions 2 and 4
39841
How to use the  method?  Say I want to find number of combinations I can get with $x_1+x_2+x_3+x_4=22$,  where $x_i\in\mathbb{N}$.  Is this the correct time to apply the method?  This is being repurposed in an effort to cut down on duplicates, see here:     and here:
40850
Is there any benefit in using compile for regular expressions in Python?  h = re.compile('hello') h.match('hello world')   vs  re.match('hello', 'hello world')
32250
I've looked at , specifically the C++ sample, and fail to recognize the difference between just defining 3 classes, creating instances and calling them, and that example. What I saw was just placing two other classes into the process and cannot see where there would be a benefit. Now I'm sure I'm missing something obvious (wood for the trees) - could someone please explain it using a definitive real-world example?    What I can make from the answers so far, it seems to me to be just a more complex way of doing this:  have an abstract class: MoveAlong with a virtual method: DoIt() have class Car inherit from MoveAlong,       implementing DoIt() { ..start-car-and-drive..} have class HorseCart inherit from MoveAlong,       implementing DoIt() { ..hit-horse..} have class Bicycle inherit from MoveAlong,       implementing DoIt() { ..pedal..} now I can call any function taking MoveAlong as parm  passing any of the three classes and call DoIt Isn't this what Strategy intents? (just simpler?)   [Edit-update] The function I refer to above is replaced with another class in which MoveAlong would be attribute which is set according to need based on the algorithm implemented in this new class. (Similar to what is demonstrated in the accepted answer.)    [Edit-update] Conclusion  The Strategy Pattern has it's uses, but I am a strong believer in KISS, and would tend to more straightforward and less obfuscatory techniques. Mostly since I want to pass on easily maintainable code (and 'cos I'll most likely be the one who have to make the changes!).
82512
I've been looking at F# recently, and while I'm not likely to leap the fence any time soon, it definitely highlights some areas where C# (or library support) could make life easier.  In particular, I'm thinking about the pattern matching capability of F#, which allows a very rich syntax - much more expressive than the current switch/conditional C# equivalents. I won't try to give a direct example (my F# isn't up to it), but in short it allows:   match by type (with full-coverage checking for discriminated unions) [note this also infers the type for the bound variable, giving member access etc] match by predicate combinations of the above (and possibly some other scenarios I'm not aware of)   While it would be lovely for C# to eventually borrow [ahem] some of this richness, in the interim I've been looking at what can be done at runtime - for example, it is fairly easy to knock together some objects to allow:  var getRentPrice = new Switch&lt;Vehicle, int&gt;()         .Case&lt;Motorcycle&gt;(bike =&gt; 100 + bike.Cylinders * 10) // "bike" here is typed as Motorcycle         .Case&lt;Bicycle&gt;(30) // returns a constant         .Case&lt;Car&gt;(car =&gt; car.EngineType == EngineType.Diesel, car =&gt; 220 + car.Doors * 20)         .Case&lt;Car&gt;(car =&gt; car.EngineType == EngineType.Gasoline, car =&gt; 200 + car.Doors * 20)         .ElseThrow(); // or could use a Default(...) terminator   where getRentPrice is a Func&lt;Vehicle,int&gt;.  [note - maybe Switch/Case here is the wrong terms... but it shows the idea]  To me, this is a lot clearer than the equivalent using repeated if/else, or a composite ternary conditional (which gets very messy for non-trivial expressions - brackets galore). It also avoids a lot of casting, and allows for simple extension (either directly or via extension methods) to more-specific matches, for example an InRange(...) match comparable to the VB Select...Case "x To y" usage.  I'm just trying to gauge if people think there is much benefit from constructs like the above (in the absence of language support)?  Note additionally that I've been playing with 3 variants of the above:   a Func&lt;TSource,TValue&gt; version for evaluation - comparable to composite ternary conditional statements an Action&lt;TSource&gt; version - comparable to if/else if/else if/else if/else an Expression&lt;Func&lt;TSource,TValue&gt;&gt; version - as the first, but usable by arbitrary LINQ providers   Additionally, using the Expression-based version enables Expression-tree re-writing, essentially inlining all the branches into a single composite conditional Expression, rather than using repeated invocation. I haven't checked recently, but in some early Entity Framework builds I seem to recall this being necessary, as it didn't like InvocationExpression very much. It also allows more efficient usage with LINQ-to-Objects, since it avoids repeated delegate invocations - tests show a match like the above (using the Expression form) performing at the same speed [marginally quicker, in fact] compared to the equivalent C# composite conditional statement. For completeness, the Func&lt;...&gt; based-version took 4 times as long as the C# conditional statement, but is still very quick and is unlikely to be a major bottleneck in most use-cases.  I welcome any thoughts / input / critique / etc on the above (or on the possibilities of richer C# language support... here's hoping ;-p).
11190
When building console applications that take parameters, you can use the arguments passed to Main(string[] args).   In the past I've simply indexed/looped that array and done a few regular expressions to extract the values. However, when the commands get more complicated, the parsing can get pretty ugly.    So I'm interested in:   Libraries that you use Patterns that you use   Assume the commands always adhere to common standards such as .
107434
I was looking at the Proxy Pattern, and to me it seems an awful lot like the Decorator, Adapter, and Bridge patterns. Am I misunderstanding something? What's the difference? Why would I use the Proxy pattern versus the others? How have you used them in the past in real world projects?
10643
I'm used to the Java model where you can have one public class per file.  Python doesn't have this restriction, and I'm wondering what's the best practice for organizing classes.
33911
I have been reading through the  and was curious about the  declaration. I personally have never used it, however I am interested in exploring the language.   What is a good example of using friend?    Reading the FAQ a bit longer I like the idea of the &lt;&lt; &gt;&gt; operator overloading and adding as a friend of those classes. However I am not sure how this doesn't break encapsulation. When can these exceptions stay within the strictness that is OOP?
11352
Duplicate:     Maybe this is a very silly question but I don't understand why I can't declare two methods that have the same signature when they have different return types.  public class MyClass {     private double d = 0;      public double MyMethod()     {         return d;     }      public string MyMethod()     {         return d.ToString();     } }   I get a compile error that states that the class already defines a member with the same parameter types.  (Obviously the way I'm using this in my code isn't as simple as my example code...but I think it gets the idea across.)  Am I missing something concerning OO design that makes what I'm trying to do an OOP anti-pattern? Surely the compiler should be able to determine which method I'm trying to use as long as I specifically tell it which one I want.  Given MyClass myClass = new MyClass(); I would expect the following code to work:  double d = myClass.MyMethod(); string s = myClass.MyMethod();   I would expect the following code to have problems:  var v = myClass.MyMethod();   But even in the case of var it should result in a compile error.  Can anyone see what I'm doing wrong here? I'm more than happy to be corrected. :-)
19919
I ran into an interesting behavior recently.  It seems that if I override .equals() to take a parameter other than Object, it doesn't get called.  Can anyone explain to me why this is happening?  It seems to violate my understanding of polymorphism in OOP, but maybe I'm missing something.  Here's much simpler code that shows what I'm seeing:  public class MyClass {   private int x;   public MyClass(int n) { x = n; }   public boolean equals(Object o) { return false; }   public boolean equals(MyClass mc) { return x == mc.x; }   public static void main(String[] args) {     List&lt;MyClass&gt; list = new ArrayList&lt;MyClass&gt;();     list.add(new MyClass(3));     System.out.println("Contains 3? " + list.contains(new MyClass(3)));   } }   When this is run, it prints "Contains 3? false".  It looks like the equals(Object) function is called, even though there is another that would work.  By contrast, if I write equals like this the code works as expected:  public boolean equals(Object o) {   if(!(o instanceof MyClass))     return false;   MyClass mc = (MyClass)o;   return x == mc.x; }   Why isn't it figuring out which version of the function to call based on the type of the parameter?
56167
I mostly use lambda functions but sometimes use nested functions that seem to provide the same behavior. Here are some trivial examples where they functionally do the same thing if either were found within another function: Lambda function &gt;&gt;&gt; a = lambda x : 1 + x &gt;&gt;&gt; a(5) 6  Nested function &gt;&gt;&gt; def b(x): return 1 + x  &gt;&gt;&gt; b(5) 6  Are there advantages to using one over the other? (Performance? Readability? Limitations? Consistency? etc.) Does it even matter? If it doesn't then does that violate the Pythonic principle:  .
372
What is the difference between the search() and match() functions in the ?  I've read the  (), but I never seem to remember it.  I keep having to look it up and re-learn it.  I'm hoping that someone will answer it clearly with examples so that (perhaps) it will stick in my head.  Or at least I'll have a better place to return with my question and it will take less time to re-learn it.