Map of type 'a key -> 'a in Rust [closed] - data-structures

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last year.
Improve this question
In OCaml, there is a construct called univ_map.t, which allows you to map from 'a Type_equal.Id.t values to 'as. Here is an example.
Is there a construct that would allow me to do something similar in Rust? I know in OCaml they are implemented with open variants, which I don't believe Rust has.

I'm not familiar with OCaml, but looking at the docs:
Univ_map: Universal/heterogeneous maps [...] useful for storing values of arbitrary type in a single map [...] built on top of Univ.
Univ: An extensible universal variant type. Every type id corresponds to one branch of the variant type.
The closest thing that Rust has that sounds like Univ is the Any trait, which is designed to represent any type (with exceptions). However, there is no standard type for storing a collection of Anys that is accessed by its TypeId. From looking how popular crates handle this, its usually a bespoke wrapper around HashMap<TypeId, Box<dyn Any>> or similar. I hope I've understood correctly.

Related

Is there a way to print a string variable in its 2 words format? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 months ago.
Improve this question
I would like to find a clear way to demonstrate concretely that a variable of type string holds a 2-words data structure (at least as far as I understand it).
This demonstration is for didactic purposes.
So, as I know, a string is a 2 words data structure where one word holds the address of the underlying slice of bytes and the word holds the length.
Given a variable defined like this a := "a string literal", is there a way to view (or print) the content of the variable in its 2 words format so that people can actually see this 2-words structure?
is there a way to view (or print) the content of the variable in its 2 words format?
No, because this is an unspecified implementation detail.
If you are okay with code that might brake: Use reflect.StringHeader. See unsafe.Pointer point (6) on how to do this.
Best not to do this. As said: this is a deliberately hidden implementation detail.

How to convert [u8] to [u32]? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last year.
Improve this question
I wanted to embed an image to my binary and used the "include_bytes" macro. The GUI library I wanted to use only accepts [u32] for input and the said macro produces only [u8].
How do I convert a [u8] to a [u32]? I've seen some in the internet but the explanations are a bit too technical for me (I'm only self-taught). There were several options that I saw like bitwise and a method in "u32" from the standard library. Anyone can give an actual code on how to do it? Like study it from there in case I will need it for other files in the future. Thank you. I almost always just understand things via code coz I'm not aware of many technical terms, algos, etc.
using .map(Into::<u32>::into)
fn main() {
assert_eq!([0_u8, 1_u8].map(Into::<u32>::into), [0_u32, 1_u32]);
}

Does Go support functional programming? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
Improve this question
As in java8:
someList.stream().map(e->e.getXXX()).toList()
For example, I have a Student array/slice, and the struct Student contains properties like Id, Name, and so on.
I want to extract all Ids into a NEW array/slice with one-line code like java8 as mentioned above, instead of range. Is there is an example?
Currently there is not an easy, builtin way to do this. Although Go has first-class functions and lexical closure, it's not possible to write a function like map that will operate on arbitrary types in the way you want. (Also, there's no compact lambda syntax, but I consider that a relatively minor issue).
Instead, you have to do one of the following:
Operate on interface{}. While this would let you write a func map([]interface{}, func(interface{})interface{}) []interface{}, you lose compile-time type safety, you lose performance, and a []interface{} is not a []string (or whatever the type is of the field you wanted to fetch), nor can you even type-assert it to one, so working with the result is cumbersome.
Use code-generation. There are libraries out there that will generate map/filter/etc. code for you, specialized to given types, so that none of the disadvantages of #1 apply. And Go ships with a Go parser in the standard library, so most code generators are fairly robust. But code generation is a separate build step, hampers debuggability, and can hurt the clarity of code.
Just live with boilerplate, writing lots of loops, and forget about trying to achieve functional style.
Wait for Go 1.18 to bring generics, which should make libraries of functional idioms a lot more practical.
Most experienced Go users would recommend approach #3, and so do I (reluctantly).

golang structs or maps in RESTFUL API [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
When creating a webserver in golang, is there any specific reason why JSON data would be represented as a MAP over a STRUCT or vice versa? ..or is this decision based purely on user preference ?
I think this discussion could go either way but the advantages of using structs vs maps are that structs give you an idea of how the schema should look like whereas a map leaves the schema open-ended.
If you use structs, developers who look at the code will have a clear idea of what parameters you're expecting for the API or how the response of the API may look like without digging further into implementation detail. On the other hand if the requests or responses were maps, they would have to look at the implementation detail to see what keys and values are being assigned. Hope this helps!

sub-subclass concept in OWL [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am posting this on stackoverflow since I am quite confused with OWL right now.
So in OWL file, can I represent a sub-subclass concept?
For example, entities belong to a class called Wine and it inside this class, we have a sub-class called WineType and then Within the WineType, we have sub-subclass called enzyme_avability. Would this be possible in OWL as a nested class concept? (e.g. )
Please help me with this
Yes it is possible. I think what you are looking for is here
http://www.w3.org/TR/owl2-syntax/#Object_Property_Restrictions
So in sort you are representing a hierarchy structure which is completely supported by OWL.
You can write axioms like:
A subClassOf B
B subClassOf C
and so on, without limits on the number of levels you want to define. A reasoner will be able to answer queries like: is A subclass of C? by following the hierarchy.
Of course, more complex ways to arrange the hierarchy exist, e.g., the object property restrictions mentioned by Jinal.

Resources