Alternative to deprecated get_io_service() - boost-asio

Asio v 1.11. The doc says that basic_stream_socket::get_io_service() member function is deprecated and get_executor() must be used instead. But the latter returns executor not io_service.
How to obtain reference to the io_service object used by socket to construct another one?

You can use get_executor().context():
socket newSocket(existingSocket.get_executor().context()));

Since ASIO v 1.13 new I/O object can be constructed using just the executor:
socket new_socket{existing_socket.get_executor()};

Since I don't have enough reputation points to comment, I write as an answer:
Using get_executor().context() did not work right away for me, but writing a macro did (see this answer)

Related

Problem with type FreeRV while adding new distribution

I'm trying to add a new discrete distribution to PyMC3 (a Wallenius non-central hypergeometric) by wrapping Agner Fogs c++ version of it (https://www.agner.org/random/).
I have successfully put the relevant functions in a c++ extension and added broadcasting so that it behaves as scipy's distributions. (For now broadcasting is done in Python. .. will later try the xtensor-python bindings for more performant vectorization in c++.)
I'm running into the following problem: when I instantiate an RV of the new distribution in a model context, I'm getting a "TypeError: an integer is required (got type FreeRV)" from where "value" is passed to the logp() function of the new distribution.
I understand that PyMC3 might need to connect RVs to the functions, but I find no way to cast them into something my new functions can work with.
Any hints on how to resolve this or general info on adding new distributions to PyMC3 or the internal workings of distributions would be extremely helpful.
Thanks in advance!
Jan
EDIT: I noticed that FreeRV inherits from theanos TensorVariable, so I tried calling .eval(). This leads to another error along the lines that no input is connected. (I don't have the exact error message right now).
One thing which puzzles me is why logp is called at instantiation of the variable when setting up the model ...

Source of randomness in kotlin-stdlib-common

In kotlin-stdlib-common is there any source of randomness available out of the box? Whether it's some implementation of standard java.util.Random, kotlin.math.random* or basic current time millis that I can use to create my own random number generator? I can't find any.
If it's not there, how would you get the source of randomness without setting your own platform dependent implementations? This is the only method I need:
expect class Rng {
fun nextInt(): Int
}
I'm trying to make it platform agnostic.
The answer would be: wait for Kotlin 1.3 to get released where the common library will be enriched with classes and methods that can provide the source for random values.
https://kotlinlang.org/docs/reference/whatsnew13.html#multiplatform-random
This maybe a post with many links, which may cause the problem of Your answer is in another castle: when is an answer not an answer?, so I try my best to write the link description. And my understanding of Kotlin Multiplatform is Kotlin-Multiplatform = Kotlin-JVM + Kotlin-JS.
I believe that the random number of Kotlin-JVM is provided by java.util.Random, and Math.Random() if it is Kotlin-JS, with these following reasons:
How can I get a random number in Kotlin?, and there is an answer in that question said that Kotlin-JS can use Math.Random() to get random number.
Can't get any result of any random number related method of Kotlin-JVM, but there is a random() in Kotlin-JS.
The source code of Kotlin-JVM related file, while using Random(), there is a import java.util.*, or some file directly use java.util.Random for example kotlin/libraries/stdlib/jvm/src/kotlin/collections/MutableCollectionsJVM.kt#L78.
And, java.util.Random is designed as result platform-independent, and also implementation platform-independent, with these reason:
Is Java's RNG (using seeds) platform-independent?, though this question may be out-dated.
We can't find keyword "native" in the source of JDK8/java.util.Random or the source of JDK10/java.util.Random, and the RNG logic is clear in these source code, where seed is decided by nanoTime() if not provided, and RNG is the implementation of Volume 2, TAOCP.
So, I think,
How would you get the source of randomness without setting your own platform dependent implementations?
Maybe a random-enough seed and a random-enough (P)RNG.

How to connect to postgresql in a multithreaded C application

I have a few threads in my application. Each has its own PGconn* connection that is individually opened with the same connection string. When a thread makes a query, it almost never returns PGRES_TUPLES_OK.
I can provide some code examples if necessary, but does anything stand out here? I have tried using global mutexes as well, to no avail. I am using postgresql 9.3
PQerrorMessage(db) returns: connection pointer is NULL
From the postgresql documentation:
If a null pointer is returned, it should be treated like a PGRES_FATAL_ERROR result. Use PQerrorMessage to get more information about the error.
Ok, I figured it out.
I was using a function to open each connection, and was passing a pointer to a PGconn struct to it.
I needed to pass a double pointer.

Kotlin: Method reference not working?

It seems I'm unable to use a method reference of an object in Kotlin. This feature exists in Java.
For example in Java if I was looping through a string to append each character to a writer:
string.forEach(writer::append);
But in Kotlin using the same syntax does not work because:
For now, Kotlin only supports references to top-level and local functions and members of classes, not individual instances. See the docs here.
So, you can say Writer::append and get a function Writer.(Char) -> Writer, but taking a writer instance and saying writer::append to get a function (Char) -> Writer is not supported at the moment.
Starting from Kotlin 1.1 writer::append is a perfectly valid bound callable reference.
However, you still cannot write string.forEach(writer::append) because Writer#append method returns a Writer instance and forEach expects a function that returns Unit.
I am using Kotlin 1.3 and while referencing a Java method I got a very similar error. As mentioned in this comment, making a lambda and passing it to the forEach method is a good option.
key.forEach { writter.append(it) }
Being it the implicit name of a single parameter.

Is there a difference between chaining EMFEditProperties methods vs. using FeaturePath.fromList?

Is there any difference between
EMFEditProperties.list(
editingDomain,
FeaturePath.fromList(DemoPackage.Literals.TRANSPORT_DETAILS__SELECTED_TRANSPORT,
DemoPackage.Literals.TRANSPORT__DEPARTURES))
and
EMFEditProperties
.value(editingDomain, DemoPackage.Literals.TRANSPORT_DETAILS__SELECTED_TRANSPORT)
.list(DemoPackage.Literals.TRANSPORT__DEPARTURES)
?
From studying the sourcecode of EMFEditProperties, I think there is no difference. In fact, if you use a feature path with more than one element, the implementation uses the chained form internally.