答:
Specify updated fields list in Query String.

PUT /resource/:id?fields=name,address,dob
Body
{
//resource body
}

Implement merge stored data with model from request body:

```<code>private ResourceModel MergeResourceModel(ResourceModel original, ResourceModel updated, List<string> fields)
{
    var comparer = new FieldComparer();

    foreach (
            var item in
            typeof (ResourceModel).GetProperties()
                    .Where(p => p.CustomAttributes.All(a => a.AttributeType != typeof (JsonIgnoreAttribute))))
    {
        if (fields.Contains(item.Name, comparer))
        {
            var property = typeof (ResourceModel).GetProperty(item.Name);
            property.SetValue(original, property.GetValue(updated));
        }
    }

    return original;
}
</code>```

Body:
Specify updated fields list in Query String.

PUT /resource/:id?fields=name,address,dob
Body
{
//resource body
}

Implement merge stored data with model from request body:

private ResourceModel MergeResourceModel(ResourceModel original, ResourceModel updated, List<string> fields)
{
    var comparer = new FieldComparer();

    foreach (
            var item in
            typeof (ResourceModel).GetProperties()
                    .Where(p => p.CustomAttributes.All(a => a.AttributeType != typeof (JsonIgnoreAttribute))))
    {
        if (fields.Contains(item.Name, comparer))
        {
            var property = typeof (ResourceModel).GetProperty(item.Name);
            property.SetValue(original, property.GetValue(updated));
        }
    }

    return original;
}


问:
static globals and anonymous namespaces in C++

Why did C++ make any distinction between static globals (internal linkage) and symbols in an unnamed namespace (external linkage, but no way to refer to it from outside anyway), when introducing the latter?

Are any of those reasons still valid, or are there new ones?

Are there any places left where they are still different but the arbitrary rule that anonymous global (or namespace-scope) unions must be static, and what are they?

For bonus-points, if there are no good reasons left for them to be different, is there a request to make them equivalent?

When C++ introduced namespaces (C++98), and specifically unnamed namespaces, static globals were deprecated as obsolete and inferior to the new thing in a bout of enthusiasm, though that was reverted with C++11:

Deprecation of the static keyword… no more?

Before C++11, symbols with internal linkage could not be used as template-arguments: Why did C++03 require template parameters to have external linkage?

答:
I don't suppose this answers all of your questions (or any of them?), but the key difference between file-level static declarations and anonymous namespaces is that the namespaces also apply to types (you can't declare a static type in the same sense you declare a variable), that's why the namespace is preferred, so there's a single idiom to declare file-scoped data and types.

Exemplifying, the following code should compile just fine. (Not really useful, since you can't distinguish between both types, but allowed)

```<code>#include <iostream>

struct Foobar
{
    int   foo;
    float bar;
};

namespace
{

struct Foobar
{
    double baz;
};

} // namespace

int main()
{
    std::cout << "HELLO!\n";
}
</code>```

A live test here.

ContentLicense: CC BY-SA 4.0

Body:
I don't suppose this answers all of your questions (or any of them?), but the key difference between file-level static declarations and anonymous namespaces is that the namespaces also apply to types (you can't declare a static type in the same sense you declare a variable), that's why the namespace is preferred, so there's a single idiom to declare file-scoped data and types.

Exemplifying, the following code should compile just fine. (Not really useful, since you can't distinguish between both types, but allowed)

#include <iostream>

struct Foobar
{
    int   foo;
    float bar;
};

namespace
{

struct Foobar
{
    double baz;
};

} // namespace

int main()
{
    std::cout << "HELLO!\n";
}

A live test here.

ContentLicense: CC BY-SA 3.0


问:
Why should I use "functional operations" instead of a for loop?
```<code>for (Canvas canvas : list) {
}
</code>```

NetBeans suggests me to use "functional operations":

```<code>list.stream().forEach((canvas) -> {
});
</code>```

But why is this preferred? If anything, it is harder to read and understand. You are calling stream(), then forEach() using a lambda expression with parameter canvas. I don't see how is that any nicer than the for loop in the first snippet.

Obviously I am speaking out of aesthetics only. Perhaps there is a technical advantage here that I am missing. What is it? Why should I use the second method instead?

答:
If anything, it is harder to read and understand.

That is highly subjective. I find the second version much easier to read and understand. It matches how other languages (e.g. Ruby, Smalltalk, Clojure, Io, Ioke, Seph) do it, it requires fewer concepts to understand (it's just a normal method call like any other, whereas the first example is specialized syntax).

If anything, it's a matter of familiarity.


问:
read file bytewise and find information
i am a hobby php-dev and have the following problem:

i have a rar-file, whose header is damaged, therefor extracting is not fully possible. the contents are non-compressed, and can be read with eg. a hex-editor. the archive contains jpeg-files, but because of the damaged header, some of them are not extractable in winrar. i have tried repairing, but a good portion of the file remains broken.

i want to read the file in, and look for the bytes that indicate start and end of a jpeg (from what i know they are FFD8FF for start and FFD9FF for end). using a hex-editor i have managed to find some of those bytes, and also extracting the image into a file and view it does work. since the file is 500mb, i want to do this automatically, and since i am php-friendly, i would like to do it there :-)

i know how to read a file bytewise (fread), what i am having difficulties is, parsing the file correctly in hex, so i can identify the starts and ends. what i am thinking about is something like this (in pseudocode):

```<code>while( READ FILE UNTIL EOF ){
    if( CURRENTBYTES == FFD8FF ){
        $jpeg_file = READ FILE UNTIL CURRENTBYTES == FFD9FF
        fwrite($jpeg_file, "xyz.jpg");
        // return to while, looking for next FFD8FF
    }
}
</code>```

could someone give me a hint, how the reading and identifying would be done best? the main question is, how can i read a file until a start-byte, then save the following, then look for the next start-byte? efficiency, security, code beauty are no concern, i just want all the pics :-)

many thanx for any helps

答:
I would step through the file, instead of reading it all at once. Look for the first byte (0xFF), and when found, see if the next byte is 0xD8. If so, open output file and write those two bytes, followed by each byte until you reach the end pattern. Look for it the same way as the start bytes and close the file output when encountered.

Memory usage will be much less. If should run much faster than using a regex.

This is basically a very simple state machine.

Title: read file bytewise and find information


问:
Implementing a Forest Data Structure in Java
Name of data structure that's tree-like with multiple root nodes

I stumbled upon someone's answer to a question regarding implementing a tree that has > 2 nodes above. I just wanted to get someone's thoughts on using a Forest Data Structure to implement a Family Genealogy Tree that consist of multiple nodes with 0-multiple children. I may use a Forest Tree, however from most representations that I searched up on and found, it looks similar to a disjointed set, but I do not want a parent node to already be pre-destined to have no children.
I hope it makes sense what I am saying.
Any advice or comments you could offer would be much appreciated.

答:
I hope I have a clear enough understanding of what your problem is. If I'm off-track let me know, otherwise the following should be of some help to you.

Just to be clear: I will consider all of the below under the context of genealogy.

A tree is simply a branching structure. In genealogy, you could consider the latest child as the root and create a tree that describes the ancestry of this person. Yes, the latest child is the root, not the oldest known ancestor. This is quite counter-intuitive and probably not really what you want anyways.

Now, once you have more complicated family issues, like a man having children with two different women, you'll run into problems with trees. If you have a separate tree for each child, then the father needs to be duplicated to be able to occur in each of these trees. Unpractical to say the least.

As Neil mentioned in his comment above, it is time to switch to a graph model. Most important for this is to clear up a typical confusion: The general populace speaks of something called a "Family genealogy tree", but as a computer scientist you must realize that such a tree is no tree as per the definitions given in computer science (or biology for that matter). We have a very clear and concise mathematical definition of a tree data structure, and a "genealogy tree" does not match it.

Why is that? Simply because in computer science trees, the different branches are not able to grow back together. With families, this is possible. Quite drastically, you could have the case of a father with two childrens, which again spawn an off-spring. Moral implications aside, this results in a diamond-like view, which is not representable in a tree.

Enter the world of graphs: A graph is just a generic term of things that are connected to each other. Hence, every computer science tree is also a graph. A very special and restricted one of course. In a graph, there is no root anymore, but there are nodes with similar properties (called sources). There are also no leaves anymore, but again nodes with similar properties (called sinks).

