ndarray: How to split an array? - multidimensional-array

Looking for the functionality of the .split_of function as used with a Vec (https://doc.rust-lang.org/std/vec/struct.Vec.html#method.split_off)
Currently I am trying to use the function split_at: (docs: https://docs.rs/ndarray/0.13.1/ndarray/struct.ArrayBase.html#method.split_at)
Usage:
let mut data: Array2<f32> = array![[1.,2.],[3.,4.],[5.,6.],[7.,8.]];
let split = data.split_at(Axis(0),1);
Getting the error:
method not found in `ndarray::ArrayBase<ndarray::data_repr::OwnedRepr<{float}>, ndarray::dimension::dim::Dim<[usize; 2]>>`
What am I missing here?

According to the documentation, these are defined only for ArrayViews and not Arrays.
It's unfortunate that this is stated right above split_at in the documentation, making it easy to miss if you simply click on it from the sidebar of methods.
Methods for read-only array views.
similarly for read-write views.
Initializing a view and splitting it as shown in split_at's documentation should work fine.

Related

Firebase Function Data Type of "data" in onCall function

I thought i had a simple question but it seems to be somewhat harder and the documentation does not help alot.
What exactly is the type of 'data' in "functions.https.onCall((data, context) {});"
I thought it varies between a simple value, a map or a list.
But even if i call the function with a map object and try to delete a key from it, it fails because it isn't a map.
It also can't be immutable and casting it to a map doesn't work too.
So whatever it is, i just want to remove a key from it. Does anyone know the datatype so i am able to find the correct function?
As #Delwinn stated out, the 'data' object seems to be a 'json object' (if this is a type in typescript) and not a map.
to delete a value from this object, a plain line like
delete json[key]
will do the job.
And yes, 'delete' is written like an operator and not a function.

Rust Map Syntax Explanation Needed

recently started learning Rust and I am having some problems with syntax.
Can anyone explain to me this line:
// Stores all the kitties, key is the kitty id / index
pub Kitties get(fn kitties): map hasher(blake2_128_concat) u32 => Option<Kitty>;
So we're creating here public Kitties(variable) that accepts some function.
Kitties is of a type map hasher(blake2_128_concat) u32 which returns Option
map hasher? Also couldn't find hasher in documentation.
Based on the link you posted in the comments, the code snippet is part of a larger snippet that looks something like
decl_storage! {
trait Store for Module<T: Trait> as SimpleMap {
SimpleMap get(fn simple_map): map hasher(blake2_128_concat) T::AccountId => u32;
}
}
The decl_storage! call is a macro (macro calls are always ended in an exclamation point, like vec! or println!), which in essence means it can do whatever it wants with the stuff that follows it. In particular, the things inside the outer braces needn't be valid Rust, as decl_storage! can transform the contents freely.
My guess (at a quick Google search) is that decl_storage! refers to this macro, so you'll have to refer to its documentation to see what it expects.
This is a macro rule. Not origin Rust syntax.
Check this out: https://substrate.dev/rustdocs/v3.0.0/frame_support/macro.decl_storage.html
If you want to learn more about Map storage type in substrate check this, it also explains what hasher is:
https://substrate.dev/recipes/storage-maps.html

Kotlin arrow-kt Flatten nested Either

I'm programming using the functional library arrow-kt (formerly known as kategory). I'm using Either monad to aggregate errors or success information of an api call. I got myself into a state (which shouldn't happen in the first place) in which I have a nestet Either monads. I'm trying to flatten the structure to get the inner monad. The documentation is very limited and I couldn't find a way to do It.
Here's an example of a nested Either monad that I would like to flatten:
Either.right(Either.right(Either.left("error")))
You may flatten such a structure with flatten:
import arrow.core.*
import arrow.typeclasses.*
val result = Either.right(Either.right(Either.left("error")))
Either.monad<String>().flatten(result)
// keep flattening until you are happy with the result
// Right(b=Left(a=error))
Or just flatMap:
import arrow.core.*
import arrow.typeclasses.*
val result = Either.right(Either.right(Either.left("error")))
result.flatMap { it.flatMap { it } }
// Left(a=error)
The fact that you ended up with such nested structure probably means that you are not using the right datatype or wrong abstraction at some point in your program as that is kind of a useless value.
If you wish to preserve the left values as indicated in your comment I think a more suitable datatype would be Validated which allows for error accumulation as demonstrated here http://arrow-kt.io/docs/datatypes/validated/
Alternatively Either#fold can help you contemplate both cases and coalesce then into whatever value you wish.
I'm assuming you already run into these where most of this stuff is explained but just in case some useful links that will help you model this with Arrow
Docs: http://arrow-kt.io/docs/datatypes/either/
Video: https://www.youtube.com/watch?v=q6HpChSq-xc
FP Error handling with Arrow: http://arrow-kt.io/docs/patterns/error_handling/
Additionally feel free to swing by our chat channels if you need a more interactive experience with the maintainers and other contributors than SO where we frequently help people of all levels learning FP and Arrow.
Gitter: https://gitter.im/arrow-kt/Lobby
Slack: https://kotlinlang.slack.com/messages/C5UPMM0A0
Cheers!

In smalltalk, deep copy of LinkedList object gives error

This is the smalltalk code I wrote to deep copy two linkedlist object.But when I do this Small talk interpreter raises an error stating that:Unhandled Exception: Message not Understood: nextlink.
list1 add:2.
list2 :=list1 dcopy.
list1 ==list2.
Kindly tell me what is the problem with my code.
This is VisualWorks. LinkedLists are collections used for internal system use and aren't intended for general use. The items added into LinkedLists must subclass from Link (or implement nextLink and nextLink:). You can't add a SmallInteger into a linked list. You can do this:
LinkedList new
add: (LinkValue value: 5);
add: (LinkValue value: 7)
We don't generally use linked lists in Smalltalk. We normally use OrderedCollection instead. If you really need a linked list, add elements which are subclasses of Link.

How can I tell the Closure Compiler not to rename an inner function using SIMPLE_OPTIMIZATIONS?

How can I tell the Closure Compiler not to rename an inner function? E.g., given this code:
function aMeaninglessName() {
function someMeaningfulName() {
}
return someMeaningfulName;
}
...I'm fine with Closure renaming the outer function (I actively want it to, to save space), but I want the function name someMeaningfulName left alone (so that the name shown in call stacks for it is "someMeaningfulName", not "a" or whatever). This despite the fact that the code calling it will be doing so via the reference returned by the factory function, not by the name in the code. E.g., this is purely for debugging support.
Note that I want the function to have that actual name, not be anonymous and assigned to some property using that name, so for instance this is not a duplicate of this other question.
This somewhat obscure use case doesn't seem to be covered by either the externs or exports functionality. (I was kind of hoping there'd be some annotation I could throw at it.) But I'm no Closure Compiler guru, I'm hoping some of you are. Naturally, if there's just no way to do that, that's an acceptable answer.
(The use case is a library that creates functions in response to calls into it. I want to provide a version of the library that's been pre-compressed by Closure with SIMPLE_OPTIMIZATIONS, but if someone is using that copy of the library with their own uncompressed code and single-stepping into the function in a debugger [or other similar operations], I want them to see the meaningful name. I could get around it with eval, or manually edit the compressed result [in fact, the context is sufficiently unique I could throw a sed script at it], but that's awkward and frankly takes us into "not worth bothering" territory, hence looking for a simple, low-maintenance way.)
There is no simple way to do this. You would have to create a custom subclass of the CodingConvention class to indicate that your methods are "local" externs (support for this was added to handle the Prototype library). It is possible that InlineVariables, InlineFunctions, or RemoveUsedVariables will still try to remove the name and would also need to be fixed up.
Another approach is to use the source maps to remap the stack traces to the original source.
read the following section
https://developers.google.com/closure/compiler/docs/api-tutorial3#export
Two options basically, use object['functionName'] = obj.functionName or the better way
use exportSymbol and exportProperty both on the goog object, here is the docs link for that
http://closure-library.googlecode.com/svn/docs/closure_goog_base.js.html
-- edit
ah, i see now, my first answer is not so great for you. The compiler has some interesting flags, the one which might interest you is DEBUG, which you can pass variables into the compiler which will allow you to drop some debugging annotations in via logging or just a string which does nothing since you are using simple mode.
so if you are using closure you can debug against a development version which is just a page built with dependiencies resolved. we also the drop the following in our code
if(DEBUG){
logger.info('pack.age.info.prototype.func');
}

Resources