Transactions in REST?

Question: I'm wondering how you'd implement the following use-case in REST. Is it even possible to do without compromising the conceptual model?
Read or update multiple resources within the scope of a single transaction. For example, transfer $100 from Bob's bank account into John's account.
As far as I can tell, the only way to implement this is by cheating. You could POST to the resource associated with either John or Bob and carry out the entire operation using a single transaction. As far as I'm concerned this breaks the REST architecture because you're essentially tunneling an RPC call through POST instead of really operating on individual resources.
Answer: Consider a RESTful shopping basket scenario.  The shopping basket is conceptually your transaction wrapper.  In the same way that you can add multiple items to a shopping basket  and then submit that basket to process the order, you can add Bob's account entry to the transaction wrapper and then Bill's account entry to the wrapper.   When all the pieces are in place then you can POST/PUT the transaction wrapper with all the component pieces.
Comment: Why would TransferMoneyTransaction not be a viable banking resource?
Comment: e.g. What happens if you do an HTTP DELETE on the endpoint UpdateXYZ ?   Does it delete XYZ?  Does it delete the Update or does it just do an Update and ignore the HTTP verb delete.  By keeping verbs out of the endpoint you remove the confusion.
Comment: If your ensure that your endpoints refer to nouns then it is usually intuitive what the standard GET, PUT, POST, DELETE verbs will do to that noun.  RPC  allows endpoints to be verbs themselves and therefore they can conflict with the HTTP verbs and the intent becomes confusing.
Comment: And what about transactions across multiple services? and what about when you want to do a set of 'unrelated' changes that the service exposes no implicit transaction container.. plus, why have a specific transaction type when we're moved to general purpose transactions that are completely unrelated to your actual data changes. Transactions might not match restful, but it seems like transactions should be layered ontop, unrelated to the rest calls apart from the fact the request headers would contain a transaction reference.
Comment: @meandmycode  Database transactions should be layered behind a REST interface.  Alternately you can expose a business transactions (not a database transaction) as a resource in itself and then you need to take compensating action in case of failure.
Comment: I get the shopping basket example, but how when you generalize this it doesn't sound RESTful at all (you're not operating on resources). There is no realistic bank-related resource to wrap John and Bob.

Good answer though! I feel like you're definitely moving in the right direction.
Comment: I thought a resource was supposed to be a real-life object, not a service (i.e. method invocation). If you're able to make up resources that mimic services then how is REST any different from RPC?
Comment: this isnt what i mean.. scenario: i want to add an album to my catalog, and an artist to my artists catalog (with a link from album to artist). i want to have this all or nothing, and i dont want to compensate, the operation should feel atomic.
Comment: Put your scenario as a separate question and I'll answer it there.  Comments are a pain for discussing this.  Also, you will get input from others too.
Comment: this is so wrong! shopping baskets are not a transaction. if you require another step to submit it, then technically the shopping basket can change after you have added items and before you submit
Answer: There are a few important cases that aren't answered by this question, which I think is too bad, because it has a high ranking on Google for the search terms :-)
Specifically, a nice propertly would be: If you POST twice (because some cache hiccupped in the intermediate) you should not transfer the amount twice.
To get to this, you create a transaction as an object. This could contain all the data you know already, and put the transaction in a pending state.
POST /transfer/txn
{"source":"john's account", "destination":"bob's account", "amount":10}

{"id":"/transfer/txn/12345", "state":"pending", "source":...}

Once you have this transaction, you can commit it, something like:
PUT /transfer/txn/12345
{"id":"/transfer/txn/12345", "state":"committed", ...}

{"id":"/transfer/txn/12345", "state":"committed", ...}

Note that multiple puts don't matter at this point; even a GET on the txn would return the current state. Specifically, the second PUT would detect that the first was already in the appropriate state, and just return it -- or, if you try to put it into the "rolledback" state after it's already in "committed" state, you would get an error, and the actual committed transaction back.
As long as you talk to a single database, or a database with an integrated transaction monitor, this mechanism will actually work just fine. You might additionally introduce time-outs for transactions, which you could even express using Expires headers if you wanted to.
Comment: Interesting discussion! I'd like to add that the initial post must be performed in one step. It cannot be added to later (then we're in shopping-cart territory and shopping carts have many checks and balances to prevent them from causing harm to the end user, even legislation, bank transfers does not)...
Answer: In REST terms, resources are nouns that can be acted on with CRUD (create/read/update/delete) verbs. Since there is no "transfer money" verb, we need to define a "transaction" resource that can be acted upon with CRUD. Here's an example in HTTP+POX. First step is to CREATE (HTTP POST method) a new empty transaction:
POST /transaction

This returns a transaction ID, e.g. "1234" and according URL "/transaction/1234". Note that firing this POST multiple times will not create the same transaction with multiple IDs and also avoids introduction of a "pending" state. Also, POST can't always be idempotent (a REST requirement), so it's generally good practice to minimize data in POSTs.
You could leave the generation of a transaction ID up to the client. In this case, you would POST /transaction/1234 to create transaction "1234" and the server would return an error if it already existed. In the error response, the server could return a currently unused ID with an appropriate URL. It's not a good idea to query the server for a new ID with a GET method, since GET should never alter server state, and creating/reserving a new ID would alter server state.
Next up, we UPDATE (PUT HTTP method) the transaction with all data, implicitly committing it:
PUT /transaction/1234
<transaction>
  <from>/account/john</from>
  <to>/account/bob</to>
  <amount>100</amount>
</transaction>

If a transaction with ID "1234" has been PUT before, the server gives an error response, otherwise an OK response and a URL to view the completed transaction.
NB: in /account/john , "john" should really be John's unique account number.
Comment: Serious mistake?  I know there are differences between PUT and POST, but there is a loose mapping to CRUD.  "Seriously"?
Comment: Equating REST with CRUD is a serious mistake. POST does not have to mean CREATE.
Comment: Yes, seriously. CRUD is a way of structuring data storage; REST is a ways of structuring application data flow. You can do CRUD on REST, but you cannot do REST on CRUD. They are not equivalent.
Answer: Great question, REST is mostly explained with database-like examples, where something is stored, updated, retrieved, deleted.  There are few examples like this one, where the server is supposed to process the data in some way.  I don't think Roy Fielding included any in his thesis, which was based on http after all.
But he does talk about "representational state transfer" as a state machine, with links moving to the next state.  In this way, the documents (the representations) keep track of the client state, instead of the server having to do it.  In this way, there is no client state, only  state in terms of which link you are on.
I've been thinking about this, and it seems to me reasonable that to get the server to process something for you, when you upload, the server would automatically create related resources, and give you the links to them (in fact, it wouldn't need to automatically create them: it could just tell you the links, and it only create them when and if you follow them - lazy creation). And to also give you links to create new related resources - a related resource has the same URI but is longer (adds a suffix). For example:

You upload (POST) the representation of the concept of a transaction with all the information.  This looks just like a RPC call, but it's really creating the "proposed transaction resource".   e.g URI: /transaction
Glitches will cause multiple such resources to be created, each with a different URI.
The server's response states the created resource's URI, its representation - this  includes the link (URI) to create the related resource of a new "committed transaction resource". Other related resources are the link to delete the proposed transaction. These are states in the state-machine, which the client can follow. Logically, these are part of the resource that has been created on the server, beyond the information the client supplied. e.g URIs: /transaction/1234/proposed,  /transaction/1234/committed
You POST to the link to create the "committed transaction resource", which creates that resource, changing the state of the server (the balances of the two accounts)**. By its nature, this resource can only be created once, and can't be updated. Therefore, glitches committing many transactions can't occur.
You can GET those two resources, to see what their state is. Assuming that a POST can change other resources, the proposal would now be flagged as "committed" (or perhaps, not available at all).

This is similar to how webpages operate, with the final webpage saying "are you sure you want to do this?" That final webpage is itself a representation of the state of the transaction, which includes a link to go to the next state. Not just financial transactions; also (eg) preview then commit on wikipedia.  I guess the distinction in REST is that each stage in the sequence of states has an explicit name (its URI).
In real-life transactions/sales, there are often different physical documents for different stages of a transaction (proposal, purchase order, receipt etc). Even more for buying a house, with settlement etc.
OTOH This feels like playing with semantics to me; I'm uncomfortable with the nominalization of converting verbs into nouns to make it RESTful, "because it uses nouns (URIs) instead of verbs (RPC calls)".  i.e. the noun "committed transaction resource" instead of the verb "commit this transaction". I guess one advantage of nominalization is you can refer to the resource by name, instead of needing to specify it in some other way (such as maintaining session state, so you know what "this" transaction is...)
But the important question is: What are the benefits of this approach?  i.e. In what way is this REST-style better than RPC-style? Is a technique that's great for webpages also helpful for processing information, beyond store/retrieve/update/delete? I think that the key benefit of REST is scalability; one aspect of that is not needing to maintain client state explicitly (but making it implicit in the URI of the resource, and the next states as links in its representation).  In that sense it helps. Perhaps this helps in layering/pipelining too? OTOH only the one user will look at their specific transaction, so there's no advantage in caching it so others can read it, the big win for http.
Comment: Could you please explain how "not needing to keep state on the client" helps scalability? What kind of scalability? Scalability in what sense?
Comment: how does the transaction not store information from the client? /shrug
Answer: I've drifted away from this topic for 10 years. Coming back, I can't believe the religion masquerading as science that you wade into when you google rest+reliable. The confusion is mythic.
I would divide this broad question into three:

Downstream services. Any web service you  develop will have downstream services that you use, and whose transaction syntax you have no choice but to follow. You should try and hide all this from users of your service, and make sure all parts of your operation succeed or fail as a group, then return this result to your users.
Your services. Clients want unambiguous outcomes to web-service calls, and the usual REST pattern of making POST, PUT or DELETE requests directly on substantive resources strikes me as a poor, and easily improved, way of providing this certainty. If you care about reliability, you need to identify action requests. This id can be a guid created on the client, or a seed value from a relational DB on the server, it doesn't matter. For server generated ID's, use a 'preflight' request-response  to exchange the id of the action. If this request fails or half succeeds, no problem, the client just repeats the request. Unused ids do no harm.This is important because it lets all subsequent requests be fully idempotent, in the sense that if they are repeated n times they return the same result and cause nothing further to happen. The server stores all responses against the action id, and if it sees the same request, it replays the same response. A fuller treatment of the pattern is in this google doc. The doc suggests an implementation that, I believe(!), broadly follows REST principals. Experts will surely tell me how it violates others. This pattern can be usefully employed for any unsafe call to your web-service, whether or not there are downstream transactions involved.
Integration of your service into "transactions" controlled by upstream services. In the context of web-services, full ACID transactions are considered as usually not worth the effort, but you can greatly help consumers of your service by providing cancel and/or confirm links in your confirmation response, and thus achieve transactions by compensation.