In terms of genealogy you can limit the result graph by making it a DAG, a directed acyclic graph. It is directed, because between two nodes, you can declare one as the parent side, one as the child side. For example, you could always point from parent to child (that's one direction, the other is feasible too). If you want to be able to traverse this structure in both directions, you may even consider an undirected variant.

Acyclicity simply means, that there are no circles in your data structure. It is pretty clear that this applies to genealogy (unless you want to count in time traveling and the implied option of becoming your own father or mother). In a DAG, you can have any number of nodes and connections between them. You can however add arbitrary limitations. For example, if all connections go from parent to child, then each child node could be restricted to have a maximum fan-in of 2. That's just graph theory lingo for saying that a person cannot have more than two parents.

Hence, I suggest to take a close look at directed acyclic graphs and the underlying graph theory in general. Also note that it'll pay off. If you want to do software development you should know about graphs. They are one of the most general data structures and apply to almost everything. They are not always the best choice of course, but they will be one of the most powerful things in your arsenal.


问:
Why is mergesort O(log n)?
Mergesort is a divide and conquer algorithm and is O(log n) because the input is repeatedly halved. But shouldn't it be O(n) because even though the input is halved each loop, each input item needs to be iterated on to do the swapping in each halved array? This is essentially asymptotically O(n) in my mind. If possible please provide examples and explain how to count the operations correctly! I haven't coded anything up yet but I've been looking at algorithms online. I've also attached a gif of what wikipedia is using to visually show how mergesort works.

答:
The complexity of merge sort is O(nlog(n)) and NOT O(log(n)).

Merge sort is a divide and conquer algorithm. Think of it in terms of 3 steps:

The divide step computes the midpoint of each of the sub-arrays. Each of this step just takes O(1) time.

The conquer step recursively sorts two subarrays of n/2 (for even n) elements each.

The merge step merges n elements which takes O(n) time.

Now, for steps 1 and 3 i.e. between O(1) and O(n), O(n) is higher. Let's consider steps 1 and 3 take O(n) time in total. Say it is cn for some constant c.

How many times are we subdividing the original array?

We subdivide the input until each sub-array has one item so there are exactly log(n) "subdivision stages". We undo each subdivision stage with a "merge stage".

For example, if n = 8, there is a total of 3 merge stages: one where each pair of n/8 sub-arrays are merged to form a single n/4 sub-array, one where pairs of n/4s are merged to form n/2s, and one where the pair of n/2 are merged to form n.

What is the time cost for merging all pairs at each merge stage?

For this, look at the tree below - for each level from top to bottom:

Level 2 calls merge method on 2 sub-arrays of length n/2 each. The complexity here is 2 * (cn/2) = cn.

Level 3 calls merge method on 4 sub-arrays of length n/4 each. The complexity here is 4 * (cn/4) = cn.

and so on ...

At each merge stage, the total cost for merging all pairs is O(cn). Since there are log(n) merge stages, the total complexity is: (cost per stage)*(number of stages) = (cn)*(log(n)) or O(nlog(n)).

Image credits: Khan Academy

Title: Why is mergesort O(log n)?


问:
When is it ok to assert for a pointer being non-null?
This came up as part of a code review for a code segment which resembled this:

```<code>auto somePikachu = GetMeAPikachu();
NT_ASSERT(somePikachu != nullptr); // this only fires on debug build
somePikachu->ThunderBolt();
</code>```

In my opinion that assert is redundant since if somePikachu is null it will crash anyway in the next line when it tries to call Thunderbolt. (Of course if  ThunderBolt doesn't require dereferencing this pointer nothing will happen on release build). So the assert is not buying anything IMHO.
There is another school of thought that was of the opinion that the assert adds value to readability and makes the intent of the design clear.
I would like to get the opinion of the community on the correct pattern of asserting the design for validity of pointers.
Please feel free to include other possible scenarios that match the problem statement.

答:
The value of the assertion is:

It explicitly documents the assumption of the programmer that GetMeAPikachu returns non-null. This is especially valuable if the function at some point in history changed behavior, or if there are specific circumstances in which the function can return null, but the programmer is asserting that these circumstances don't apply here.

It turns the error situation from "there was a crash, now go and find the reason" into "this pointer was null".

It is independent of the implementation details of ThunderBolt. If that function isn't virtual and doesn't access member variables itself, the crash could simply not happen at all, or be deep within the call tree, and it might be not an access violation, but possibly an illegal instruction because you eventually managed to jump through an invalid function pointer/vtable.


问:
What's RAII? Examples?
Always when the term RAII is used, people are actually talking about deconstruction instead of initialisation. I think I have a basic understanding what it might mean but I'm not quite sure. Also: is C++ the only RAII language? What about Java or C#/.NET?</p>

答:
RAII is partly about deciding when an object becomes responsible for its own cleanup - the rule being that the object becomes responsible if and when its constructor initialisation completes. The symmetry of initialisation and cleanup, constructor and destructor, means the two have close ties to each other.

One point of RAII is to ensure exception safety - that the application remains self-consistent when exceptions are thrown. At first sight this is trivial - when an exception causes a scope to exit, the local variables in that scope need to be destructed.

But what happens if the exception throw occurs within a constructor?

Well, the object hasn't been fully constructed, so cannot be safely destructed. The constructor should have try blocks as needed to ensure that any necessary cleanups are done before the exception is propogated. Once the exception propogates outside the scope where the object was constructed, there will be no destructor call, because the object wasn't constructed in the first place.

Consider in particular the constructors for member data inside the object being destructed. If one of those throws an exception, your main constructor code won't run at all - but some code that forms an implicit part of that constructor will have. Any members that have been successfully constructed will be automatically destructed. Any members that were not constructed (including the one that threw the exception) aren't.

So basically, RAII is a policy that ensures that anything that gets fully constructed will get destructed in a timely way, particularly in the presence of exception throws, and that any object either gets fully constructed or it isn't (there are no half-constructed objects that you can't know how to safely clean up). Resources that are allocated are also freed. And a lot of the work is automated, so the programmer doesn't have to worry too much about it.

Title: What's RAII? Examples?

ContentLicense: CC BY-SA 2.5


答:
When is it ok to assert for a pointer being non-null?

I would tell you that it is never ok to use assert.

If the condition fails, then the program terminates. It gives you some debug information, but the effect is the same - not something you want the users of your application to see.

If you define NDEBUG, then the check goes away. But in a case when somehow that condition is met, then your application will most likely end with some kind of undefined behaviour.

So, what to do if the condition is not met? Throw an exception.

And in the exception handler, you can try to fix the problem. If it is not possible, then ask for user's assistance.

Body:
This came up as part of a code review for a code segment which resembled this:

```<code>auto somePikachu = GetMeAPikachu();
NT_ASSERT(somePikachu != nullptr); // this only fires on debug build
somePikachu->ThunderBolt();
</code>```

In my opinion that assert is redundant since if somePikachu is null it will crash anyway in the next line when it tries to call Thunderbolt. (Of course if  ThunderBolt doesn't require dereferencing this pointer nothing will happen on release build). So the assert is not buying anything IMHO.
There is another school of thought that was of the opinion that the assert adds value to readability and makes the intent of the design clear.
I would like to get the opinion of the community on the correct pattern of asserting the design for validity of pointers.
Please feel free to include other possible scenarios that match the problem statement.


问:
Using code under Apache 2.0 License in my program using MIT License
I'm using source code from a code example that is licensed under the Apache 2.0 License in my program. Now I would like to publish that program and source code on GitHub using the MIT License.
I've read the following in the license:

You must cause any modified files to carry prominent notices stating that You changed the files;

But I didn't change any of the files I just copied the relevant parts of the code into my source files.

Now here are my questions:

I know I have to add a copy of the Apache 2.0 License into my project but do I have to put anything into my source files to correctly publish my program/source code?

As it is a command line program do I have to add a -license switch and display the license or do I have to add it the "USAGE" output?

Do I have to put the MIT License into every of my source files or is it enough to have a copy of it in the project?

Thanks in advance!

答:
As parts of the sources are licensed under one license (Apache 2.0) and parts are under a different license (MIT), it is important that each file carries a notice stating which license applies to the contents of that file.

It is strongly recommended to have only a single license for each source file, so you should have the code that you copied from the third-party library in a separate source file (or multiple separate source files). These files should have a copyright notice referring to the Apache license and state that you copied the code from the third-party library.

For your own code that you want to publish under the MIT license, it is recommended to put the MIT license in its entirety in a comment at the top of your files.

The MIT license isn't that long, and it will avoid all confusion about what license the code is under even if the file gets separated from the rest of the project.

If it is unfeasible to separate the code under the different licenses into separate files, then you could proceed like this (assuming a small portion of a file is under the Apache license):

Put your source file under the MIT license

Directly below the copyright & license statements, add a comment that parts of the file are under a different license.

Immediately above the code that is under the Apache license, add a comment block giving the copyright and license details for the code following it.

To avoid thoroughly confusing others, this should be done at the granularity of a function.


问:
Why isn't there a true "Date-Only" data type?
I'm so ridiculously frustrated with having to use DateTime values for data sets that are truly "just a day." Birthdays being the most-common example, but this comes up in business applications all the time.

I've gotten used to just setting the Time part of "date-only" records to "noon" (which avoids the date ever changing no matter the timezone). This seems like a hack, and I'm forever finding bugs from junior devs that trip over this issue.

Time is always relative to a fixed point. 4PM is 4 hours post meridian, or noon. The sun's highest point in transit is observable, and allows us to set up a coordinate system. 3 hours before noon (Ante Meridian), 2 hours after noon, 1441899402938 milliseconds since January 1, 1970. To people raised in a Cartesian world, this is second nature.

But our Calendar predates Descartes. My argument is that it is more properly thought of as a enumeration over which a modulo function is applied. Monday follows Sunday, and so on until you get to the fact that Sunday follows Saturday. There is no positive and negative, it is a modulus, or absolute value.

Similarly with years repeating. Every 365 days (or so) there are several special days for me: birthdays, anniversaries, childrens' birthdays, etc. etc. Business scheduling applications are rife with examples of meetings every seven days, first Tuesday of the month, etc. Just because we CAN map this onto a floating point number, and in truth mapping it onto said number solves a LOT of problems that are really difficult the old way, but that doesn't mean it's the only way to do it.

Awareness and understanding of the "square peg in a round hole" nature of using DateTimes to store Dates makes you a better programmer in my opinion.

Is there value in an application explicitly intended as a scheduling application in defining a Date class, or is "set all times to noon" the best approach? What issues might there be with using DateTime and setting the Time component to Noon?  Can timezone shifting be accounted for in such an approach? I've used MomentJS, but I think that's just a better Date class.

答:
I think Mike Nakis in his answer above does a better job than I can of explaining how time in general is an absolute measured coordinate and any other communication, supposed state or persistence of that time coordinate is merely an abstracted representation of said time coordinate.

You speak to such representations when referring to Day of the Week as merely some modulus kind of representation of an actual point in time.  In reality it is somewhat more complicated than that.  If you were tasked to write a function that will return the Day of the Week for a given point in time, consider the following information that you will need as an input to such an algorithm.  You will require the point in time, the Calendar, the time-zone to consider (Keep in mind, timezones change ALL THE TIME so you need to know when that effective timezone began to when it ended in specific time coordinates. North Korea recently just changed theirs for example!), and if Daylight Savings time is in effect, this changes over time as well.  Now consider if you were given a DateTime in local timezone, you also have to convert this into a time coordinate for your algorithm.

You can see how complicated this seemingly simple question can truly be.

I know the pain you are feeling as I was in your shoes at one point fixing all the bugs in a visitation scheduling application for a product that was written by inexperienced developers.  The whole thing had to be scrapped.

Time is indeed a coordinate, but besides a mere Date, consider other time sensitive data that might be needed like:

Duration: A span of milliseconds that might occur that signifies a length or passage of time with no specific time coordinates specified.  A use case might be,

As a user, I would like this task to be performed 15 seconds after the completion of the midnight job every other Wednesday.

Interval: A range of time between two specific time coordinates.  A use case where you may want to consider an interval.

As a user, I need to see every Day of the Month wholly contained within a specified interval of time.

Another quick point I wanted to make is that you made a comment about Floating Point numbers for time based data and I advise against it.  Floating point arithmetic inevitably leads to rounding errors that may not give you as precise a measurement as necessary for time.

So in conclusion, all of this information inevitably leads to the following design considerations:

Specific points or ranges in time should be persisted in UTC or some datatype that contains enough information to be easily abstracted back to UTC where needed

Application logic should be written to format UTC or a range of UTC coordinates into a representational data state that is more digestible for the end user.

Important durations should be persisted in milliseconds

Locale specific or end user preferences on the display of any data regarding time should be persisted as additional data and treated like a display option. (Eg. Kiosk A, Central Time Zone, Military Time format, or User B preferences, Standard Time in Tel Aviv (GMT+7:00) timezone, etc...)

Avoid FP numbers


答:
In short, because most computer based time types are focused on handling the time and time zone issue properly.

There are 2 edge cases that are not served well by the usual approach.  Setting a point in time that is on the other side of a daylight saving change using local time, which then gets converted in the UTC by a lower abstraction layer, and then makes you 1 hour early / late for your meeting.

The other one is (as per the question) modelling arbitrary date information, such as recording a person's date of birth.  Imagine the case where two people are born at the same time, one in New Zealand, the other in Hawaii.  The probability is that they will have different dates of birth on their passports, and if the person born in Hawaii moves to New Zealand, they will be considered a day older than the New Zealand born person, despite having lived for the exact same time.

The suggestion in the question as setting the date to have a time of midday, UTC will work, ALMOST everywhere. UTC offsets range from -12 to +14, so there are a few places in the pacific where this approach will fail.  I tend to treat these data types as strings, in a yyyymmdd format, and if I have to do comparisons calculations between two dates, this can be safely done as a string comparison.  When doing delta comparisons (e.g. between date and now, or how long till they reach age X), you need to ensure that all dates are created in the same UTC offset, and can then use the standard time functions to do the work.

Title: Why isn't there a true "Date-Only" data type?

Body:
I'm so ridiculously frustrated with having to use DateTime values for data sets that are truly "just a day." Birthdays being the most-common example, but this comes up in business applications all the time.

I've gotten used to just setting the Time part of "date-only" records to "noon" (which avoids the date ever changing no matter the timezone). This seems like a hack, and I'm forever finding bugs from junior devs that trip over this issue.

Time is always relative to a fixed point. 4PM is 4 hours post meridian, or noon. The sun's highest point in transit is observable, and allows us to set up a coordinate system. 3 hours before noon (Ante Meridian), 2 hours after noon, 1441899402938 milliseconds since January 1, 1970. To people raised in a Cartesian world, this is second nature.

But our Calendar predates Descartes. My argument is that it is more properly thought of as a enumeration over which a modulo function is applied. Monday follows Sunday, and so on until you get to the fact that Sunday follows Saturday. There is no positive and negative, it is a modulus, or absolute value.

Similarly with years repeating. Every 365 days (or so) there are several special days for me: birthdays, anniversaries, childrens' birthdays, etc. etc. Business scheduling applications are rife with examples of meetings every seven days, first Tuesday of the month, etc. Just because we CAN map this onto a floating point number, and in truth mapping it onto said number solves a LOT of problems that are really difficult the old way, but that doesn't mean it's the only way to do it.

Awareness and understanding of the "square peg in a round hole" nature of using DateTimes to store Dates makes you a better programmer in my opinion.

Is there value in an application explicitly intended as a scheduling application in defining a Date class, or is "set all times to noon" the best approach? What issues might there be with using DateTime and setting the Time component to Noon?  Can timezone shifting be accounted for in such an approach? I've used MomentJS, but I think that's just a better Date class.


问:
writing a controller file in Python
I need some advice on my idea to write a controller file in Python, and not like the C in MVC type controller but a more simpler idea. Just a Python script that controls the operation of some other Python scripts.

I will outline the idea, and any advice or ideas would be greatly appreciated.

I have to write a system that does some background tasks for a web application. The web app is in PHP and separate from the Python system I am writing, the Python will do some automated tasks and calculate and save some analytical data for the web app to use and display.

I have experience with basic Python but I have not pushed forward to learn much about class/objective Python. So far what I have written for the system is a bunch of separate py scripts that do the jobs they have to do, written totally procedurally. They all work fine, but it's not really a system yet. My initial krux was that I had to keep them all alive and running infinitely in the background. After some research I decided that the best way to do this was to have a process manager like Supervisord look after each script, I would start up the scripts and allow them to run for a certain time with a finite loop (in each script) and then they would exit, then the supervisor would restart them every time they exit and keep them alive. I wanted to avoid using infinite loops because of the possibility of memory leaks (I believe allowing the process to exit totally frees up memory associated with that process and prevents leaks). I have not tested any of this, it's just a concept at the moment.

Something I would prefer to do would be to have one script be the controller of the rest of the scripts and have the supervisor only look after the controller. This saves me having to update the supervisors config file every time I add to the Python system.

The basic idea for the controller would be to have a loop fork new threads for each separate script/process and then have the controller wait for all the other threads to complete before allowing the loop to do another iteration (starting everything again).

I'm guessing what I need to do for this would be to wrap all the code in each script in a function, then import them as modules into the controller and call each function on a new thread inside the 'main' loop of the controller. Then the loop just runs, calls each function on a new thread and waits for each thread to finish before starting again.

How does this sound as a concept?

The only direct coding question I have is about turning my scripts into modules, how do I go from totally procedural code (no functions, no classes) into an importable module? can I wrap the procedures in just a function, or do I need to have them in a class also?

答:
I have a similar system but have a different concept:

I use cron to auto-start processes

Check if related process is running

If it is not running, then start the process.

The module uses psutil package to get a list of running processes, search for the related process and returns whether is it in the list or not. That may sound practical or not according to your use case though:

```<code>import psutil

class ProcessControl(object):
    """
    This program checks whether the given python file is running or not. If check_params flag is set as True, then
    it will be checked if an instance of the python file is running with all of the given paramters. If check_params
    flag sets as False, then the check will be made with only using the python file name and instances of the file
    running with different parameters will also be count.
    """
    def __init__(self, filename, *args):
        self.filename = filename
        self.args = args
        self.arg_num = len(args)

    def process_count(self, check_params):
        process_count = 0
        # Examine the process list to check if given process is running or not...
        for _prs in psutil.process_iter():
            try:
                _cmdline = _prs.cmdline()
            except TypeError:
                _cmdline = _prs.cmdline
            if len(_cmdline) >= 2 and "python" in _cmdline[0] and self.filename in _cmdline[1] and _prs.is_running():
                # We found an instance of the process that is running. Since This control function is triggered when
                # we run the python code, There would be at least one process (this one) which is running. Counting 2
                # or more processes means there is another instance which is still running when we trigger the python
                # code file
                if check_params:
                    # We also will check the parameters for the complete similarity
                    if len(_cmdline) == 2 + self.arg_num and all(str(_arg) in _cmdline[2:] for _arg in self.args):
                        process_count += 1
                    else:
                        # This is no match...
                        pass
                else:
                    process_count += 1
            else:
                # This is no match....
                pass
        return process_count

    def is_running(self, check_params=True):
        return self.process_count(check_params) > 1
</code>```

I have little python files which have similar code as below:

Ex:

myFile.py

```<code>class MyCodeClass:
    def run_code(self):
         ...

if __name__ == "__main__":
    my_code = MyCodeClass()
    try:
        if ProcessControl(__file__).is_running():
            print "Already Running"
        else:
            my_code.run_code()
    except Exception as e:
        print e
</code>```

And finally I have lines that trigger this file in my cron:

```<code>* * * * * python myFile.py
</code>```

Logic:

cron triggers this file every x minutes. ProcessControl checks whether file is running or not. When calling is_running method, you can pass a bool value so the controller will ignore the parameters passed while running the python file. Like if check_params is True following two commands will be accepted as different and both will be triggered:

```<code>python myOtherFile.py 127.0.0.1 220
python myOtherFile.py 127.0.0.1 250
</code>```

and disabling check_params will evaluate the second call as the same program and will not trigger it

Title: writing a controller file in Python

Body:
I have a similar system but have a different concept:

I use cron to auto-start processes

Check if related process is running

If it is not running, then start the process.

The module uses psutil package to get a list of running processes, search for the related process and returns whether is it in the list or not. That may sound practical or not according to your use case though:

import psutil

class ProcessControl(object):
    """
    This program checks whether the given python file is running or not. If check_params flag is set as True, then
    it will be checked if an instance of the python file is running with all of the given paramters. If check_params
    flag sets as False, then the check will be made with only using the python file name and instances of the file
    running with different parameters will also be count.
    """
    def __init__(self, filename, *args):
        self.filename = filename
        self.args = args
        self.arg_num = len(args)

    def process_count(self, check_params):
        process_count = 0
        # Examine the process list to check if given process is running or not...
        for _prs in psutil.process_iter():
            try:
                _cmdline = _prs.cmdline()
            except TypeError:
                _cmdline = _prs.cmdline
            if len(_cmdline) >= 2 and "python" in _cmdline[0] and self.filename in _cmdline[1] and _prs.is_running():
                # We found an instance of the process that is running. Since This control function is triggered when
                # we run the python code, There would be at least one process (this one) which is running. Counting 2
                # or more processes means there is another instance which is still running when we trigger the python
                # code file
                if check_params:
                    # We also will check the parameters for the complete similarity
                    if len(_cmdline) == 2 + self.arg_num and all(str(_arg) in _cmdline[2:] for _arg in self.args):
                        process_count += 1
                    else:
                        # This is no match...
                        pass
                else:
                    process_count += 1
            else:
                # This is no match....
                pass
        return process_count

    def is_running(self, check_params=True):
        return self.process_count(check_params) > 1

I have little python files which have similar code as below:

Ex:

myFile.py

class MyCodeClass:
    def run_code(self):
         ...

if __name__ == "__main__":
    my_code = MyCodeClass()
    try:
        if ProcessControl(__file__).is_running():
            print "Already Running"
        else:
            my_code.run_code()
    except Exception as e:
        print e

And finally I have lines that trigger this file in my cron:

* * * * * python myFile.py

Logic:

cron triggers this file every x minutes. ProcessControl checks whether file is running or not. When calling is_running method, you can pass a bool value so the controller will ignore the parameters passed while running the python file. Like if check_params is True following two commands will be accepted as different and both will be triggered:

python myOtherFile.py 127.0.0.1 220
python myOtherFile.py 127.0.0.1 250

and disabling check_params will evaluate the second call as the same program and will not trigger it


答:
In answer to:

Well, I'd honestly like to know why almost every language only has one data type for this.

The most common reason might be "because it's not necessary".  If you want a date time that doesn't care about hours, minutes, seconds, etc, then you'd just initialize it like this:

```<code>date = new DateTime(year, month, day, 0, 0, 0);
</code>```

If you want you can extend DateTime yourself:

```<code>public class Date extends DateTime {
    ...
    public Date(int year, int month, int day) {
        this(year, month, day, 0, 0, 0);
    }
}
</code>```

Note: I'm intentionally ignoring the default time and timezone.  It doesn't really matter what you set them to, so long as they're the same for all Dates.  You can make a case for UTC.  You can make a case to use the timezone your server sits in - either way I don't think it's important for representing an imprecise value like Date.  Same with the default time - you can make it 0, you can make it noon.  It doesn't matter.  If Facebook sends me a birthday notification at 00:01 but I was born at 23:59 I don't really care, and I won't be offended that they were more than 12 hours off.

The above is in Java but would work similarly in any language with a DateTime and inheritance. Java actually has numerous ways of solving this, and the above is deprecated(they want you to use Calendar now).  But as others posted in comments, some languages actually do provide a Date class, presumably for just this reason.

In all likelihood, every Date implementation is probably just a wrapper for the language's DateTime and it zeroes out the time.  Otherwise you would need duplicated code to solve problems like the number of days between two Dates/DateTimes, or whether two Dates are equal(What about Feb 29 and Mar 1?).  Those kinds of things are usually solved in the DateTime class.  It makes sense to reuse the same code for a Date.

Body:
In answer to:

Well, I'd honestly like to know why almost every language only has one data type for this.

The most common reason might be "because it's not necessary".  If you want a date time that doesn't care about hours, minutes, seconds, etc, then you'd just initialize it like this:

date = new DateTime(year, month, day, 0, 0, 0);

If you want you can extend DateTime yourself:

public class Date extends DateTime {
    ...
    public Date(int year, int month, int day) {
        this(year, month, day, 0, 0, 0);
    }
}

Note: I'm intentionally ignoring the default time and timezone.  It doesn't really matter what you set them to, so long as they're the same for all Dates.  You can make a case for UTC.  You can make a case to use the timezone your server sits in - either way I don't think it's important for representing an imprecise value like Date.  Same with the default time - you can make it 0, you can make it noon.  It doesn't matter.  If Facebook sends me a birthday notification at 00:01 but I was born at 23:59 I don't really care, and I won't be offended that they were more than 12 hours off.

The above is in Java but would work similarly in any language with a DateTime and inheritance. Java actually has numerous ways of solving this, and the above is deprecated(they want you to use Calendar now).  But as others posted in comments, some languages actually do provide a Date class, presumably for just this reason.

In all likelihood, every Date implementation is probably just a wrapper for the language's DateTime and it zeroes out the time.  Otherwise you would need duplicated code to solve problems like the number of days between two Dates/DateTimes, or whether two Dates are equal(What about Feb 29 and Mar 1?).  Those kinds of things are usually solved in the DateTime class.  It makes sense to reuse the same code for a Date.


答:
Merge Sort is a recursive algorithm and time complexity can be expressed as following recurrence relation.

T(n) = 2T(n/2) + ɵ(n)

The above recurrence can be solved either using Recurrence Tree method or Master method. It falls in case II of Master Method and solution of the recurrence is ɵ(n log n).

Time complexity of Merge Sort is ɵ(nLogn) in all 3 cases (worst, average and best) as merge sort always divides the array in two halves and take linear time to merge two halves.

It divides input array in two halves, calls itself for the two halves and then merges the two sorted halves. The merg() function is used for merging two halves. The merge(arr, l, m, r) is key process that assumes that arr[l..m] and arr[m+1..r] are sorted and merges the two sorted sub-arrays into one. See following C implementation for details.

```<code>MergeSort(arr[], l,  r)
If r > l
     1. Find the middle point to divide the array into two halves:
             middle m = (l+r)/2
     2. Call mergeSort for first half:
             Call mergeSort(arr, l, m)
     3. Call mergeSort for second half:
             Call mergeSort(arr, m+1, r)
     4. Merge the two halves sorted in step 2 and 3:
             Call merge(arr, l, m, r)
</code>```

If we take a closer look at the diagram, we can see that the array is recursively divided in two halves till the size becomes 1. Once the size becomes 1, the merge processes comes into action and starts merging arrays back till the complete array is merged.

Title: Why is mergesort O(log n)?

Body: Mergesort is a divide and conquer algorithm and is O(log n) because the input is repeatedly halved. But shouldn't it be O(n) because even though the input is halved each loop, each input item needs to be iterated on to do the swapping in each halved array? This is essentially asymptotically O(n) in my mind. If possible please provide examples and explain how to count the operations correctly! I haven't coded anything up yet but I've been looking at algorithms online. I've also attached a gif of what wikipedia is using to visually show how mergesort works.

Body:
Merge Sort is a recursive algorithm and time complexity can be expressed as following recurrence relation.

T(n) = 2T(n/2) + ɵ(n)

The above recurrence can be solved either using Recurrence Tree method or Master method. It falls in case II of Master Method and solution of the recurrence is ɵ(n log n).

Time complexity of Merge Sort is ɵ(nLogn) in all 3 cases (worst, average and best) as merge sort always divides the array in two halves and take linear time to merge two halves.

It divides input array in two halves, calls itself for the two halves and then merges the two sorted halves. The merg() function is used for merging two halves. The merge(arr, l, m, r) is key process that assumes that arr[l..m] and arr[m+1..r] are sorted and merges the two sorted sub-arrays into one. See following C implementation for details.

MergeSort(arr[], l,  r)
If r > l
     1. Find the middle point to divide the array into two halves:
             middle m = (l+r)/2
     2. Call mergeSort for first half:
             Call mergeSort(arr, l, m)
     3. Call mergeSort for second half:
             Call mergeSort(arr, m+1, r)
     4. Merge the two halves sorted in step 2 and 3:
             Call merge(arr, l, m, r)

If we take a closer look at the diagram, we can see that the array is recursively divided in two halves till the size becomes 1. Once the size becomes 1, the merge processes comes into action and starts merging arrays back till the complete array is merged.


答:
Dates and times are many different things depending on context, and you need many separate types to cover all use cases.

The DateTime type present in many languages represent a precise point in time ("instantaneous time"). Beyond this we have a number of relative or "human" time and timespan concepts like calendar days, recurring dates, months, years etc. which are in many cases ambiguous and context dependent. These types are not as universally useful, but necessary in specific application domains like calendars, scheduling tools and other applications which interact with human concepts of time.

If you are writing something like a calendar app you would definitely benefit from using a library like Joda-time which provides a richer set of time types. For example LocalDate, a date without time. This has different semantics than an ordinary DateTime with the time part set to zero, since the DateTime still indicates a specific point in time (midnight in a specific timezone), while LocalDate indicates the whole day, and is not tied to a specific timezone. This also means you cannot directly translate one to the other.

LocalDate is certainly simpler than DateTime because it doesn't have to take timezones into consideration, but you should be aware of the other issues, e.g. that the current date may actually go backwards when you are crossing a timezone, and that the same instant in time may correspond to different dates in different timezones. If you are using local dates in networked or web apps you should be very careful about these issues. Removing the time part from a date does not solve the fundamental problem of timezones! And if you take historical dates and different cultures into consideration it gets even trickier, since the same date may correspond to wildly different instances in time in say the Julian versus the Gregorian calendar.

Now you ask why languages does not have something like LocalDate built-in. Well first off, some languages like SQL and Visual Basic do have a date type without time part. And Java have also added a LocalDate in the recent version. But other platforms like .Net does not. Only the language designers can really answer why this is not included in the standard library, but my guess would be that "instantaneous time" is conceptually simple and universally useful, while the other time concepts are only useful for specific application domains (like calendars etc.). So it makes sense to let the application developer write custom types to handle the more complex use cases, or let it be handled by a third-party library (like Joda-time).


问:
Who is the Owner of Information? Memento vs. Originator
Imagine for a second that I'm implementing the Memento Pattern, using the following classes.

Classes

An Originator class that has public and private, properties and fields respectively

A Memento class that has many properties/fields representing state of Originator

A CareTaker class that acts on an instance of Originator, and saves a list of Mementos representing the Originator's state.

Using the example classes above, is there an objective reason to use one of the following solutions over the other? The premise here is that I need to expose a few properties that partially represent the state of the Originator.

Solution #1

Properties and fields representing state are duplicated across Originator & Memento

Originator has a CreateMemento method that initializes a new Memento with all of the shared properties/fields between the two

Originator has a SetMemento method that sets Originator's properties/fields to those that correspond with Memento's

Solution #2

Originator has an instance of Memento, representing Originator's current state

Originator exposes as properties, the necessary properties/fields of it's Memento, using the getters/setters to update the memento

Originator doesn't have it's own copy of properties/fields representing its state, instead it uses it's Memento as a backing store.

I understand that ideally there should be yet another class OriginatorState, that Originator & Memento both have instances of, but I'm not asking about which classes to create. I'm asking which class holds the information of the current state of the Originator. Both the Originator and the Memento? Just the Memento? or the Originator vicariously through the Memento as in Solution #2?

答:
Once the Originator generates a Memento, the Caretaker shouldn't have an API available to modify the data contained within that object.  The key is that encapsulation isn't lost when giving the Memento to the Caretaker.

Objectively, if the only really difference between Option 1 and Option 2 is that the Originator uses an instance of the Memento internally to maintain its state, it really doesn't matter.  It is not required to, but it is not prohibited.


问:
Android App with Ruby Backend Server
I'm working on a personal project to help me branch out and learn some new/different technologies. I'm a .NET programmer but I want to learn Ruby and how to develop Android apps. I have developed pieces already but need the whole system to tie together now.

End Goal:

I want to develop an Android app that will display game schedules, scores, and standings for a local sports league. The server should periodically poll (scrape) the web site for new data/changes and push the data to the corresponding user's app. The polling will happen on a per user basis (as the web scrapper must log in to the website on behalf of each user in order to scrape their relevant data).

What I have so far:

I have a web scrapper (already developed in ruby) that logs in acquires a user's data from the facility's web site.

I have ruby script which allows me to push data down to a test Android app using Google Cloud Messaging

Where I'm stuck:

So now I need the app to register it's GCM registration ID with my server...except I don't have a server part figured out yet. I could currently do this with the knowledge I have acquired so far, but I fear this will only work on my local machine (where I'm currently developing/testing). This is because both Rack and Sinatra launch a WEBrick web server, I want to be able to use Rack (or maybe Sinatra, but trying to learn Ruby without any frameworks) with Apache as that's what my web hosts uses. Can I instruct Rack to use Apache? Are WEBrick and Apache even the same thing?

I keep reading about all kinds of Ruby specific web servers (Puma, Passenger, Thin, Unicorn, etc.) that I've never heard of before...I'm not sure if these are akin to Apache or something entirely different.

答:
I found my answer on StackOverflow. Basically no, WEBRick is not the same thing as Apache, one is a "application server" and the other is a web server (respectively), and both are needed. Essentially the Web Server is a pass through for HTTP messages to the App Server (this is known as reverse proxy web server).

More detailed information can be found on StackOverflow (and I highly recommend reading it): Ruby On Rails Server Options

Title: Android App with Ruby Backend Server


问:
Mixing OOP and Non-OOP
I'm working on a command line interface text based game. I'm writing it in C but there are various ways in which I could refactor the code by using Objective-C:

using NSDictionary to allow me to store data in Plists (easier to read/write because of the Foundation framework but much larger in size) instead of plain text

having classes with methods (not a big thing but increases code readability)

eliminating the need of converting between an object's char* name and its int/enum equivalent (using and enum to represent things reduces memory usage and complexity when using plain non-OO C, otherwise I would have to use strncmp everywhere)

What I want to know is whether or not there are disadvantages to mixing OO- and non-OOP. It's still command line, which means most of it will be written in pure non-OO C.

Are there any reasons for which I shouldn't mix these?

答:
Object Orientation is not primarily a language feature. It is a programming paradigm/method/style. That is, you can write perfectly object oriented code in pure C, and you can just as well write perfectly non-object oriented code in Java.

Object Orientation is not a virtue in itself. It is helpful most of the time, and thus rightly encouraged heavily, but there are use cases where a non-object oriented solution works just as well or better.

There is no problem with mixing object oriented and non-object oriented code in the same project. Object oriented code can call non-object oriented code and vice-versa.

The only problem that may come up is mixing code written in different languages. But, as Doc Brown rightly notes in the comments, Objective-C is a strict superset of C, which means that you can just compile any C code as Objective-C. This is very different from trying to compile C code as C++ code, which will force changes in many places. But that is not your problem, you can easily switch from a .c suffix to a .m suffix wherever you need it without breaking the code in the file.

That said, your main focus when refactoring your game should be to simplify your code. If you know object orientation well, simplifying your code will likely mean making it heavily object oriented. If you don't know how to use object orientation well yet, try to find at least two different object oriented solutions for each problem, and try to choose the better one in each case. But don't try to keep object oriented and non-object oriented code separate.

Concerning your enum/char* problem: I would suggest that you read up on string interning and try to implement it in your game. Because, once you have interned strings, you can simply compare your strings by comparing their pointer. That should allow you to reduce the enum usage in a flexible and performant way.

Title: Mixing OOP and Non-OOP


问:
Licensing for artistic work used inside GPLv3 licensed software
I'm a volunteer for a non-profit organization and am helping them develop a free open source Android application that will be licensed under GPLv3.

We are going to use the logo and some other graphics files from the organization and are wanting to use licensing of this artistic work to protect ourselves from anyone doing damage to the reputation of the organization.

What license would you suggest us using for our artistic work? Thinking outside of the box, is there another approach that would be better for us? Any thoughts more in general about what we can do to protect the organization?

答:
You can make it clear in the license included with the application that the artistic assets are not covered by the GPL.

Additionally, I suggest removing the artistic assets from the source code you distribute and include placeholder assets instead. That way a build from the public GPL source will not include your assets.


问:
Why don't "multi-infinite" list comprehensions work with lazy evaluation?
As a simple demonstration of the efficiency of Haskell style, I thoughtlessly ran the following:

take 100 [(a, b, c) | a <- [1..], b <- [1..], c <- [1..], a^2 + b^2 == c^2]

This should be a way of deriving the first 100 Pythagorean triples, with duplicates. In practice however, it never halts, because the algorithm itself defies lazy evaluation.

To think about it in terms of actual implementation, the following should be something similar to how the list comprehension is actually evaluated, in an imperative style:

```<code>results = []
for (a = 0; a < ∞; a++) {
  for (b = 0; b < ∞; b++) {
    for (c = 0; c < ∞; c++) {
      if (a^2 + b^2 == c^2) {
        results[] = [a, b, c]
      }
    }
  }
}
</code>```

When written like this, it becomes obvious that the function can never yield results, because infinite time will be spent testing whether 1^2 + 1^1 == c^2, as only the innermost for loop will advance, and a and b will remain '1'.

The common solution in this particular case is to constrain the values of the smallest two variables to that of the largest:

take 100 [(a, b, c) | c <- [1..],a <- [1..c], b <- [1..c],a^2 + b^2 == c^2]

However, this seems like an obvious oversight for implementors of the language. When you think about it, any list comprehension with more than one infinite source of search space will never halt, for the same reason, except some will yield useful results when (1, 1, x) is useful. There are questions discussing this problem, but most discuss specific cases, rather than the problem overall. Why isn't fixing this within the language with a different iteration pattern trivial?

答:
Well the simplest, practical answer, which does take experience to appreciate, is that this situation is at most a minor annoyance.  A more elaborate answer, which takes still more experience to appreciate, comprises first two observations and a conclusion.

First observation: the Haskell community sees list comprehensions as a concrete application of monads.  The features offered by list comprehension coincide with the those provided by MonadPlus instance for lists, and GHC has a MonadComprehensions extension that allows you to use the comprehension syntax with any monad.

Second observation: there is a classic technique for interleaving infinite lists, due to famous mathematician Georg Cantor's proof that the set of natural numbers has the same cardinality ("size") as the set of rational numbers.  This can be easily translated into Haskell:

{-# LANGUAGE DeriveFunctor, TupleSections #-}

import Control.Applicative

cantor :: [a] -> [b] -> [(a, b)]
cantor [] _ = []
cantor _ [] = []
cantor (a:as) (b:bs) =
    (a,b) : interleave (map (a,) bs) (interleave (map (,b) as) (cantor as bs))

interleave :: [a] -> [a] -> [a]
interleave [] ys = ys
interleave (x:xs) ys = x : interleave ys xs
```
<p>And we might try to define an alternative list <code>Monad</code> instance based on that.  First we define a wrapper type <code>Cantor</code>:</p>
<pre class="lang-hs prettyprint-override"><code>newtype Cantor a = Cantor [a]
    deriving (Eq, Functor)
</code>```

Monad is a subclass of Applicative, so first we define an Applicative instance for Cantor:

instance Applicative Cantor where
    pure a = Cantor [a]
    Cantor fs <*> Cantor as = Cantor (map (uncurry ($)) (cantor fs as))
```
<p>...but here we've already gone wrong, because it turns out that this violates the laws ("contract", in OO terms) of the <code>Applicative</code> class.  In <code>Applicative</code>, the following unit test must succeed no matter what values are passed in as arguments:</p>
<pre class="lang-hs prettyprint-override"><code>-- | One of the consequences of the associative law for 'Applicative'.
-- This function must return true no matter what arguments are passed
-- in.  Haskell's QuickCheck unit testing library runs tests in this
-- format, and this one will fail if we run it on 'Cantor'.
associativity
    :: (Applicative f, Eq (f (a, (b, c)))) => f a -> f b -> f c -> Bool
associativity as bs cs =
    as `pair` (bs `pair` cs) == fmap assoc ((as `pair` bs) `pair` cs)

pair :: Applicative f => f a -> f b -> f (a, b)
pair = liftA2 (,)

assoc :: ((a, b), c) -> (a, (b, c))
assoc ((a, b), c) = (a, (b, c))
</code>```

That this test fails can be verified very easily by just running a few simple examples.  Note that pair in the Cantor type is the same as the cantor function as above, so we can simplify this test to this:

associativity' :: (Eq a, Eq b, Eq c) => [a] -> [b] -> [c] -> Bool
associativity' as bs cs =
    as `cantor` (bs `cantor` cs) == map assoc ((as `cantor` bs) `cantor` cs)
```

So Cantor is not a law-abiding Applicative.  It follows it cannot be a law-abiding Monad either.

Now the conclusion is the following: these two observations, put together, are an answer to your question.  Making list comprehensions use cantor or some similar technique to work in the infinite lists case would mean abandoning the idea that list comprehensions are an application of monads.  But Haskellers think that monads are too valuable a technique to neglect for a corner case like this.

Body:
Well the simplest, practical answer, which does take experience to appreciate, is that this situation is at most a minor annoyance.  A more elaborate answer, which takes still more experience to appreciate, comprises first two observations and a conclusion.

First observation: the Haskell community sees list comprehensions as a concrete application of monads.  The features offered by list comprehension coincide with the those provided by MonadPlus instance for lists, and GHC has a MonadComprehensions extension that allows you to use the comprehension syntax with any monad.

Second observation: there is a classic technique for interleaving infinite lists, due to famous mathematician Georg Cantor's proof that the set of natural numbers has the same cardinality ("size") as the set of rational numbers.  This can be easily translated into Haskell:

{-# LANGUAGE DeriveFunctor, TupleSections #-}

import Control.Applicative

cantor :: [a] -> [b] -> [(a, b)]
cantor [] _ = []
cantor _ [] = []
cantor (a:as) (b:bs) =
    (a,b) : interleave (map (a,) bs) (interleave (map (,b) as) (cantor as bs))

interleave :: [a] -> [a] -> [a]
interleave [] ys = ys
interleave (x:xs) ys = x : interleave ys xs

And we might try to define an alternative list Monad instance based on that.  First we define a wrapper type Cantor:

newtype Cantor a = Cantor [a]
    deriving (Eq, Functor)

Monad is a subclass of Applicative, so first we define an Applicative instance for Cantor:

instance Applicative Cantor where
    pure a = Cantor [a]
    Cantor fs <*> Cantor as = Cantor (map (uncurry ($)) (cantor fs as))

...but here we've already gone wrong, because it turns out that this violates the laws ("contract", in OO terms) of the Applicative class.  In Applicative, the following unit test must succeed no matter what values are passed in as arguments:

-- | One of the consequences of the associative law for 'Applicative'.
-- This function must return true no matter what arguments are passed
-- in.  Haskell's QuickCheck unit testing library runs tests in this
-- format, and this one will fail if we run it on 'Cantor'.
associativity
    :: (Applicative f, Eq (f (a, (b, c)))) => f a -> f b -> f c -> Bool
associativity as bs cs =
    as `pair` (bs `pair` cs) == fmap assoc ((as `pair` bs) `pair` cs)

pair :: Applicative f => f a -> f b -> f (a, b)
pair = liftA2 (,)

assoc :: ((a, b), c) -> (a, (b, c))
assoc ((a, b), c) = (a, (b, c))

That this test fails can be verified very easily by just running a few simple examples.  Note that pair in the Cantor type is the same as the cantor function as above, so we can simplify this test to this:

associativity' :: (Eq a, Eq b, Eq c) => [a] -> [b] -> [c] -> Bool
associativity' as bs cs =
    as `cantor` (bs `cantor` cs) == map assoc ((as `cantor` bs) `cantor` cs)

So Cantor is not a law-abiding Applicative.  It follows it cannot be a law-abiding Monad either.

Now the conclusion is the following: these two observations, put together, are an answer to your question.  Making list comprehensions use cantor or some similar technique to work in the infinite lists case would mean abandoning the idea that list comprehensions are an application of monads.  But Haskellers think that monads are too valuable a technique to neglect for a corner case like this.


答:
Why isn't there a true “Date-Only” data type?

For the same reasons, I think, that DateTime values are generally specified in UTC: simplicity and reliability. The point of a DateTime value is to specify a single point in time that's unaffected by time zone, daylight savings, calendar, and other local adjustments. DateTime values specify one instant (up to the limit of the type's resolution), not a time period or a set of times. These limitations make it possible to compare DateTime values in a reliable, predictable, uncomplicated way.

Trying to specify a date with a DateTime value is like trying to use a point to specify an area. You can make that work by using a convention, like "this point represents the center of a circle with a 100m radius," but there are a lot of problems there: everybody needs to use the same convention, you need to write a bunch of supporting code to make working with the wrong type less painful, and it's pretty much guaranteed that at some point you'll need to specify an area that's larger or smaller than the conventional area. So it is with dates: you can use 'noon' as the conventional time for specifying dates, but then you get into time zones because people expect to specify dates in their local time rather than UTC. And even if you come up with a satisfactory way to use a DateTime to specify a date, you'll need more information to know whether it's an absolute date or a relative one: is it July 4, 1776 or every July 4? What if you want to repeat using some other period? And calendars have all sorts of crazy issues: some months are longer than others, some years are longer than others, some days are even longer than others, and some calendars have gaps in them. You wouldn't want to solve these problems just for whole days, because the same issues come up for shorter periods: you'd probably like to be able to write code that expresses "take 1 pill every 4 hours" as readily as "the group meets on every third Friday."

So, there's lots of complication involved in working with dates. It's relatively (no pun intended) easy to provide a type that specifies a point in time and work with it as you would a number, but extremely difficult to provide a type that addresses all the ways that dates are used.

As others have pointed out, there are languages and libraries that provide good support for dates, and it's often a good idea to use them given it's quite difficult to get date-related code exactly right.


答:
Why isn't there a true “Date-Only” data type?

There are many such types in various libraries for various languages. There is almost certainly one for your current language. The Java util package had a horrible API for time calculations, but the introduction of the java.time package has made life much better. See java.time.LocalDate, containing a year-month-day value, or java.time.MonthDay, containing only a month and day number.


问:
Azure BLOB grained access
I have a standalone WPF desktop application which gathers data from Digital-to-Analog converter boards, computes, analyzes this data and then it creates binary files (proprietary format) with the result. There is a pace of one record (basically a dictionary with the resulted data) added each second in the final storage file.

Further, this real-time data must be available to a web application (which displays real-time graphs) hosted in Azure cloud. So, maybe obviously, an Azure Blob storage will be used where final storage files will be stored.

The ideal scenario is to update, as soon as possible, the web application with the new record computed on a machine where the standalone app is running. This means that the standalone app must upload a single record of data at once and not an entire file each second. The web app must be able to read the new record and not only the entire file. So the blobs which are hosted in the Azure blob storage must be read similar with local streams, using APIs like Stream.Seek(int) and Stream.ReadBytes(int).

Are there any possibilities to accomplish this using a grained control over a blob hosted in an Azure storage account?

If not, what other scenarios can be useful? An ASP Web Api 2 application exists for other purposes so maybe it can be leveraged as an intermediate between the standalone app and the web app. Maybe something as a queue?

Later edit

There are two kind of usage of the computed data. First one is described above, computed data must be viewed real-time on a web application. The second one emphasize the need of binary files and blobs: files are downloaded and edited offline. So is mandatory to have files generated and stored in a blob storage.

答:
Why do you want to use blobs instead of record-based storage?

The typical pattern for a data collection and analytics solution is

Collect raw data -> Transform data -> Store data -> Analyze -> Display

Azure has a set of services specifically designed to handle these steps very efficiently (like thousands of transactions per second):

Event Hub -> Data Factory -> DocumentDB or SQL DB -> HDInsights -> PowerBI or Web App

For lower volume applications you can use the general-purpose services which would probably be simpler to configure:

Web API or Storage Queue -> Web Job -> DocumentDB / SQL DB / Table Storage -> Web App

Title: Azure BLOB grained access


问:
OAuthv2 for distributed applications
Please note: Months ago I asked this question that received a truly amazing answer and introduction to OAuthv2.  I’m now knee deep in the implementation of such a system and have similar (but notably different) concerns, and so this question is not a dupe of it. It’s more like a “Part 2” that, hopefully, documents the proper setup for a distributed OAuthv2-backed architecture. I think there’s value to that for future readers and onlookers.

Essentially I have the following architecture:

A public RESTful web service provides CRUD operations on resources stored in other web services, databases, systems, etc.; and

A web application sits “in front of” that REST service and provides a nice web/mobile GUI to those REST endpoints, allowing logged in users the chance to hit the endpoints through a user interface

There is also a command-line tool that uses a HTTP client under the hood, so that if devops folk wish to hit the REST endpoint from their command-line, they can do so without having to write their own HTTP client or go through the UI (however they still need to authenticate/authorize themselves)

Sitting “behind” this public REST service are other “backing” web services, some of which are backed by other things such as databases, LDAP directories, etc.

Since the REST service is public, other clients/customers can build their own HTTP clients and integrate with it, and so the auth option must be tolerant to that scenario as well.

Every component needs to be authenticated and authorized to do anything here. The CLI tool that devops folk use must be authenticated against the public REST service. Mobile/web users must be authed before they can even log in to the web app, and the web app must auth against the public REST service. The public REST service must auth against its backing services. Those services must auth against their backing resources/services/databases. Etc. I’m looking for an OAuthv2 solution for all of this.

One item that is not in the picture above, but very important, is that there will be a single OAuthv2 Auth Server used by all the server-side components (because I own everything except the client-side). Hence all services, databases, etc. will have access to the same auth server (and in reality, it is a cluster of HA Auth Servers).

Which grant types to use for each component?

My understanding of OAuthv2’s Client Credentials grant is for components updating their own metadata with the Auth Server, such as their client secrets or redirect URLs (yes???).

This means I really have to “map” each of those components to one of the other 3 grant types:

Authorization code grant

Implicit grant

Resource Owner Password Credentials (hereafter ROPC)

After reading several | great OAuthv2 tutorials, it feels like the implicit grant is not applicable to any of the components here. It seems to be for when you have an unprotectable, client-side app that has no way of protecting access tokens. In my scenario, the only component that might qualify for this is the web app, however that will be on its own protected, trusted server that I control/own 100%.

So unless I’m misunderstanding something fundamental here, it seems I need to choose between Authorization Code grant and ROPC for all the following components/interactions:

End user authentication against web app (to login to the web app itself)

Web app authentication against public REST service

CLI authentication against public REST service

Custom built HTTP clients (not built by me, that is) authenticating against public REST service

Public REST service authenticating against each backing (private) web service

Private backing web services authenticating against their databases or external dependencies

Finally, my concerns!

I would imagine that the web app should use the Auth Code grant, but have concerns about that (see below). I would imagine the CLI tool and custom HTTP clients should use ROPC, but I have doubts about that as well. As far as the service-to-service and service-to-database use cases, I’m at a total loss. And so, finally, my questions:

For each of the 6 use cases identified above, what is the proper grant type to use, and why?

OK great, so that takes for of authentication. What about authorization?!? How does the auth server (or each component) assign roles/permissions to each access token?

In each of the 6 uses cases that require the Auth Code grant, who hangs onto the refresh token (the client app, the auth server, the user agent)? How often will the end user need to authorize the client app (just one time ever, each working session/login)? It would be strange to force users to authorize the client app every single time they sit down to work in that system.

答:
OAuth2 Flows

The OAuth flows in order of strongest to weakest are Authorization code grant then Implicit grant, then Resource Owner Password Credentials.

Authorization code grant

The Authorization code grant flow is used where not only the user needs to authenticate to gain access to a protected resouce but so does the 3rd party application. This means only applications you have approved in advance can act on the users behalf

In the Authorization code grant there are 2 sets of credentials; the users, and the 3rd party applications (called the client ID and client Secret - like a username and password but for a server

Implicit Grant flow

This flow is used when either the client secret and client ID can't be protected (that being if they wouldn't be held on a server but would be distributed as an application) or you explicitly want non preapproved applications to be able to act on the users behalf

Resource Owner Password Credentials

In this case the 3rd party app collects the credentials and then passes them onto you. If you're thinking that the 3rd party app could also email them to a hostile party; you're absolutely correct, they could. But sometimes this is the only practical implementation (e.g. a mobile app - although it is technically possible to use the Implicit Grant flow with a mobile app it is clunky)

Your Scenarios

I can see 2 things you want to protect; the public REST endpoints and the private web services.

public REST endpoints

End user authentication against web app (to login to the web app itself)

Here it is possible to use the Implicit Grant flow as redirecting to an authentication page is easy

Web app authentication against public REST service

Again it is possible here to use the Implicit Grant flow as redirecting to an authentication page is easy. However, as we're about to allow ROPC anyway don't worry about using ROPC if it is easier to implement (you are as secure as your weakest endpoint)

CLI authentication against public REST service

This seems like it will have to be ROPC or a slighly clunky autoopening of a webpage

Custom built HTTP clients (not built by me, that is) authenticating against public REST service

Again this seems like it will have to be ROPC or a slighly clunky autoopening of a webpage

Private web services

Public REST service authenticating against each backing (private) web service

Now this is an entirely different beast, you do want it only accessable by preapproved applications. These may not be authenticated by an OAuth flow at all but if they are you definately want to use a client ID and client Secret. As the user is not "present" in the interactions between the REST endpoints and the webservices you can only use the ROPC flow, but here you oblige the passing of valid clientID/clientSecrets. This means your (presumably) more high level access to your private webservices can't be accessed by anyone not approved by you. It may make more sense to use a 2 legged server to server OAUTH for this but that is outside my area of expertise.

Private backing web services authenticating against their databases or external dependencies

This is probably out of scope and will depend on exactly how your system is set up. I would expect to be inside your private network by this point.

Authorisation and authentication.

Technically OAuth is all about authorisation; authorising a 3rd party to act on behalf of the user. Howver this implicitly authenticates the user to the 3rd party (the authorisation wouldn't have succeeded unless they are who they say they are. OAuth implimentations intended for authentication will generally have an endpoint which gives information about the user.

The 3rd party requests certain authorisations using the scope parameter(e.g. to post to a users facebook wall) and in implicit and authorisation code flows the user should be asked if they permit the requesting application the requested permissions; the authorisation server then only allows the permitted actions (stored against the token)

Who holds the tokens

The 3rd party app (whatever form that may take) holds onto the tokens (both regular and refresh), their validity time is entirely up to you and dependant on your application. This could be between a few seconds (for authentication only applications) to infinity - generally refresh tokens are used for very long lived interactions.

Title: OAuthv2 for distributed applications


问:
Multiple Canvas in the Browser -- How to Proceed?
Here's something I wrote ages ago as Java applet.  I want to convert it into JavaScript and run in html5 canvas in the browser. From what I can see, canvas makes the most sense here.  (SVG / DIV techniques aren't so friendly for what I want to do.)  The canvas does allow for mouse drag and visual update, something you need to be able to understand the concepts displayed.

Actually that is four separate applets.  This doesn't translate perfectly well to canvas.  I'm not sure the best way to make this update to JavaScript/Canvas.  I think I have three choices:

One large canvas, divided into fourths.  (The problem here is when you grab and drag the control elements there is nothing stopping that drag from hitting a neighboring technique. I could program internal boundaries, but where is the fun in that? This is my least favorite option.)

Four individual canvases (spelling?), each with its own #id.

four iframes with each canvas as a stand alone in its own html file.  These would all be hosted at the same server.

Anybody got an opinion on best technique to proceed for displaying four functional graphic boxes (and why?)  As I understand it, because I'm asking for an opinion, this question doesn't really fit over at stackoverflow...

答:
Ouch.  It turns out because of a significant problem not previously mentioned (or even understood at the time of the original posting) it was WAY easier to go with four iframes.

Note that a significant portion of the display is common to all, and would have to be re-created four times.

The problem issue was a help oriented text screen that would over-write the canvas.  From what I can see Canvas and text divs can't occupy the same space in time.  for whatever reason, Canvas elements alway bleed thru the div including background images, etc... I ended up using jQuery to show and hide different elements. When you add up overlaps, the four canvas elements on a single page got way ugly.

Additionally, I had a heck of a time with CSS and multiple canvas elements. Its fine if you have a fixed size screen, floating elements, no so much.  I know iframes are "bad".. so be it.

using iframes made the job way easier..

```<code>    <div class="container">
        <iframe src="./bezier_curve.html?lineType=Simple"></iframe>
        <iframe src="./bezier_curve.html?lineType=Bezier"></iframe>
    </div>
    <div style="clear: both;"></div>
    <div class="container">
        <iframe src="./bezier_curve.html?lineType=BSpline"></iframe>
        <iframe src="./bezier_curve.html?lineType=NURB"></iframe>
    </div>
    <div style="clear: both;"></div>
</code>```

ref: How to pass a URL parameter...

Body:
Ouch.  It turns out because of a significant problem not previously mentioned (or even understood at the time of the original posting) it was WAY easier to go with four iframes.

Note that a significant portion of the display is common to all, and would have to be re-created four times.

The problem issue was a help oriented text screen that would over-write the canvas.  From what I can see Canvas and text divs can't occupy the same space in time.  for whatever reason, Canvas elements alway bleed thru the div including background images, etc... I ended up using jQuery to show and hide different elements. When you add up overlaps, the four canvas elements on a single page got way ugly.

Additionally, I had a heck of a time with CSS and multiple canvas elements. Its fine if you have a fixed size screen, floating elements, no so much.  I know iframes are "bad".. so be it.

using iframes made the job way easier..

    <div class="container">
        <iframe src="./bezier_curve.html?lineType=Simple"></iframe>
        <iframe src="./bezier_curve.html?lineType=Bezier"></iframe>
    </div>
    <div style="clear: both;"></div>
    <div class="container">
        <iframe src="./bezier_curve.html?lineType=BSpline"></iframe>
        <iframe src="./bezier_curve.html?lineType=NURB"></iframe>
    </div>
    <div style="clear: both;"></div>

ref: How to pass a URL parameter...


问:
Maybe monad vs exceptions
I wonder what are the advantages of Maybe monad over exceptions? It looks like Maybe is just explicit (and rather space-consuming) way of try..catch syntax.

update Please note that I'm intentionally not mentioning Haskell.

答:
I second Tikhon's answer, but I think there's a very important practical point that everybody's missing:

Exception handling mechanisms, at least in mainstream languages, are intimately coupled to individual threads.

The Either mechanism isn't coupled to to threads at all.

So something that we're seeing nowadays in real life is that many asynchronous programming solutions are adopting a variant of the Either-style of error handling.  Consider Javascript promises, as detailed in any of these links:

http://12devs.co.uk/articles/promises-an-alternative-way-to-approach-asynchronous-javascript/

http://www.html5rocks.com/en/tutorials/es6/promises/

http://www.toptal.com/javascript/javascript-promises

https://spring.io/understanding/javascript-promises

The concept of promises allows you write asynchronous code like this (taken from the last link):

```<code>var greetingPromise = sayHello();
greetingPromise
    .then(addExclamation)
    .then(function (greeting) {
        console.log(greeting);    // 'hello world!!!!’
    }, function(error) {
        console.error('uh oh: ', error);   // 'uh oh: something bad happened’
    });
</code>```

Basically, a promise is an object that:

Represents the result of an asynchronous computation, which may or may not have been finished yet;

Allows you to chain further operations to perform on its result, which will be triggered when that result is available, and whose results in turn are available as promises;

Allows you to hook up a failure handler that will be invoked if the promise's computation fails.  If there is no handler, then the error is propagated to later handlers in the chain.

Basically, since the language's native exception support doesn't work when your computation is happening across multiple threads, a promises implementation has to provide an error-handling mechanism, and these turn out to be monads similar to Haskell's Maybe/Either types.

Title: Maybe monad vs exceptions

Body:
I second Tikhon's answer, but I think there's a very important practical point that everybody's missing:

Exception handling mechanisms, at least in mainstream languages, are intimately coupled to individual threads.

The Either mechanism isn't coupled to to threads at all.

So something that we're seeing nowadays in real life is that many asynchronous programming solutions are adopting a variant of the Either-style of error handling.  Consider Javascript promises, as detailed in any of these links:

http://12devs.co.uk/articles/promises-an-alternative-way-to-approach-asynchronous-javascript/

http://www.html5rocks.com/en/tutorials/es6/promises/

http://www.toptal.com/javascript/javascript-promises

https://spring.io/understanding/javascript-promises

The concept of promises allows you write asynchronous code like this (taken from the last link):

var greetingPromise = sayHello();
greetingPromise
    .then(addExclamation)
    .then(function (greeting) {
        console.log(greeting);    // 'hello world!!!!’
    }, function(error) {
        console.error('uh oh: ', error);   // 'uh oh: something bad happened’
    });

Basically, a promise is an object that:

Represents the result of an asynchronous computation, which may or may not have been finished yet;

Allows you to chain further operations to perform on its result, which will be triggered when that result is available, and whose results in turn are available as promises;

Allows you to hook up a failure handler that will be invoked if the promise's computation fails.  If there is no handler, then the error is propagated to later handlers in the chain.

Basically, since the language's native exception support doesn't work when your computation is happening across multiple threads, a promises implementation has to provide an error-handling mechanism, and these turn out to be monads similar to Haskell's Maybe/Either types.


问:
Why is the Scala Option type not called Maybe, just as in Haskell?
Why is the Scala Option type not called Maybe, just as in Haskell?

Maybe makes a lot more "semantic sense" to me, but maybe Option has different behaviour I am not aware of.

Is there any particular reason why Option in Scala was not called Maybe?

答:
A better question here would be why Haskell's option type is called Maybe.  ML's option type is probably the grandaddy of all option types, and it's called option.

In any case, this is a terminological question, so it's not going to have a principled answer.  The people who picked the name in Haskell liked it better, that's it.


答: Calendrical manipulation is one of the poorest-understood aspects of computing.  Whole books have been written on the topic.  @MichealBlackburn is absolutely right in asking for a date-only datatype, one that does not resolve down to a point on a timeline, subject to re-interpretation.  Historically, there have been legitimate disputes about the meaning of a date.  One has to look no further than the adoption of the Gregorian calendar to find how complex it can get.  Furthermore, years have not always started on January 1st, even in western Europe and its colonies (e.g., Britain and British America started the year on March 25th).


问:
Inheritance when following the Repository Pattern in PHP
I am trying to build a PHP application using the Repository Pattern but I'm not sure how I should implement the save method.

I have an abstract class called ItemRepository which have the following method:

```<code>abstract class ItemRepository
{
    public function save(Item $item);
}
</code>```

Where Item also is an abstract class.

Now I want to implement the class MovieRepository which extends ItemRepository. Here I want to save instances of the Movie class which extends Item.

Though doing it like this in PHP

```<code>// MovieRepository.php
class MovieRepository extends ItemRepository
{
    public function save(Movie $movie)
    {
        ...
    }
}
</code>```

gives the following error

Declaration of MovieRepository::save() should be compatible with ItemRepository::save(Item $item)

What is the right way to do this?

答:
You can't. A subclass should support the same methods as the parent class, without imposing any additional restrictions. That way if you encounter a variable of type ItemRepository, you don't need to know which subclass it is; you can always call save and pass it an Item.

If you don't want this, you can add a different method, like saveMovie(Movie $movie). But you may want to reconsider whether you want a subclass at all.


问:
Using SessionStorage to keep client side state
I am working on an web app and I would like to keep the client data in the LOCAL
session storage.

I can't use the servers session and the app will only call some existing rest WS.I have to develop pure client side app, no server side rendering

In this case when going from one page to another I plan to read the data previously stored
for each page from local session storage and render each page accordingly so refreshing the page will not affect the behavior by deleting client data for each page

I am sure is doable but is this a proper way keeping each page's data?

This is suitable for me because for years I have done frontend development with in Java Swing.

Thanks

答:
Although using server side session state to store user data between page loads works, the downside is it eats memory on the server per user. So its not scalable.

keeping data in a cookie, although it seems old fashioned and you have to worry about its security. Is generally considered better.

A more modern approach is to use javascript client side databases. Or in a single page app you can simply persist the data in memory client side.

You can also get around the memory problem by storing the session in a DB or a separate scalable cache, such as memcache or aerospike etc However, this starts to raise more fundamental questions about the architecture of your application. Why do you need to have this state stored? is it actually better than regenerating it from a database on each page load? etc etc

Again in general I would say the popular view is to move towards stateless JSON services which are called via client side AJAX. (I'm not going to call it "REST" becuase its such a loaded term)


问:
Does a tree node have an ancestor?
Below is a rooted tree, where any node C except root has one parent P

Ancestors of a node C are the nodes on path from C to root, including P,P's parent,P's grandparent, .... till root.

My understanding is, Tree is just a set of nodes & edges that connect these nodes. Between any two nodes there is exactly one single path. A tree does not need to have any root.

Any rooted tree MUST be a tree but any tree may not be a rooted tree.

In the below tree(not a rooted tree),

--

My understanding is, parent-child, depth, height, subtree, siblings, leaf-node concepts are only applied to rooted trees.

If no, then,

Is X a parent of Y? If yes, Who are the ancestors of Y?

答:
No.  The concept of "parent" and "ancestor" applies to the rooted tree because one unique root is defined and since there is a connected graph there is always a path from any node to the root.

In an unrooted tree there is no root defined and therefore the concepts of "parent" and "ancestor" do not apply.

In that graph either X or Y could be labelled as the root, and if this were the case then either would be parent and ancestor accordingly.

Title: Does a tree node have an ancestor?


问:
How do SQL transactions return immediate results?
I am using transactions to do bulk insert/updates. This is my little test loop:

```<code>$now = date('Y-m-d H:i:s');
for ($i=0; $i<60; $i++) {
    $db->insert($cfg['ps_manufacturer'], array(
        'reference' => 'MF'.$i,
        'name'      => 'MF NAME'.$i,
        'date_add'  => $now,
        'date_upd'  => $now,
        'active'    => true,
    ));
    echo $db->lastInsertId().'<br>';
}
</code>```

Now the script above is obviously very slow because the query is in a loop. By using transactions the execution time drops from 2s to 0.06s:

```<code>$db->beginTransaction();
$now = date('Y-m-d H:i:s');
for ($i=0; $i<60; $i++) {
    $db->insert($cfg['ps_manufacturer'], array(
        'reference' => 'MF'.$i,
        'name'      => 'MF NAME'.$i,
        'date_add'  => $now,
        'date_upd'  => $now,
        'active'    => true,
    ));
    echo $db->lastInsertId().'<br>';
}
$db->commit();
</code>```

Now what confuses me is how does the second sample return an ID of every inserted row immediatelly? From my understanding the transaction starts and queues all procedures, until the queue is "released" by using commit().

Now obviously the test showed that I'm wrong, could someone explain how does this work? It does essentially the same, but works a whole lot faster. Does using transactions start some kind of "procedural session" which is optimized for continued querying? This code is a bit counter-intuitive to me, almost looks like async programming :)

答:
There are two different questions here, one about the performance behaviour, and one about the auto generated IDs.

From my understanding the transaction starts and queues all procedures, until the queue is "released" by using commit()

That is only partially correct. When the transaction starts, the database makes something like a "private snapshot" and execute your inserts immediately there. All other users of the database won't see your changes until you call commit. So the database can do this typically in main memory. It has to persist your inserts to a hard disk only once, as well as it has to ensure consistency with the concurrent operations of the other users only once at the end. That is why the operation works faster than making a commit after every "insert" (which is what happens automatically in your first example).

And to your second question:

how does the second sample return an ID of every inserted row immediately?

Auto increment columns are incremented outside the scope of your transaction. See this former SO post for an explanation.