lundi 27 avril 2015

Scala - Does pattern matching break the Open-Closed principle?

First of all, I know this question has been asked previously here, 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,

pattern for return values

In validate methods what kind of return types or return value patterns are preferred?

If a validate function returns success/failure and failed fields as a result, should the function return, array with {true/false, array of error fields]} or true on success and array of error fields in case of failure

Is it appropriate to use Factory/delegate pattern to handle exceptions?

I am working on a library that handles connection with various imap servers (mail provider) like (Gmail, Outlook, Yahoo, Orange ...).

Into the subject, One method request the access token using a refresh token (OAuth2), this method may throw an exception for many reasons, (access revoked, invalidParameter, badRequest, invalidCredentials, dailyLimitExceeded, or no connection... Google Error Responses).

So for example, if I got a revoked access exception, I should handle it, so the next time, the user connect, we ask for his consent.
If I got a too many request exception, then I handle it by backing off...

The question: Is it appropriate to use Factory/delegate (C#) pattern to handle the different type of exceptions? Or should I handle it differently?

Thank you, and sorry for my English.

dimanche 26 avril 2015

(Gnu) make: multiple targets with special compiler switches

I have a problem getting a makefile to work. I have several parsers that compile XML schemas as follows:

$(srcdir)/schema_1_parser.cpp: \
                 $(srcdir)/schema_1_parser.l \
                 $(srcdir)/schema_1_parser.tab.cpp
    $(LEX) -Pschema_1 -o$(srcdir)/schema_1_parser.cpp \
                        $(srcdir)/schema_1_parser.l

$(srcdir)/schema_1_parser.tab.cpp $(srcdir)/schema_1_parser.tab.hpp: \
                $(srcdir)/schema_1_parser.y
    $(YACC) -ldv -p schema_1 -o $(srcdir)/schema_1_parser.tab.cpp \
                                $(srcdir)/schema_1_parser.y

(There are multiple such rule pairs.)

I added the second target $(srcdir)/schema_1_parser.tab.hpp because of a makefile error "Do not know how to make schema_1_parser.tab.hpp", even though the header gets generated automatically together with the .tab.cpp file.

Now the bison parser is being called twice. I read up on this and am given to believe that this is expected behavior for special rules with multiple targets, and that I should use pattern rules instead. However, I am a bit unsure what about the command line options -Pschema_1 for flex and -p schema_1 for bison. Can I use the pattern to take care of those also? (My experience has been negative, but I may be missing something else.)

XERGM R-package: GOF error with bipartite, temporal ERGM

I was able to estimate the following temporal, bipartite ERGM using the xergm package in R:

require(xergm)
model1 <- btergm(observed_network ~ edges + b2star(2:3), R = 1000)
summary(model1)

When I went to estimate the goodness-of-fit (GOF) for the model, the following error appeared:

gof(model1)

Error in MHproposal.formula(constraints, arguments = 
control$MCMC.prop.args,  : 
The combination of class (c), model constraints (), reference measure 
(Bernoulli), and proposal weighting (default) is not implemented. Check 
your arguments for typos.

My hunch is that the previous error relates to a problem with the way I specified the model for the goodness-of-fit. I used the data.table package to construct the adjacency matrix, so it may be that xergm and data.table are not working well together.

After reading the XERGM and ERGM manuals, and some significant googling, I was unable to get much of a lead on the substantive meaning of the error.

Any help would be appreciated.

Checking if a label I click, is a part of a pattern of labels

I am making a game about remembering a pattern of labels, that show up by them changing colors. The user then has to click the labels showed, in the correct order.

After the pattern has been randomly chosen, it goes into a list called the 'pattern'. My problem here is; how do I make it so that my label_Click event handler can be used to check if the correct label has been clicked for the entire list, in the correct order. Sorry that my code is clumsy and without comments, I have just started programming.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace Spil
{
    public partial class Form1 : Form
    {
        Random rnd = new Random();
        Label[] labelArray;
        int turn = 1;
        int lives = 3;
        List<Label> pattern = new List<Label>();

        public Form1()
        {
            InitializeComponent();

            labelArray = new Label[] { label1, label2, label3, label4, label5, label6, label7, label8, label9 };
        }

        private void DisplayOrder()
        {
            for (int i = 0; i < labelArray.Length; i++)
            {
                labelArray[i].BackColor = Color.Blue;
                labelArray[i].Click += label_Click;
            }

            for (int i = -2; i < turn; i++)
            {
                int chosenNumber = rnd.Next(0, 9);
                labelArray[chosenNumber].BackColor = Color.Green;
                Thread.Sleep(1000);
                labelArray[chosenNumber].BackColor = Color.Blue;
                pattern.Add(labelArray[chosenNumber]);
            }
        }

        private void label_Click(object sender, EventArgs e)
        {
            Label clickedLabel = sender as Label;
        }

        private void Form1_Shown(object sender, EventArgs e)
        {
            System.Timers.Timer t = new System.Timers.Timer(100);

            t.Elapsed += t_Elapsed;

            t.Start();
        }

        void t_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            ((System.Timers.Timer)sender).Stop();

            DisplayOrder();
        }
    }
}

Difference between builder pattern and constructor

I am making a presentation about the builder pattern and I am pretty sure I will be asked what is the difference between the builder pattern and a constructor.

I mean the builder pattern is just a way to build an object similar to what a constructor does, so why use a builder pattern instead of plain old constructors?