Your requirement is a fundamental one. Don't let people tell you your solution is not kosher. Judge their architectures in the light of how well, and how simply, they address your problem.
Answer: If you stand back to summarize the discussion here, it's pretty clear that REST is not appropriate for many APIs, particularly when the client-server interaction is inherently stateful, as it is with non-trivial transactions.  Why jump through all the hoops suggested, for client and server both, in order to pedantically follow some principle that doesn't fit the problem?  A better principle is to give the client the easiest, most natural, productive way to compose with the application.
In summary, if you're really doing a lot of transactions (types, not instances) in your application, you really shouldn't be creating a RESTful API.
Comment: Right, but what should be an alternative in case of distributed micro-service architecture?
Comment: @Vitamon definitely gRPC
Answer: You'd have to roll your own "transaction id" type of tx management.  So it would be 4 calls:
http://service/transaction (some sort of tx request)
http://service/bankaccount/bob (give tx id)
http://service/bankaccount/john (give tx id)
http://service/transaction (request to commit)

You'd have to handle the storing of the actions in a DB (if load balanced) or in memory or such, then handling commit, rollback, timeout.
Not really a RESTful day in the park.
Comment: I don't think this is a particularly good illustration. You only need two steps: Create transaction (creates a transaction in "pending" state) and Commit transaction (commits if uncommited, and moves the resource to committed or rolled-back state).
Answer: First of all transferring money is nothing that you can not do in a single resource call. The action you want to do is sending money. So you add a money transfer resource to the account of the sender.
POST: accounts/alice, new Transfer {target:"BOB", abmount:100, currency:"CHF"}.

Done. You do not need to know that this is a transaction that must be atomic etc. You just transfer money aka. send money from A to B.

But for the rare cases here a general solution:
If you want to do something very complex involving many resources in a defined context with a lot of restrictions that actually cross the what vs. why barrier (business vs. implementation knowledge) you need to transfer state. Since REST should be stateless you as a client need to transfer the state around.
If you transfer state you need to hide the information inside from the client. The client should not know internal information only needed by the implementation but does not carry information relevant in terms of business. If those information have no business value the state should be encrypted and a metaphor like token, pass or something need to be used.
This way one can pass internal state around and using encryption and signing the system can be still be secure and sound. Finding the right abstraction for the client why he passes around state information is something that is up to the design and architecture.

The real solution:
Remember REST is talking HTTP and HTTP comes with the concept of using cookies. Those cookies are often forgotten when people talk about REST API and workflows and interactions spanning multiple resources or requests.
Remember what is written in the Wikipedia about HTTP cookies:

Cookies were designed to be a reliable mechanism for websites to remember stateful information (such as items in a shopping cart) or to record the user's browsing activity (including clicking particular buttons, logging in, or recording which pages were visited by the user as far back as months or years ago).

So basically if you need to pass on state, use a cookie. It is designed for exactly the very same reason, it is HTTP and therefore it is compatible to REST by design :).

The better solution:
If you talk about a client performing a workflow involving multiple requests you usually talk about protocol. Every form of protocol comes with a set of preconditions for each potential step like perform step A before you can do B.
This is natural but exposing protocol to clients makes everything more complex. In order to avoid it just think what we do when we have to do complex interactions and things in the real world... . We use an Agent.
Using the Agent metaphor you can provide a resource that can perform all necessary steps for you and store the actual assignment / instructions it is acting upon in its list (so we can use POST on the agent or an 'agency').
A complex example:
Buying a house:
You need to prove your credibility (like providing your police record entries), you need to ensure financial details, you need to buy the actual house using a lawyer and a trusted third party storing the funds, verify that the house now belongs to you and add the buying stuff to your tax records etc. (just as an example, some steps may be wrong or whatever).
These steps might take several days to be completed, some can be done in parallel etc.
In order to do this, you just give the agent the task buy house like:
POST: agency.com/ { task: "buy house", target:"link:toHouse", credibilities:"IamMe"}.

Done. The agency sends you back a reference to you that you can use to see and track the status of this job and the rest is done automatically by the agents of the agency.
Think about a bug tracker for instance. Basically you report the bug and can use the bug id to check whats going on. You can even use a service to listen to changes of this resource. Mission Done.
Answer: You must not use server side transactions in REST.
One of the REST contraints:

Stateless
The client–server communication is further constrained by no client context being stored on the server between requests. Each request from any client contains all of the information necessary to service the request, and any session state is held in the client.

The only RESTful way is to create a transaction redo log and put it into the client state. With the requests the client sends the redo log and the server redoes the transaction and

rolls the transaction back but provides a new transaction redo log (one step further)
or finally complete the transaction.

But maybe it's simpler to use a server session based technology which supports server side transactions.
Comment: The quote is from the wikipedia REST entry. Is this the real source or did wikipedia get it from somewhere? Who's to say what's client context and what's server context?
Answer: I think that in this case it is totally acceptable to break the pure theory of REST in this situation. In any case, I don't think there is anything actually in REST that says you can't touch dependent objects in business cases that require it.
I really think it's not worth the extra hoops you would jump through to create a custom transaction manager, when you could just leverage the database to do it.
Comment: then it's just a normal web call, rpc or name it what you want, but it isn't REST
Comment: @bebbo ... 13 years later my answer has more nuance. If you need transactions, your view of REST is probably coupled to the database structure and you have a leaky abstraction. The Resource in this case is the transfer and it should encompass all of the information required to create the resource.
Answer: In the simple case (without distributed resources), you could consider the transaction as a resource, where the act of creating it attains the end objective.
So, to transfer between <url-base>/account/a and <url-base>/account/b, you could post the following to <url-base>/transfer.

<transfer>
    <from><url-base>/account/a</from>
    <to><url-base>/account/b</to>
    <amount>50</amount>
</transfer>

This would create a new transfer resource and return the new url of the transfer - for example <url-base>/transfer/256.
At the moment of successful post, then, the 'real' transaction is carried out on the server, and the amount removed from one account and added to another.
This, however, doesn't cover a distributed transaction (if, say 'a' is held at one bank behind one service, and 'b' is held at another bank behind another service) - other than to say "try to phrase all operations in ways that don't require distributed transactions".
Comment: The other problem with this suggestion is that, if a cache hiccups and POSTs twice, you get two transfers.
Comment: If you can't "phrase all operations in ways that don't require distributed transactions", then you really do need a two phase commit. The best idea I could find for implementing two phase commit on REST is http://rest.blueoxen.net/cgi-bin/wiki.pl?TwoPhaseCommit, which importantly doesn't mess up the URL namespace and allows a two phase commit to be layered over clean REST semantics.
Comment: True, in which case you'd need to have a two step process - create a "transfer" resource with a unique URL then add the transfer details to it as part of the commit (two parts as mentioned in the other answers).  Of course, this could then be phrased as creating a "transaction" resource then adding a "transfer" operation to it.
Answer: I believe that would be the case of using a unique identifier generated on the client to ensure that the connection hiccup not imply in an duplicity saved by the API.
I think using a client generated GUID field along with the transfer object and ensuring that the same GUID was not reinserted again would be a simpler solution to the bank transfer matter.
Do not know about more complex scenarios, such as multiple airline ticket booking or micro architectures.
I found a paper about the subject, relating the experiences of dealing with the transaction atomicity in RESTful services.
Answer: I guess you could include the TAN in the URL/resource:

PUT /transaction to get the ID (e.g. "1")
[PUT, GET, POST, whatever] /1/account/bob
[PUT, GET, POST, whatever] /1/account/bill
DELETE /transaction with ID 1

Just an idea.
Comment: If a client has to assemble URIs, then your API is not RESTful.
Comment: I can't understand you guys, really! If you treat a transaction as a resource (like in the example above) you simply stop treating transaction in the classical sense and utilize it in "the proper REST way" which further simplifies programming transactional processes. You can for example include an href to the transaction in your responses to go around the displacement in distributed server-side environment, it's still stateless (it's just a resource, isn't it?) and you can implement the actual transaction mechanism anyway you want (what if you don't have a DB in the back?)
Comment: One way or the other if you simply stop thinking SQL/SOAP and start thinking HTTP (like the browser does) everything becomes simple
Comment: I see two problems with this approach:

1) It implies you can't access a resource outside a transaction (though maybe this isn't a big deal).
2) None of the answers so far has touched upon the fact that the server is no longer stateless, though I suspect nothing can be done about that.
Comment: Well, /1/account/bob and /account/bob are just two different resources. :) And RE: stateless, it implies that the resource is always available and not dependent on a previous request. Since you asked for transactions, yes that is not the case. But then again, you wanted transactions.

Transactions in REST?


Statistical modelling of outcome of an inquiry

Question: Suppose a population of dentists are sent an inquiry. A number of inquiries are filled in and sent back. Each question on the inquiry represents a certain number of points. The total number of points represents the precentage that a dentist adheres to some safety regulation. I would like to test whether there is some significant relation between the outcome of the question "how long are you employed as a dentist" versus the total number of points. Ie., I would like to test whether older/younger dentists work more or less by the rules.
I am not quite sure how I would go about testing this. I thought about checking whether the percentage over the whole sample is normally distributed, and then testing whether the percentage within an agegroup is likely to come from the same distribution, but I'm not sure if this is the best way to go about it. Any help would be greatly appreciated.
Answer: It seems that you have a predictor, the answer to "How long are you employed as a dentist ?", which could be discrete or continuous depending on what types of answer you allowed; & a response, the total number of points, which can likely be treated as continuous.  Perform  analysis of variance or linear regression as appropriate (& check the assumptions).
Answer: @Scortchi's answer will work fine if you assume a linear relationship between experience and rule-following (roughly: if dentist A has twice as much experience as dentist B, he will also follow the rules twice as much).
If this assumption is unrealistic, you could test for a monotonic relationship between experience and rules-following without assuming this specific relationship, e.g., using Spearman's or Kendall's correlation coefficient. See here.


Orders in extension of $Q_p$

Question: Suppose we have field extensions $L/K/\mathbb{Q}_p$. If $R$ is a ring such that
$$\mathcal{O}_K \subset R \subset \mathcal{O}_L$$
(where if $M$ is an extension of $\mathbb{Q}_p$ then $\mathcal{O}_M$ is the integral closure of $\mathbb{Z}_p$ in $M$) and $R$ has rank $[L : K]$ as an $\mathcal{O}_K$-module, then can we say that $R = \mathcal{O}_L$? In the number field case, this isn't true, but I'm wondering if it is true in the local case.
Answer: The ring of integers of $ \mathbf Q_2(\sqrt{5}) $ is not $ \mathbf Z_2[\sqrt{5}] $, it's $ \mathbf Z_2[(1 + \sqrt{5})/2] $, so that's a counterexample to your claim.
In general, you can turn global counterexamples to this into local ones by recalling that the discriminant captures the ramification information of a number ring, so if you pick an order lying inside it whose discriminant includes a prime factor $ p $ which is unramified in the actual number ring, then you'll get a counterexample after taking completions of the rings at $ p $. In the example I gave, the discriminant of $ \mathbf Z[(1 + \sqrt{5})/2] $ is $ 5 $, but the discriminant of the order $ \mathbf Z[\sqrt 5] $ is $ 20 = 2^2 \cdot 5 $, so you get a counterexample after completing at the prime $ p = 2 $.

Orders in extension of $Q_p$


Vector function of distance traveled.

Question: Let the scenario be the following:
We have a driving car whose start velocity is $100\frac{m}{s}$ and it's brakes reduce the velocity by $10\frac{m}{s}$, quite simple.
If we were to make a vector function of distance-traveled vs velocity.
The velocity would be equal to:
$$y(t)=v - v_{brake}\cdot t$$
Because, for every time step, the brakes reduce the speed by the amount listed above eg $v_{brake}\cdot t$.
The distance traveled:
$$x(t)=y(t)\cdot t$$
My reasoning being:
$$distance = v\cdot t$$
A plot of the vector function:
plot
Although this might seem pretty reasonable, it should be clear that the units don't add up.
Velocity would be $\frac{m}{s} - m$, which is illegal according to the physics police. This would also invalidate the second function for distance traveled. I've probably overlooked something important.
Sorry for asking such a simple question.
Comment: $$x(t) = y(t) * t = t(v - v_{brake} * t) = vt - v_{brake} * t^2 = [LT^{-1} * T] - [LT^{-2} * T^2] = [L] - [L] = [L]$$  Which is dimensionally correct. I think you made a mistake by considering $v_{brake}$ as velocity
Answer: The brakes reduce it's velocity by $10$ m/s in every second. So the acceleration is $-10 \, \text{m/s}^2$.
So
$$y(t)=v-10\,\text{ms}^{-2}t$$
$$x(t)=100\,\text{ms}^{-1}t+\frac{1}{2}(-10\,\text{ms}^{-2})t^2$$

Vector function of distance traveled.


Java - Is this good practice?

Question: I'm designing a simple car rental system and was wondering whether I was using good practice for the design. Basically, I have a showroom arraylist, which stores all rental cars (new objects). Inside each car object is an arraylist for the availability of that car for that month. The availability comprises of 31 values (corresponding to each day in that month), either 0 or 1 (available). They are initially set to 1 until a car is booked for that day. Is there another way of incorporating availability?
ArrayList<Car> showroom = new ArrayList<Car>();

ArrayList<Integer> Available1 = new ArrayList<Integer>();
ArrayList<Integer> Available2 = new ArrayList<Integer>();

setAllDatesAvailable(Available1);
setAllDatesAvailable(Available2);

Car number1 = new Car(objectitems, ... , Available1);
showroom.add(number1);
Car number2 = new Car(objectitems, ... , Available2);
showroom.add(number2);

//booking process

setAllDatesAvailable(ArrayList Array) {
   for (int i = 0; i < 31; i++) {
      Array.add(1);
}

NB:

i'm not worried about timestamps, assume that the car is booked for
the whole day
assume that the system is only for a single month of 31 days
Comment: Knock the door of [codereview.se] with this question.
Comment: start by changing the declarations to List, the interface.
Answer: It would be simpler to record availability as an array of booleans (or ints). There's no need to use ArrayList for fixed-length records like this (the main benefit of ArrayList is that it can grow dynamically).
I think it would be better to use the default value of boolean or int to indicate availability - then you won't need to explicitly initialize the array at all. For boolean this would mean recording bookings not availability:
    boolean[] booked = new boolean[31];
    System.out.println(booked[0]);  // false

This also highlights a potential confusion - days of the month often get confused in software because of zero-indexing, so you need to be clear whether the 1st day of the month is 0 or 1.
It would be better and more Object-Oriented to store (and initialise, if necessary) the bookings/availability inside the Car object, and provide methods on the Car object to manipulate that data as required.
Comment: +1 for the boolean array, no need for a List for this.
Answer: Here are some improvements that I would suggest for your code: -

First, follow Java Naming Conventions. Variable name should start with lowercase letters. Available -> available, or even better, availableDates to match the purpose of your List
Always use Generic Type collections.
setAllDatesAvailable(ArrayList Array)

to:
setAllDatesAvailable(ArrayList<Integer> array);

Use Interface as your Reference types. You should declare your list as: -
List<Integer> availableDates = new ArrayList<Integer>();

Same thing also in the formal parameters in your methods, as shown in comment by @Alex.
Choose meaningful name for your variables.
Car number1 = ...; <-- // makes no sense to reader

change it to: -
Car car1 = ...;

As far as possible, try to name your formal parameters same as of your actual parameters.
setAllDatesAvailable(ArrayList<Integer> array);

should better be declared as: -
setAllDatesAvailable(List<Integer> availableDates);

After all these changes, you can look forward to improvise the functionality of your code, which @DNA has very nicely written in his answer.
Comment: Use also the `List` interface for parameters in `setAllDatesAvailable(List availableDates);` or a `Collection` if you are just traversing it.
Comment: @Alex.. Oh yeah. I missed that.
Answer: Something like below maybe - I'd avoid passing around naked collections. LocalDate is in the joda library. Default scope on Car is deliberate and assuming these are in the same package but the client is not:
public class Showroom {

    private Collection<Car> cars = new ArrayList<Car>();

    public boolean isCarAvailable(Car car, LocalDate day) {
        return car.isAvailable(day);
    }

    public void bookCarOnDate(Car car, LocalDate day) throws IlegalStateException {
          car.book(day);
    }

    public Collection<Car> getCars() {
          return Collection.unmodifiableCollection(cars);
    }
}

class Car {

    private Map<LocalDate, Boolean> dayAvailability = new HashMap<LocalDate, Boolean>();

    Car() {
        //add other private attributes
        //don't provide setters for these - create new whenever modified so don't leak
        //state and thread safety
    }

    boolean isAvailable(LocalDate day) {
       return dayAvailability.get(day) != null && dayAvailability.get(day);
    }

    void book(LocalDate day) {
     if (isAvailable(day)) {
         dayAvailability.put(car, true);
     else {
         throw IllegalStateException("Car "+car+" not available for booking on date "+localDate);
     }
}

Java - Is this good practice?


How to make Flex Markers appear in Logic Pro X?

Question: I'm trying to use Flex Markers in Logic Pro X. They finally appeared on one audio track after a half an hour of experimentation and reading documentation (by accident, I discovered that I had to delete all the alternate takes), but another half hour of experimentation hasn't made them appear on a second track. Deleting all the alternate takes wasn't enough this time. The first one has options for Quantize, Q-Swing etc. in the Region pane, but the second one does not. All it has is "Flex and Follow", which is "on" like the other. Both have "Flex Time" set to "Polyphonic" in the edit pane at the bottom of the screen.
Is there any repeatable way to predictably force Flex Markers to appear in Logic Pro X?
Answer: You may need to hit the "Show/Hide Flex" button, which is next to the "Show/Hide Automation" button. The keyboard shortcut is Command-F.
Additionally, you don't need to delete all the alternate tracks, but you have to flex edit them each individually. If the tracks are packed, you will need to expand the take folder, and you will be able to flex edit each take.
Comment: Ahh, thank you. The Take Folder thing is working. Where are those two buttons located? I can't find either one.
Comment: Along the top of the arrange window; to the right of the Edit, Functions, and View menus
Comment: The Arrange Window is the main window, right?
Comment: Yes, the window with all the audio and MIDI tracks that make up your session.
Answer: I've had issues turning on flex and nothing happens.
My solution:
I end up having to import the audio clip to a new track and then it works.


Passar mouse - efeito descer imagem

Question: Como posso fazer um efeito ao passar o mouse em cima da imagem, descer uma caixa em baixo dessa imagem?
Ps: parecido com MENU quando passa o mouse

Answer: Exite uma infinidade de formas para fazer isso. Eu fiz um pequeno exemplo que pode te ajudar. Leia o código e de uma estudada nele para entender o que foi feito e como vc pode customiza-lo. Deixei o css o mais simples possível pra ficar mais fácil de entender.
Usei transition para fazer o efeito aparecendo e um pseudo elemento para criar a legenda usando um atributo custom data-name no content do pseudo elemento. Outra coisa o elemento  não precisa da tag de fechamento
Veja o exemplo para entender melhor:

a {
    position: relative;
}
a::after {
    content: attr(data-name);
    position: absolute;
    left: 0;
    top: 100%;
    width: 100%;
    background-color: red;
    text-align: center;
    transform: scaleY(0);
    transform-origin: top;
    opacity: 0;
    transition: opacity 300ms, transform 300ms;
}
a:hover::after {
    transform: scaleY(1);
    opacity: 1;
}

Passar mouse - efeito descer imagem


Is that possible to get device token using OneSignal

Question: Maybe someone knows how to get device token when using OneSignal to receive notifications?
Answer: This is it:
OneSignal.idsAvailable(new OneSignal.IdsAvailableHandler() {
            @Override
            public void idsAvailable(String userId, String registrationId) {
                Log.d("debug", "User:" + userId);
                if (registrationId != null)
                    Log.d("debug", "registrationId:" + registrationId);

            }
        });

It helps to get user id and that is actually a device token.
Comment: Also one can use OneSignal.getPermissionSubscriptionState().getSubscriptionStatus().getPushToken()
Answer: If you are using the react native SDK you can retrieve it from the device state:
const pushToken = (await OneSignal.getDeviceState()).pushToken

Answer: For Flutter use this code:
OneSignal.shared.getDeviceState().then((deviceState) {
     final pushToken = deviceState.pushToken;
}

Comment: This is depreceated
Answer: For React Native getDeviceState is no longer a function on the latest OneSignal v5.0.3. Here is how you get the subscription ID and subscription token for the user after they have accepted permission:
const subId = OneSignal.User.pushSubscription.getPushSubscriptionId();
const subToken = OneSignal.User.pushSubscription.getPushSubscriptionToken();


Iterating over specific items of a pair (or tuple) in a list in C++

Question: Say I have something like:
using MyList = std::list<std::pair<string,int>>;

How can I get an iterator to just the strings (or ints) of the list? I have a function I'd like to pass something.begin() and something.end() that is just of the ints. Is there an easy/clever/slick way to do this? Can it be done with a tuple as well?
Comment: I'm not sure if there is a standard way to do this but you can write your own iterator adapter.
Answer: There is Boost's transform_iterator.
using MyList = std::list<std::pair<string,int>>;

MyList l{ {"Hello", 42}, {"World", 0} };

auto tr = [](auto const& p){
    return p.first;
};

copy(boost::make_transform_iterator(l.begin(), tr),
     boost::make_transform_iterator(l.end(), tr),
     ostream_iterator<string>(cout, ", "));

live example
Answer: With range-v3, you may do something like
std::list<std::pair<std::string, int>> pairs = {{"John", 2}, {"Hello", 42}};

auto names = pairs | ranges::view::keys;
auto ints = pairs | ranges::view::values;
auto doubleInts = pairs | ranges::view::transform([](const auto& p){ return 2 * p.second; };

Comment: @krzaq: They are lightweight views (as `doubleInts`).
Comment: What are `names` and `ints`? Are they lightweight views or full sequences?


Como excluir itens do HTML usando JavaScript

Question: Estou fazendo um exercício em que tenho que criar uma lista de itens usando HTML e uma função para excluir itens da lista.
A função deve usar o input do tipo submit para excluir o item inteiro do HTML sem excluir outro item da lista.
Criei à lista e a função, mas não está funcionando. Podem me ajudar? Obrigado.

    <input type="submit" value="EXCLUIR" />

ITEM QUE DEVE SER EXCLUIDO

    <input type="submit" value="EXCLUIR" />

ITEM QUE DEVE SER EXCLUIDO

Answer: Alguns problemas no teu código:

usa botões, é mais semântico e para além disso o evento submit tem de ser interrompido para evitar recarregar a página
const produto = document.querySelector(".produto"); carrega 1 só produto, tens vários... terias de usar querySelectorAll e iterar para amarrar um auscultador de eventos
produto.innerHTML = '' não remove o produto, só o esvazia de conteúdo

Sugestão:

usa botões com `onclick``
procura o .produto desse botão com .closest
usa .remove() que é feito para remover elementos do DOM

//Função para excluir um item da lista
const excluirProduto = btn => btn.closest('.produto').remove();

    <button type="button" value="EXCLUIR" onclick="excluirProduto(this)" />

ITEM QUE DEVE SER EXCLUIDO

    <button type="button" value="EXCLUIR" onclick="excluirProduto(this)" />

ITEM QUE DEVE SER EXCLUIDO

Comment: Gostei de mais! Obrigado.


Trivial Example of a Module that Creates a Content Type

Question: I want to write a module that creates a content type with a title and one or more textfields. I'm having a very hard time simplifying the roles of each hook function call in the documentation of the node_examples module.
Right now I'm trying to figure out what the next function should be and what it will do with the form data. So far I've gotten the content type and the forms to show up in the add content section of my site but when I save the "votable" content none of the form data is stored, just a blank piece of content with content type "votable". Could someone point me towards the next step?
<code>function votable_form($node, &$form_state) {
    $type = node_type_get_type($node);
    $form = array();
    $form['votable_title'] = array(
        '#type' => 'textfield',
        '#title' => check_plain($type->title_label),
        '#default_value' => !empty($node->title) ? $node->title : '',
        '#required' => TRUE,
        '#weight' => -5,
    );

    $form['votable_results'] = array(
        '#type' => 'textarea',
        '#title' => t('Results'),
    );

    return $form;
}

 function votable_node_info() {
    return array(
        'votable' => array(
            'name' => t('Contestant'),
            'base' => 'votable',
            'module' => 'votable',
            'description' => t("Select this content type to create a Contestent Page"),
            'help' => 'add content',
            'title_label' => t('Contestant Name'),
            'has_body' => FALSE,
        ),
    );
 }
</code>
Answer: Flex Slider creates a content type for a slider image and a view to make the slider itself, so you could look at that for an example module.


Windows updates, Anti-virus updates for Air gapped hosts

Question: I just took over as an admin for a network that uses corporate imaged hosts, to manage various special purpose machines.
These hosts directly connect to the equipment they are managing via Serial or ethernet(cross-ver), with absolutely no internet or intranet access.
A few of these hosts have not been connected to any network for ~2 years, since this is a standard image, windows updates or anti-virus updates have not occurred for over ~2 years.
The vulnerability I'm worried about, is the operators of the special purpose machines connect USB thumb drives to these air gapped hosts, for legitimate reasons as well as personal reasons (music etc.)
I would like to remediate this by one of the following options:
1- Connect these hosts to the corporate LAN to update anti-virus, windows on a periodic basis (once a month), and return to air gap
2- Create a special subnet[s], that only allows through windows update, anti-virus update
another things I'm considering is building custom image for these hosts that does not have unncessary apps (MS Offic etc.)
option 1 is cumbersome and easily forgotten, option 2 requires I go through IT Governance which would take a while to get through.
I would like to hear any recommendations you have, for this situation.
Comment: http://www.wsusoffline.net/
Comment: very interesting software, I'm going to look into this.
Answer: If people are using their own usb drives on these PCs it certainly is an issue.
I would set them up on an isolated LAN along with a WSUS server and whatever software you antivirus provides for distributing patches.  The new server could either have a second connection to the internet, or stay isolated and have you manually transfer updates to it on a regular basis to distribute to the rest.
Comment: to provide further clarification, none of the systems are related, and do not have the same requirements so it is difficult to put them all on a single LAN they range from a host controlling a specialized telemetry device, to a host controlling a large digital sign board, a host controller a laminating machine etc. I like your idea of using a dedicated WSUS server, however because of the different systems I hesitate to put them on the same LAN.
Answer: option 1 is cumbersome and easily forgotten, option 2 requires I go through IT Governance which would take a while to get through.
Option 1: IT management requires effort. If you go with option 1 then it's your responsibility to put in the effort to remember to do this. Create a calendar reminder or a recurring task for your self for this.
Option 2: Whatever you do you should make sure that you have the approval and buy in from the IT staff/management.
You can, as Michael points out in his comment, use an offline WSUS solution. You should also be able to download AV updates offline and apply them to these machines.


HTTPS connection fails on API 19 if server doesn't support SSL

Question: I have an application that I want to use on the emulator for testing purposes. I'm using the emulator/platform/system image in API level 19 with x86 ABI and Google APIs.
The app needs to POST some data to an HTTPS enabled server. The certificate is provided by Let's Encrypt and is signed by the X3 CA.
I have the following error when trying to make the request:
Couldn't execute API call retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall@b0fdfbd8 (Request{method=POST, url=https://hostname.net/api/session, tag=null})
javax.net.ssl.SSLHandshakeException: javax.net.ssl.SSLProtocolException: SSL handshake aborted: ssl=0xb80589b0: Failure in SSL library, usually a protocol error
error:1407742E:SSL routines:SSL23_GET_SERVER_HELLO:tlsv1 alert protocol version (external/openssl/ssl/s23_clnt.c:741 0xa90ed990:0x00000000)
  at: com.android.org.conscrypt.OpenSSLSocketImpl startHandshake (OpenSSLSocketImpl.java:448)
  at: okhttp3.internal.connection.RealConnection connectTls (RealConnection.java:299)
  at: okhttp3.internal.connection.RealConnection establishProtocol (RealConnection.java:268)
  at: okhttp3.internal.connection.RealConnection connect (RealConnection.java:160)
  at: okhttp3.internal.connection.StreamAllocation findConnection (StreamAllocation.java:256)
  at: okhttp3.internal.connection.StreamAllocation findHealthyConnection (StreamAllocation.java:134)
  at: okhttp3.internal.connection.StreamAllocation newStream (StreamAllocation.java:113)
  at: okhttp3.internal.connection.ConnectInterceptor intercept (ConnectInterceptor.java:42)
  at: okhttp3.internal.http.RealInterceptorChain proceed (RealInterceptorChain.java:147)
  at: okhttp3.internal.http.RealInterceptorChain proceed (RealInterceptorChain.java:121)
  at: okhttp3.internal.cache.CacheInterceptor intercept (CacheInterceptor.java:93)
  at: okhttp3.internal.http.RealInterceptorChain proceed (RealInterceptorChain.java:147)
  at: okhttp3.internal.http.RealInterceptorChain proceed (RealInterceptorChain.java:121)
  at: okhttp3.internal.http.BridgeInterceptor intercept (BridgeInterceptor.java:93)
  at: okhttp3.internal.http.RealInterceptorChain proceed (RealInterceptorChain.java:147)
  at: okhttp3.internal.http.RetryAndFollowUpInterceptor intercept (RetryAndFollowUpInterceptor.java:125)
  at: okhttp3.internal.http.RealInterceptorChain proceed (RealInterceptorChain.java:147)
  at: okhttp3.internal.http.RealInterceptorChain proceed (RealInterceptorChain.java:121)
  at: com.redacted.backend.SessionInterceptor intercept (SessionInterceptor.java:61)
  at: okhttp3.internal.http.RealInterceptorChain proceed (RealInterceptorChain.java:147)
  at: okhttp3.internal.http.RealInterceptorChain proceed (RealInterceptorChain.java:121)
  at: okhttp3.RealCall getResponseWithInterceptorChain (RealCall.java:200)
  at: okhttp3.RealCall execute (RealCall.java:77)
  at: retrofit2.OkHttpCall execute (OkHttpCall.java:180)
  at: retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall execute (ExecutorCallAdapterFactory.java:91)
  at: com.redacted.backend.Backend executeApiCall (Backend.java:139)
  at: com.redacted.backend.Backend createSession (Backend.java:62)
  at: com.redacted.viewmodel.LoginActivityViewModel$1 run (LoginActivityViewModel.java:50)
  at: java.util.concurrent.ThreadPoolExecutor runWorker (ThreadPoolExecutor.java:1112)
  at: java.util.concurrent.ThreadPoolExecutor$Worker run (ThreadPoolExecutor.java:587)
  at: java.lang.Thread run (Thread.java:841)

So I figured that the emulator had an outdated CA bundle, so I uploaded a new one generated following these instructions:
This is a custom script but it should be straightforward. $device contains the emulator name, such as emulator-5554.
echo "Download lets encrypt X3 CA cert"
[ -f X3.pem ] && rm X3.pem
wget -O X3.pem https://letsencrypt.org/certs/lets-encrypt-x3-cross-signed.pem.txt || fail "Unable to download LE X3 CA cert"

echo "Encode CA cert to emulator format"
filename="$(openssl x509 -in X3.pem -hash -noout).0"
openssl x509 -in X3.pem >$filename
openssl x509 -in X3.pem -text -fingerprint -noout >>$filename

echo "Push encoded CA cert to $device"
$adb -s $device root || fail "Unable to access emulator '$device' as root"
$adb -s $device remount || fail "Unable to remount RW the emulator '$device'"
$adb -s $device push $filename /system/etc/security/cacerts/ || fail "Unable to push encoded CA cert to '$device'"

The file was indeed pushed, but I still have the same error message in the logs.
Is there a way to update the CA certs on the emulator?
More information:

server uses TLS v1.2
I even added the server certificate (not its CA) in the cacerts folder of the emulator, same result
Comment: have look [this](https://stackoverflow.com/a/30302235/5110595) hope it helps you.
Comment: Actually, TLS versions 1.1 and 1.2 are supported from API 16, but they're not enabled by default until API 20 (see https://developer.android.com/reference/javax/net/ssl/SSLSocket.html). That could be a cause of the issue. If the server wants TLS v1.2 and it's not enabled on the client, the client could very well be falling back to SSLv3 and failing.
Comment: What version(s) of TLS does the server support?
Comment: @clownba0t: great question. It uses TLSv1.2 with `ECDHE-RSA-AES256-GCM-SHA384`. I also noticed that the certificate is issued for `test.domain.net` but I'm using `demo.domain.net` (same IP). I will try to use `test.domain.net` and see if that was the issue.
Comment: @HemantParmar: I'm using KitKat 4.4, the link mentions <4.4. I had already seen this and assumed that a strict <4.4 meant that my emulator didn't have this bug.
Comment: @clownba0t: wow, nice. Indeed the server offers TLS v1.1 and v1.2 but not SSL v3. I will try to force the use of TLS and see if this solves the issue.
Comment: @clownba0t: I have implemented what's described [here](https://github.com/square/okhttp/issues/2372#issuecomment-244807676) and it indeed fixed the issue. I will let you post this as an answer to get all the credit :)
Answer: Support for certain versions of TLS on earlier versions of Android is a bit complicated. TLS v1.0 is supported from API 1, and TLS v1.1 and v1.2 from API 16. However, despite being supported, TLS v1.1 and v1.2 are not enabled by default for client sockets until API 20. See the SSLSocket documentation for details.
What this means is that by default SSL connections from Android devices running APIs 16, 17, 18 and 19 to servers which only support TLS v1.1 or v1.2 will probably fail. The link that Hemant Parmar posted in the comments seems to explain why - the client appears to fall back to SSLv3 in these cases, which the server doesn't support and therefore the handshake fails.
The fix is to specifically enable TLS v1.1 and/or TLS v1.2 support in your application code. There are quite a few articles around which describe various ways of doing this - for example, How to enable TLS 1.2 support in an Android application (running on Android 4.1 JB), and the issue on the okhttp repository that you kindly posted in the comments.


apparmor not working ubuntu 11.04 insize OpenVZ

Question: so it seems I have everything installed, but apparmor_status gives "apparmor module is not loaded." Also some apparmor scripts check for presence of /sys/module/apparmor and it is not there... Any idea of what is wrong?
Oh and it's virtual server using OpenVZ
Answer: Seems like OpenVZ and apparmor can't coexist
Answer: To be more accurate; AppArmor (and SELinux) does not work with OpenVZ: they are incompatible.
AppArmor is designed to protect processes according to security policies within the Kernel. With OpenVZ, processes work within their own Container, separated from the Kernel. Protected by their own Init.
The way they access resources would not work with Apparmor, nor SELinux by design.


Format or Simplify a Long Boolean Expression in Python?

Question: I'm looking for some ideas to keep the following Python function as readable as possible.
The function is from a pytest conftest.py file. It finds filename pairs in a directory and, where cvcorpus_docx and cvcorpus_expected are used as fixtures in a test, will run the test once for each pair.
def pytest_generate_tests(metafunc):
    if 'cvcorpus_docx' in metafunc.fixturenames and 'cvcorpus_expected' in metafunc.fixturenames:
        actual_expected = cv_testcorpus_actual_expected()
        metafunc.parametrize("cvcorpus_docx,cvcorpus_expected", actual_expected)

I can see a number of ways you might go according to the Python style guide (PEP8). I am working with a capable C# developer who is not experienced in Python, and we're working on a small internal API. I want him to be able to read code easily and quickly.
Would the Pythonistas among us leave the code as above, and keep the line at 98 chars long? Or use a backslash to break the if across lines:
def pytest_generate_tests2a(metafunc):
    if 'cvcorpus_docx' in metafunc.fixturenames and \
          'cvcorpus_expected' in metafunc.fixturenames:
        actual_expected = cv_testcorpus_actual_expected()
        metafunc.parametrize("cvcorpus_docx,cvcorpus_expected", actual_expected)

Or would you bracket the big boolean expression so you don't need a backslash?
def pytest_generate_tests2b(metafunc):
    if ('cvcorpus_docx' in metafunc.fixturenames and
          'cvcorpus_expected' in metafunc.fixturenames):
        actual_expected = cv_testcorpus_actual_expected()
        metafunc.parametrize("cvcorpus_docx,cvcorpus_expected", actual_expected)

Do 2a and 2b get confusing around indentation?
or would you go for a more functional approach and avoid the long line that way:
def pytest_generate_tests3(metafunc):
    ae_pair = ('cvcorpus_docx', 'cvcorpus_expected')
    if all(ae in metafunc.fixturenames for ae in ae_pair):
        actual_expected = cv_testcorpus_actual_expected()
        metafunc.parametrize(','.join(ae_pair), actual_expected)

In some ways this looks the tidiest of the lot, but I'm not sure it's the most readable.
I've been looking at this code for a while now, and wondering which to use (or
whether there's a much better idea I haven't seen.) I'm genuinely interested in how you would reason about this situation.
Comment: I would stop fiddling about and just format it using [black](https://pypi.org/project/black/). While I might not always agree with it, I end up with much more consistent formatting between projects.
Comment: `black` doesn't cut it because this is not only about indentation. Truly I don't want to spend much more time on this, but I am not interested in right or wrong so much as the Why. Thanks for your response.
Answer: Here's what I decided to do in the end, using the Clean Code principle of "telling the story" in code. Note that it breaks the DRY (don't repeat yourself) rule, but for a good cause. I think this is more readable than my other examples.
def pytest_generate_tests(metafunc):
    docx_fixture = 'cvcorpus_docx' in metafunc.fixturenames
    expected_fixture = 'cvcorpus_expected' in metafunc.fixturenames
    if docx_fixture and expected_fixture:
        actual_expected = cv_testcorpus_actual_expected_pairs()
        metafunc.parametrize("cvcorpus_docx,cvcorpus_expected", actual_expected)

Comment: This is clean and simple. For more boolean expressions I would consider using `all` and `map`: `if (all(map(lambda x: x in metafunc.fixturenames, ['cvcorpus_docx', 'cvcorpus_expected'])))`. This one can easily be formatted.


C++ WinAPI 'AnimateWindow' has not been declared

Question: I am trying to make a WinAPI C++ program however I am running into the following errors:
<code>error: 'AW_SHOW' was not declared in this scope
error: 'AW_HIDE' was not declared in this scope
error: 'AW_BLEND' was not declared in this scope
error: 'AnimateWindow' was not declared in this scope
error: 'SW_MINIMISE' was not declared in this scope
</code>
I have tried using ::AnimateWindow however I recieve the same error.
This is my header file, included before windows.h
<code>#ifdef WINVER
#undef WINVER
#endif

#ifdef _WIN32_WINNT
#undef _WIN32_WINNT
#endif

#define WINVER 0x0500
#define _WIN32_WINNT 0x0500
</code>
I have also tried setting the vars to 0x0502, and commenting out the preprocessor in Winuser.h for AnimateWindow.
Comment: @Shane: Make sure to answer your own question so this question can be closed as solved.
Comment: Also, what version of the Windows SDK are you using?
Comment: Are you including WinUser.h directly? Don't do that. Include Windows.h.
Comment: Roger, no I am including windows.h directly. Thanks for your prompt response.
Comment: Is the project defining `NOGDI` or `WIN32_LEAN_AND_MEAN`?  You need to check the makefile as well as source files, since macros can be set on the command-line.
Comment: Thanks guys, have found issue (see question). Many thanks for your responses!
Answer: have figured it out.
The file in which the error was occurring was being included in my main.cpp, however it was included after windows.h in main.cpp.
Comment: I will, however I must wait 12 hours.


Wrapping a recursive function to count the number of function calls

Question: Say I have a recursive function that I want to know how many times the function has called itself per input value. Rather than putting printf expressions or changing the return type to include the number of calls, is it possible to "wrap" the function with another to achive this? I would like the wrapped function to return the number of function calls and the original functions result. It should be reusable across different functions.
Here is what I have and it doesn't work.
open System
open System.IO
open System.Collections.Generic

/// example recursive function
let rec getfilenames dir =
    seq {
        yield Directory.GetFiles dir
        for x in Directory.GetDirectories dir do yield! getfilenames x}

/// function to count the number of calls a recursive function makes to itself
let wrapped (f: 'a -> 'b) =
    let d = new Dictionary<'a, int>()

    fun x ->
        let ok, res = d.TryGetValue(x)
        if ok then d.[x] <- d.[x] + 1
        else
            d.Add(x, 1)
        d, f x

> let f = wrapped getfilenames
let calls, res = f "c:\\temp";;

val f : (string -> Dictionary<string,int> * seq<string []>)
val res : seq<string []>
val calls : Dictionary<string,int> = dict [("c:\temp", 1)]

Answer: This is not going to work, because getfilenames is defined as calling getfilenames, not any other function and especially not a function defined after that. So, as soon as your wrapper calls the function, the function will ignore your wrapper and start calling itself.
What you need to do is move the recursion out of the getfilenames function and into another function, by providing the function to be called recursively as a parameter.
let body recfun dir =
    seq {
        yield Directory.GetFiles dir
        for x in Directory.GetDirectories dir do yield! recfun x}

let rec getfilenames dir = body getfilenames dir

Now, you can wrap body before plugging it into a recursive function:
let wrap f =
   let d = (* ... *) in
   d, fun recfun x ->
       let ok, res = d.TryGetValue(x)
       if ok then d.[x] <- d.[x] + 1
       else d.Add(x, 1)
       f recfun x

let calls, counted_body = wrap body

let getfilenames dir = counted_body getfilenames dir

Note that the wrap function returns both the wrapped function (with a signature identical to the original function) and the dictionary, for external access. The number of calls will then be found in calls.
Comment: I'm stuck on 2 points, commenting out the dictionary, what types should go there now? And in wrap function the last line f recfun x I don't understand how this works :(
Comment: Dictionary: the same as before (the only difference is that now the type of f is `('a -> 'b) -> 'a -> 'b` because it expects `recfun`). `recfun` represents the function that should be called by `f` on the subdirectories (because `f` is not recursive anymore). This lets you use the counted function to work on the subdirectories
Comment: let calls, counted_body = wrap body;;

error FS0030: Value restriction. The value 'counted_body' has been inferred to have generic type
    val counted_body : ((string -> '_a) -> string -> seq) when '_a :> seq
Comment: Sounds like F# does not deduce that types `'b` are the same in `('a -> 'b) -> 'a -> 'b` (merely that the first may convert to the second). You will have to annotate `f` as `('a -> 'b) -> 'a -> 'b` and `recfun` as `'a -> 'b` manually.
Comment: This technique is known as "untying the recursive knot".
Answer: As Victor points out, you cannot take a recursive function and "inject" some behavior into the place where the recursive call happens (because the function is already complete). You'll need to provide some extension point for that. In Victor's solution, this is done by taking a function to be called recursively as an argument, which is the most general solution.
A simpler option is to use F# value recursion which allows you to create a function value and use it in its declaration. You can use this to create a recursive function by calling another function that adds some behavior to the function (e.g. counting):
let rec factorial = counted (fun x ->
  if x = 0 then 1
  else x * (factorial (x - 1)) )

factorial 10

Inside the lambda function, we can directly access the function we're defining, so there is no need for passing function to be called recursively as additional parameter. The function counted simply wraps the given function f and adds some functionality:
let counted f =
  let count = ref 0
  (fun x ->
    count := !count + 1;
    printfn "call: %d" (!count)
    f x)

Thanks to the value recursion, the functionality will be added to the factorial function (and so when it calls itself, it will call the version with added counting support).
Comment: This is simpler, and easier to understand, but Victors code is what I was trying to achieve.


Is this proof that the vectors are colinear correct?

Question: I was solving the following exercise: "Let $x,y \in \mathbb{R}^n$ be nonzero such that if $z$ is orthogonal to $x$ then $z$ is orthogonal to $y$. Prove that $x$ and $y$ are colinear".
My idea was: since $x$ is nonzero, we know that we can write $y =\alpha x + z$ where $z$ is orthogonal to $x$. But, by hypothesis, being orthogonal to $x$ implies being orthogonal to $y$, so that taking the inner product on both sides with $z$ we have:
$$\left\langle y,z \right\rangle=\alpha\left\langle x,z \right\rangle + \left\langle z,z \right\rangle = 0,$$
but since $\left\langle x,z\right\rangle = 0$ this implies $|z|^2 = 0$ so that by properties of norm, we must have $z = 0$ proving that $y = \alpha x$.
Is this proof ok? I felt it was too easy to prove it and the answer in the book is a little different, so I thought there might be some error in this thought.
Thanks very much in advance!
Comment: It's a book by the Brazilian mathematician Elon Lages Lima called Análise Real Volume 2. It's about analysis over $\mathbb{R}^n$. I'm afraid there's not a version in english though. But he does this proof on the beginning of the book. He takes $\alpha = \left\langle x,y \right\rangle / \left\langle x,x\right\rangle$ and then he let $z = y - \alpha x$.
Comment: What book are you looking at? I haven't seen it stated as a theorem before that $$y = \alpha x + z \text{ for } z \perp x,$$ but I see why it is true, and I like that way of putting it. Kind of like a Euclidean algorithm for vectors!
Answer: Your proof looks good to me!
Incidentally, an easier way might be to note that $$\langle x \rangle ^{\perp} = \langle y \rangle ^{\perp} \Rightarrow \left( \langle x \rangle ^{\perp} \right)^\perp = \langle x \rangle = \left( \langle y \rangle ^{\perp} \right)^\perp =  \langle y \rangle, $$ where $\langle x \rangle$ is the subspace generated by $x$.  But some of these things might not have been proven in your book yet...


How to print the prices node.tpl.php?

Question: I installed the Commerce Price Savings Formatter module. Can you explain me how to display the price in a file node--scs.tpl.php? it is for a newsletter.
When I put
 <? php print render($content[ 'product: commerce_price']); ?>

It displays the full price with the original price, percent discount and the reduced price.
I want to display each item in the "td" separate.
I tried
<? php print render($content['product: commerce_price: Trade-price-savings-formatter-savings']); ?>

But it does not work.
thank you in advance
Answer: Calling render($content[...]); renders the field—but you need to change how the field is being rendered. To do this, you'll need to modify it's display widget.
Go to Admin→Store→Configuration→Product types→Product type→Manage Display. Then, on the price field, change the the formatter (the first arrow):

This can be further configured by clicking on the gear icon (right arrow) to get access to more configuration.
If this still does not suffice, try these:

Add a HOOK_commerce_price_savings_formatter_formatter to your theme to override the theme provided by the module.
Create a new formatter (the price saving formatter modules can be used as an example) and then use that formatter to display your prices.
Comment: thank you for your quick response, but I'm not expert :) I want to create a newletters with "simplenews" and "simplenews SCS" So I create my template with the inline style.
Here are my files: template.php http://pastebin.com/Qxaz6QHZ simplenews-newsletter-body.tpl.php http://pastebin.com/5uv2rH1B
node - scs.tpl.php http://pastebin.com/SkrCsMsW

see line 31 of node - scs.tpl.php
Comment: https://www.drupal.org/project/commerce_price_savings_formatter now displays the price and promotion on a line, I am able to display separately by adding two additional td line 31. A first td with the original price, a dexième td with the calculated price and a third td with the discount percentage to be customized each cell.

How to print the prices node.tpl.php?


Drupal implementing an advertising takeover with page background image

Question: I need to implement a background image advertising takeover that surrounds the page content in drupal. It's similar to the ones on ign (screenshot attached below).
The only ways I can think of seem really hacky, such as using a view block to display one result of a custom content type containing the image and showing the view in a custom region that spans the background of the page.
Can anyone think of a better way? It needs to be easy to set up in the CMS for an administrator and they need to be able to create different takeovers for different sections of the site.
Thanks
Answer: In your theme's preprocess_page() hook you could set a variable that is accessible in your page.tpl.php file.  Basically something like:
function YOURMODULE_preprocess_page(&$vars) {

  $vars['background_image']=make_me_a_background_for_page($_GET['q']);

  // other page preprocessing goes here

}

where make_me_a_background_for_page() is a custom function that returns a path to an image based upon the page being viewed.  You can then use that variable however you want to create the background.
One way would be to put a quick div in the page somewhere like...
<?php if (!empty($background_image)): ?>

<?php print $background_image; ?>

<?php endif; ?>

and have it hidden in css:
#use-this-background-image {
  display: none;
}

and then use jQuery to set the body's background to it:
// Drupal Behaviors Wrapper here :)

  if ($('#use-this-background-image').length) {
    $('body').css('background-image','url("' + $('#use-this-background-image').text() + '")');
  }

// Drupal Behaviors Wrapper here :)

An added bonus of doing it in this somewhat convoluted way is the background image isn't loaded until after your page is, so it pops in after the content, which, to me at least, is nicer looking than watching a big background load and THEN the content.
Answer: Do you want a pure Drupal  / module based solution? If not, then I have done this most effectively with a bit of jQuery.  When someone clicks to go to a certain page, the javascript would check to see if this matches the url in the script. If so, it dynamically changes the CSS as needed (background images, logo / header images, etc).
This is also most effective because you can have arrays of themes. How about multiple games each with its own branding? Very easily accomplished with this solution.
If that is a route you would like to take, let me know and I will provide some links and resources if you are not very familiar with jQuery.
Answer: very very easy PHP based solution.
Perhaps even easier than the jQuery would be this:
Lets say you have multiple style.css files - streetfighter.css; kidicarus.css; etc
In your page.tpl.php file (or maybe even in your html.tpl.php file) you add a PHP switch statement just below where the default drupal print $scripts php loads.
Because of the power of cascading style sheets, you can override any of the default style sheets before it to create whatever theme you like. Use php to check the URL then load the stylesheet as needed:
switch($url)
    case (url.com/streetfighter)
        print path/to/streetfighter.css;
    case (url.com/kidicarus)
        print path/to/kidicarus.css;
etc

now your final css overrides everything from background photos to link colors or colorscheme. Endless possibilities.


How to fix "Gradle build failed to produce an Android bundle package." in Flutter

Question: On running
flutter build appbundle I am getting the following error:
Gradle build failed to produce an Android bundle package.
When running in verbose mode this is what I additionally get:
#0      throwToolExit (package:flutter_tools/src/base/common.dart:24:3)
#1      _buildGradleProjectV2 (package:flutter_tools/src/android/gradle.dart:585:7)
<asynchronous suspension>
#2      buildGradleProject (package:flutter_tools/src/android/gradle.dart:331:14)
<asynchronous suspension>
#3      buildAppBundle (package:flutter_tools/src/android/app_bundle.dart:43:10)
<asynchronous suspension>
#4      BuildAppBundleCommand.runCommand (package:flutter_tools/src/commands/build_appbundle.dart:43:11)
<asynchronous suspension>
#5      FlutterCommand.verifyThenRunCommand (package:flutter_tools/src/runner/flutter_command.dart:545:18)
#6      _asyncThenWrapperHelper.<anonymous closure> (dart:async/runtime/libasync_patch.dart:77:64)
#7      _rootRunUnary (dart:async/zone.dart:1132:38)
#8      _CustomZone.runUnary (dart:async/zone.dart:1029:19)
#9      _FutureListener.handleValue (dart:async/future_impl.dart:126:18)
#10     Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:639:45)
#11     Future._propagateToListeners (dart:async/future_impl.dart:668:32)
#12     Future._complete (dart:async/future_impl.dart:473:7)
#13     _SyncCompleter.complete (dart:async/future_impl.dart:51:12)
#14     _AsyncAwaitCompleter.complete.<anonymous closure> (dart:async/runtime/libasync_patch.dart:33:20)
#15     _rootRun (dart:async/zone.dart:1124:13)
#16     _CustomZone.run (dart:async/zone.dart:1021:19)
#17     _CustomZone.bindCallback.<anonymous closure> (dart:async/zone.dart:947:23)
#18     _microtaskLoop (dart:async/schedule_microtask.dart:41:21)
#19     _startMicrotaskLoop (dart:async/schedule_microtask.dart:50:5)
#20     _runPendingImmediateCallback (dart:isolate/runtime/libisolate_patch.dart:115:13)
#21     _RawReceivePortImpl._handleMessage (dart:isolate/runtime/libisolate_patch.dart:172:5)

I am on Flutter stable channel v1.2.1.
I can build an apk without a problem though.
I can also build an appbundle from another Flutter project.
Based on the error messages above how would you start to find the issue?
What might be the problem here?
Answer: I have similar issue with the same error messages.
(Build Release APK successfully but failed to build app bundle)
The solution that works for me is to upgrade the Android Gradle Version.
Below are the config that works for me (You may upgrade to the latest version also):

/android/gradle/wrapper/gradle-wrapper.properties:

distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.2-all.zip

/android/build.gradle:
buildscript {
    repositories {
       google()
       jcenter()
      }

    dependencies {
       classpath 'com.android.tools.build:gradle:3.2.1'
    }
}

Comment: If anyone else runs into this and this solution did not solve the problem, check if you are using the android.tools.build:gradle with version 3.5.0. There is currently an issue with that version, see [this flutter issue](https://github.com/flutter/flutter/issues/38934). Temporarily downgrading to 3.4.2 solved the issue for me.
Comment: Thanks a lot! I had to also remove references to kotlin in my gradle files (three different places)
Comment: Before further investigating the issue, i think there is one trick you can try is to create a new flutter project with the latest Android SDK, Android Gradle Plugin [link](https://developer.android.com/studio/releases/gradle-plugin) and Flutter Version to make sure your environment is able to build a app bundle first. Then you can copy the dart files in '/lib' to the new project to build app bundle from now on, or try to diff the gradle setting between the new project and your existing project to see whats the problem. (In case you are really having the exact same error message in this post)
Answer: I commented list of APIs in app.gradle and then it works:
android {
    splits {

        abi {

            //include "x86", "x86_64", "armeabi", "armeabi-v7a", "arm64-v8a"

        }
    }
  }

Comment: but don't you need that line so that the app works on 32bit and 64bit devices??
Comment: @F-1 True, This line doesn't make any problem if you trying to build bundle apk..., It happen if you trying to build a normal release apk. So you have to leave it when you need the advantage of it when using bundle !...
Comment: I figured it out, you can't build an app bundle in Flutter you have to do it in Android Studio for now.
Answer: There is a multitude of reasons for which you might encounter a frustrating problem like Gradle build failed to produce an Android bundle package.
As of @Tam Tam Tam's answer, first try to update your gradle and android gradle plugin to a latest version.
If that does not fix your problem, then try to disable all the different architectures and set universalApk to false in your android/app/build.grade in splits section. Put something like this:
splits {
        abi {
            enable true
            reset()
            // include 'x86', 'armeabi-v7a', 'x86_64'
            universalApk false
        }
    }

This suggestion was provided here:
https://github.com/flutter/flutter/issues/27462#issuecomment-507229131
It happens because gradle generates the apk following a convention, eg, app-<architecture-name>.apk, which is not understandable by flutter, it expects apk names in another format, eg, simply app.apk. Disabling all different architectures name bring them to a common understanding :)
If that is also not work, then try to remember if you change your package name com.example.packagename to something your own personal like com.mywebsite.packagename. If you have done it, then you probably changed it everywhere needed, for example, in build.gradles and AndroidManifest.xml but you might forgot to change it in your java (or kotlin class).
To make your new package name works, you also have to change the package name in src/main/java/com/mywebsite/packagename/MainActivity.java (in case of kotlin, it's src/main/kotlin/com/mywebsite/packagename/MainActivity.kt)
I hope this helps.
Answer: Here is my senario:
I'm using a remote machine from CI to sign apk with release sign config.
So I removed buildTypes.release.signConfig in build.gradle like this:
    buildTypes {
        release {
            // signingConfig signingConfigs.release
            minifyEnabled true
            useProguard true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

This makes the name of the output apk change to app-release-unsigned.apk but flutter expects app-release.apk, which leads to Gradle build failed to produce an Android bundle package.
After adding signingConfig signingConfigs.release back the problem was solved.
Answer: In my case, I added flavor to my project and forgot to add flavor name to Run/Debug Configuration.
Comment: Wasted 2 hours debugging this, and if you hadn't written this I would have wasted two more.  So silly of me to forget, but so easy to get tunnel vision thinking the problem is something bigger than it really is.  Appreciate you noting this to jog my memory.
Answer: I lost some part of the config of the default project.gradle
rootProject.buildDir = '../build'
subprojects {
    project.buildDir = "${rootProject.buildDir}/${project.name}"
}
subprojects {
    project.evaluationDependsOn(':app')
}

Answer: For me, migrating to AndroidX fixed everything:
https://flutter.dev/docs/development/packages-and-plugins/androidx-compatibility
Answer: For me, I had signingConfig signingConfigs.debug line missing in my release config of app's build.gradle file. But error I was getting is the same "Gradle build failed to produce an Android bundle package".
android {
    ...
    buildTypes {
        release {
            signingConfig signingConfigs.debug
            ...
        }
    }
}

Answer: For me changing my gradle version as following line solved the issue. (I was using a higher version)
android/build.gradle
  dependencies {
     classpath 'com.android.tools.build:gradle:3.3.0'
  }


If $ T(f)=\int_0^12xf(x)\,dx$ find the value of $||T||$

Question:
Let , $T:(C[0,1],||.||_{\infty})\to \mathbb R  $ be defined by $\displaystyle T(f)=\int_0^12xf(x)\,dx$ for all $f\in C[0,1]$. Then find $||T||$, where , $\displaystyle ||f||_{\infty}=\sup_{0\le x\le 1}|f(x)|$.

We have , $\displaystyle |T(f)|\le 2\int_0^1x\,dx=1$. Then , $||T||\le 1$
.  I am unable to find the converse inequality...
Comment: You have $Tf = \int_0^1 f(x)dx^{2}$, which is integration with respect to a positive measure. The norm of $\|T\|$ is the variation of the measure (this works for signed and even complex Borel measures,) and the variation of a positive measure is the total measure $\int_0^1 dx^{2}=1$. So the solution posted below is general: use $T(1)$ to obtain the norm whenever the representing measure is positive.
Answer: Take the constant function $f(x)=1$ to show that $||T||=1$.
Comment: Why take a particular function ?
Comment: @Panja717 : because it works. If for a function $f$, you have $\frac{\|Tf\|}{\|f\|} = 1$, then obviously $\| T\| \geq 1$
Comment: @uniquesolution  $|T(f)|\le 2\int_0^1|xf(x)|\,dx \le 2 \int_0^1|f(x)|\,dx \le 2\|f\|_{\infty}\int_0^1 dx$
Comment: from this, I can  say that $\|T\| \le 2$
Comment: I don't know , how did user181525 prove $\|T\| \le 1$?


I having troubles with aggregate, but it should work

Question: I have the following data frame called "test" that contains gene names and variances:
genes     variance
RERE      0.27742
DLEC1     0.630556
RERE      0.45678
...       ...

I would like to create a new data frame with the max variance per gene:
genes     variance
RERE      0.45678
DLEC1     0.630556
...       ...

I tried:
aggregate(test$variance, by = list(test$genes), max)

but get this error:
Error in Summary.factor(13308L, na.rm = FALSE): 'max' not meaningful for factors

I would greatly appreciate any suggestions. Thank you!
Comment: Seems as if `test$variance` is of type factor, not of type numeric. Check with `str(test)`. In that case, use `test$variance <- as.numeric(as.character(test$variance))` before aggregate.
Comment: Check your original data that you are importing. I'm guessing you have a character string somewhere in your `variance` column, like `"missing"` or something that is causing it to be made a factor.
Answer: As indicated in the comments, it seems like your "variance" column, which you were expecting to be numeric, is actually a factor.
You can convert it in the aggregate command itself, but it is probably a smarter choice to figure out why this conversion happened in the first place.
aggregate(test$variance, by = list(test$genes), max)
# Error in Summary.factor(3L, na.rm = FALSE) :
# 'max' not meaningful for factors
aggregate(as.numeric(as.character(test$variance)),
          by = list(test$genes), max)
#   Group.1        x
# 1   DLEC1 0.630556
# 2    RERE 0.456780

Sample data
test <- structure(
  list(genes = c("RERE", "DLEC1", "RERE"),
       variance = structure(
         c(1L, 3L, 2L), .Label = c("0.27742", "0.45678", "0.630556"),
         class = "factor")), .Names = c("genes", "variance"),
  row.names = c(NA, -3L), class = "data.frame")


How to add to List Generic on each button clicks

Question: I have a button that gets items from controls and add to a list.
As shown
This is my ojects thats holds the values.
public class SelectedPurchaseItems
{
    public int ItemId { get; set; }
    public string ItemName { get; set; }
    public double PurchasePrice { get; set; }
    public int Quantity { get; set; }
    public string UnitOfMeasure { get; set; }
    public double Total { get; set; }
}

This is my mainwindow class that does the adding to the list
public partial class MainWindow : Window
{
    public List<SelectedPurchaseItems> SelectedList;

    private void btnSaveModalSelectItem_Click(object sender, RoutedEventArgs e)
    {
            SelectedList = new List<SelectedPurchaseItems>();
            SelectedPurchaseItems _value = new SelectedPurchaseItems()
            {
                ItemId = Convert.ToInt32(comboboxSelectItemItem.SelectedValue),
                ItemName = comboboxSelectItemItem.Text,
                PurchasePrice = _purchasePrice,
                Quantity = _quantity,
                UnitOfMeasure = comboboxSelectItemUnitofMeasure.Text,
                Total = _total
            };
            SelectedList.Add(_value);
            DataGridSelectedPurchaseItems.ItemsSource = SelectedList;
    }
}

My challenge now is, any time it adds an item to the List, it always reinitialize the list, which makes the previous item added to clear off.
But my aim is for the List to hold each item that has been added to it, and i don't know how to go about that.
I'm also Binding the list to a WPF datagrid. So after showing the first added item, it won't display any further added item.
Comment: You have to remove this line `SelectedList = new List();` from the method, it always reinitialize the list.
Answer: SelectedList = new List<SelectedPurchaseItems>();

is the line that reinitializes your list. You should just delete it, and move initial list initialization to a constructor or somwhere else
Answer: Dont re-instantiate it in the button click. Simply initialise it once as shown:
public partial class MainWindow : Window
{
    public List<SelectedPurchaseItems> SelectedList = new List<SelectedPurchaseItems>();
    public MainWindow()
    {
        DataGridSelectedPurchaseItems.ItemsSource = SelectedList;
    }

    private void btnSaveModalSelectItem_Click(object sender, RoutedEventArgs e)
    {
        SelectedPurchaseItems _value = new SelectedPurchaseItems()
        {
            ItemId = Convert.ToInt32(comboboxSelectItemItem.SelectedValue),
            ItemName = comboboxSelectItemItem.Text,
            PurchasePrice = _purchasePrice,
            Quantity = _quantity,
            UnitOfMeasure = comboboxSelectItemUnitofMeasure.Text,
            Total = _total
        };
        SelectedList.Add(_value);
    }
}

Comment: I have done this `public List SelectedList = new List();

private void btnSaveModalSelectItem_Click(object sender, RoutedEventArgs e)
{
         ...`  and still doesn't work.
Comment: @MIchealDavid - Did you also delete the `SelectedList = new List();` line in the click method?
Comment: Yeah, and i'm trying to bind it to a DataGrid on each click `DataGridSelectedPurchaseItems.ItemsSource = SelectedList;`. But after displaying the first added, it won't display any added item again.
Comment: Please [edit] your question to show where you have `DataGridSelectedPurchaseItems.ItemsSource = SelectedList;`, also add Tags to indicate this is WPF.


Performing model validation in stata

Question: I need to validate a cox-regression model internally - i was thinking of either k-fold cross validation or bootstrapping methods. are either of these possible to do with a cox model? any idea how to perform these? an example of our stepwise regression is below:
sw, pr(0.05): stcox age3 apricat2 meldcat2 etiol5a sex if yrs>=0.5
Thanks for your help
Answer: A few scattered comments:
For "cox" read "Cox" throughout: Sir David Cox, whose name you are using, is no relation, but I'm sensitive on that detail.
In principle, certainly you can bootstrap and see how much variability there is. But the bootstrap command you might most naturally turn to expects you to work with named scalar quantities, most notably parameter estimates or figures of merit. So, you need to decide what results you want to focus on. There is no magic command that says "bootstrap this and report on variability".
Your use of stepwise fitting through the sw command is a matter of taste (which I don't share, but that is another story). But in principle doing things stepwise can only complicate your comparisons. Suppose different predictors are included in the final model in different replications; how are you going to handle that?
Cross-validation raises similar issues. In general terms, you would need to write much more code for that. If you are not an experienced Stata programmer, you are best advised to back off.
My reply, like your question, is sketchy and based mostly on general Stata experience.
Answer: There is an example on how to use the bootstrap the check the stability of which variables were selected using stepwise in combination with Cox regression here:
http://www.stata.com/statalist/archive/2011-05/msg01427.html

Performing model validation in stata


Por que o incrementador se "perde" no laço?

Question: Fazendo um simples laço FOR, encontrei um comportamento estranho que não consegui entender.
A contagem se "perde" na requisição AJAX, mantendo o valor do incrementador com o último. A requisição a URL sempre retorna 200 e mesmo assim não influencia em nada para a execução correta.
Há algo relacionado com escopo da variável?

for (i = 0; i < 60; i++) {

  console.log("For:" + i);

  var urlMapas = "http://servicomapas.ibge.gov.br/api/mapas/" + i + '/1';

  $.ajax({
    url: urlMapas,
    dataType: "json",
    success: function(data) {
      console.log("AJAX:" + i);

    }
  });
}

Comment: Veja se isso ajuda: http://pt.stackoverflow.com/questions/1859/como-funcionam-closures-em-javascript
Comment: Eduardo, você verificou a variável dentro do AJAX? A de fora da requisição é contada normalmente mas a de dentro não.
Comment: Eu entendi o que você quer fazer. Se você der um `console.log` na variável `data`, você vai ver que as requisições para cada URL estão sendo retornadas. Se você quiser um loop dentro de `.success()` terá que criar outro for, pois trata-se de uma `callback`.
Comment: Eu testei seu código e a variável é incrementada um a um até chegar no 60. Eu testei com o console do Chrome.
Answer: Ele não se perde, ele faz exatamente o que foi mandado fazer. O laço é uma coisa e a função passada para o jQuery é outra completamente separada. Isto ocorre porque a função é uma closure. Ela captura a variável, mas não seu estado. Então só o estado final é considerado.
Como pode ser observado no console o laço conta de 0 à 60 perfeitamente e o i fica com este valor quando encerra.
A solução é criar uma função e passar a variável do laço como parâmetro para esta função. E esta função é que deve chamar o AJAX.

for (var i = 0; i < 60; i++) {
    console.log("For:" + i);
    (function(p) {
        $.ajax({
            url: "http://servicomapas.ibge.gov.br/api/mapas/" + p + '/1',
            dataType: "json",
            success: function(data) {
                console.log("AJAX:" + p);
            }
        });
    })(i);
}

Coloquei no GitHub para referência futura.
Por isso que eu digo que a closure só deveria ser usada em último caso, quando a pessoa sabe todas as consequências dela. jQuery abusa de closures.
Comment: Entendido, @bigown. O link passado pelo Pablo também deu uma esclarecida.


How to fix XMLHttpRequest error while loading XML feed form a different domain using jQuery?

Question: I need to load an xml feed from vimeo, for this I am using jquery like this:
$(function(){

// Get vimeo feed
$.ajax({
    type: "GET",
    url: "http://vimeo.com/api/v2/<my username>/videos.xml",
    dataType: "xml",
    success: function( xml ) {
        $(xml).find( "video" ).each( function() {
            console.log( $(this).find( "title" ) );
        });
    }
});

});

But I get this error: XMLHttpRequest cannot load http://vimeo.com/api/v2//videos.xml. Origin http://localhost:8888 is not allowed by Access-Control-Allow-Origin.
I am using MAMP if that makes any difference.
Comment: possible duplicate of [Ways to circumvent the same-origin policy](http://stackoverflow.com/questions/3076414/ways-to-circumvent-the-same-origin-policy)
Answer: Please read the vimeo API - User

Making the URL
http://vimeo.com/api/v2/username/request.output
username Either the shortcut URL or ID of the user, an email address will NOT work.
request The data you want. The different request types are listed below.
output Specify the output type. We currently offer JSON, PHP and XML formats.

So instead of making a request like http://vimeo.com/api/v2//videos.xml make a request like http://vimeo.com/api/v2//videos.json
Now you can use $.getJSON to get the results like this.
$(document).ready(function() {
    var url = "http://vimeo.com/api/v2/{username}/videos.json?callback=?";
    $.getJSON(url, function(data) {
        var items = [];
        $.each(data, function(key, datum) {
            items.push("
");
            items.push("
Title: " + datum.title + "
");
            items.push("
Tags: " + datum.tags + "
");
            items.push("
");
        });
        $("#result").html(items.join(""));
    });
});

View Demo: http://jsfiddle.net/naveen/Ssdjp/1/
Comment: thanks a lot for the code snippet works like a charm, I was just reading about the JSON bits


What does $\text{Sym}(\mathfrak{g}[1])$ and $\mathfrak{g}[1])$ mean?

Question: In Costello&Gwilliam's Factorization ALgebras in Quantum Field Theory, the Chevalley--Eilenberg complex of a module $M$ over a Lie algebra $\mathfrak{g}$ with field $\mathbb{K}$ is defined as $\wedge g\otimes_\mathbb{K} M$ but at the same time, it is given by
$$C_*(\mathfrak{g},M)=(Sym(\mathfrak{g}[1])\otimes_K M, d)$$
What  does $\mathfrak{g}[1]$ mean and why is the Sym is there when we are considering the exterior algebra over $\mathfrak{g}$?
Comment: It's a suspension (degree shift).
Comment: @RicardoBuring Thanks! But could you tell me what that might be?
Answer: If $V$ is a space in odd degree, then $\mathsf{Sym}(V)$ is actually exterior, in the sense $v\wedge w = - w\wedge v$ on generators (graded commutativity forces these signs to appear). If $V$ is graded, then $V[1]$ is the graded object with $V[1]_n = V_{n-1}$. So, if $\mathfrak g$ is a plain (non-dg) Lie algebra in degree zero (even), $\mathfrak g[1]$ is concentrated in degree one (as $\mathfrak g[1]_1 = \mathfrak g_0 = \mathfrak g$) and $\mathsf{Sym}(\mathfrak g[1])$ is just the exterior algebra on $\mathfrak g$ (with the catch that an element $v_1\wedge\cdots \wedge v_n$ is in homological degree $n$).


Adding drift to an ARIMA(0,1,1) (0,1,1) model in R

Question: I'm trying to add drift to my ARIMA(0,1,1)(0,1,1) model in R, however R is giving me the error message,

Warning message:
In Arima(insample, order = c(0, 1, 1), seasonal = c(0, 1, 1), include.drift = TRUE) :
No drift term fitted as the order of difference is 2 or more.

I don't understand the intuition behind this, I've tried writing out the ARIMA model using the lag operator:
$(1-\phi_1L)(1-\Phi_1L^4)(1-L)(1-L^4)y_t=\alpha +(1+\theta_1L)(1+\Theta_1L^4)\epsilon_t$
where $\alpha$ is the drift term.
I can't see why adding a drift term to a differenced series would be problematic?
Answer: This is to prevent fitting high-order polynomials for the time series in levels (not differenced), something that is only rarely plausible. According to the function's documentation,

<...> if there is more than one difference taken, no constant is included regardless of the value of this argument. This is deliberate as otherwise quadratic and higher order polynomial trends would be induced.

{Emphasis is mine.)


Azure Automation Variables

Question: I am trying to make a code using PowerShell so that secrets are not hardcoded on my runbook so that it will not be exposed in the script. I created encrypted variables in my automation account. These variables are AppID, AppSecret and TenantID.
This is the part of the script to login automatically to Azure. I didn't use managed identity for some compatibility reasons with the script.
My script is running fine when secrets and IDs are hardcoded but when I created variables it  is not working. Error message is "Run Connect-AzAccount". Below is my code. Need help on how to correct this. Thank you in advance.
$AzVariableApplicationID = 'AppID'
$AzVariableAppSecret = 'AppSecret'
$AzVariableTenantID = 'TenantID'

$AppID = Get-AzAutomationVariable -Name $AzVariableApplicationID
$AppSecret = Get-AzAutomationVariable -Name $AzVariableAppSecret
$TenantID = Get-AzAutomationVariable -Name $AzVariableTenantID

$SecureSecret = $AppSecret | ConvertTo-SecureString -AsPlainText -Force

$Credential = New-Object -TypeName System.Management.Automation.PSCredential `
-ArgumentList $AppID, $SecureSecret

Connect-AzAccount -ServicePrincipal -Credential $Credential -Tenant $TenantID

Comment: Have you created "azure run as account" under automation accounts?
Comment: I didn't create "azure run as account" as it will be retired soon as per microsoft. I came up with the below code and it is working now.

**$AppID = Get-AutomationVariable -Name AppID
$TenantID = Get-AutomationVariable -Name TenantID
$AppSecret = Get-AutomationVariable -Name AppSecret

$SecureSecret = $AppSecret | ConvertTo-SecureString -AsPlainText -Force

$Credential = New-Object -TypeName System.Management.Automation.PSCredential `
-ArgumentList $AppID, $SecureSecret

Connect-AzAccount -ServicePrincipal -Credential $Credential -Tenant $TenantID**
Answer:
As described in PsCustom Object - Hitchikers GUID(e) to Automation
, It is not possible to retrieve values for encrypted variables as they're available within the runbook at runtime via the Get-AutomationVariable cmdlet

I found an alternative approach to "Connect Azure" by using "Certificate-based authentication" inside PowerShell runbook without hardcoding the values:

Created a new Service principal and provided the "Owner" role access to avoid any restrictions.

To authenticate via service principal, I create a new self-signed certificate with the command:

$cert=New-SelfSignedCertificate -Subject "CN=xxxxxCert" -CertStoreLocation "Cert:\CurrentUser\My"  -KeyExportPolicy Exportable -KeySpec Signature

Upload a certificate under Certifications & Secrets:

Click windows + R to open the run box and give certmgr.msc as shown here.

Export a certificate without private key.

Upload a certificate in the below path:
AzureAD -> App registrations -> Serviceprincipal

Now, I have exported the same certificate with key and uploaded inside my automation account to authenticate Service principal connection:

Added an "Azure Service Principal" connection inside automation accounts by providing "ApplicationID, TenantID, Certificate Thumbprint" of my Service principal as shown:

Inside PowerShell runbook, I ran the below script that works for me:

  $connectionName = "serviceprincipalname"
  try
  {
     $servicePrincipalConnection=Get-AutomationConnection -Name $connectionName
    "Logging in to Azure..."
     Add-AzureRMAccount
     -ServicePrincipal `
     -TenantID $servicePrincipalConnection.TenantID `
     -ApplicationID $servicePrincipalConnection.applicationID  `
     -CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint
}
catch {
  if (!$servicePrincipalConnection)
  {
     $ErrorMessage = "Connection $connectionName not found. "
     throw  $ErrorMessage
  }  else{
       Write-Error -Message $_.Exception
       throw  $_.Exception
     }
}

Logged in to Azure:

Register an App in App registrations and create a service principal in AzureAD

Azure Automation Variables