What would be the idiomatic Clojure way to do this. In js we can use json to stringify the array and then save this in local storage, but I am not sure what would the ideal solution be in clojure.
I am assuming you mean browser local storage.
You could just (de)serialize your Clojure value as an EDN string.
Please see spit and slurp from the Clojure CheatSheet. It can be found through this list of documentation sources.
Related
I am working on a Windows app using javascript as the UI. We are delegating a heavy processing task to a c++ component in order to increase performance. Unfortunately we have to transfer a large amount of JSON data between our c++ component to the javascript (like length 100000+ length arrays of objects) and I'm not sure the best way to do this. Our app is looking to perform this computation multiple times per second. Truth be told I don't actually know what is going on behind the scenes when data gets passed between javascript and c++.
One approach that I'm considering is to stringify the JSON, and pass that string through to the javascript before parsing. I haven't done any testing on that, but I'm afraid that it would be quite slow. As I mentioned above I don't know how/if the long string length would affect data transfer, and what the stringify and parse impact would be.
Another approach that I have considered is to essentially store the data as a blob, and pass the address to the javascript which could read the data from the blob. However I find the Windows documentation a little sparse/difficult to search (I'm a c++ rookie) and I don't know how I would accomplish that.
What I'm looking for is advice as to which method sounds better (and I'm open to other suggestions!) and/or help as to how to accomplish method 2. Thanks for your time!
I have a perl script which generates a very large data structure (which starts life as an array of array references). This is then written to a text file using some weird home-brew serialisation scheme.
The data from the text file is stored as the value in a key-value store db.
A c++ file then retrieves the data, and deserializes it (into a hashmap, although can potentially be flexible on how this data is structured).
What I'm interested in is finding if there are any good ways of sharing a data structure between perl and c++ (something like Storable, but that is meant for perl->perl not perl->c++). The current method is a headache to maintain, and may not have the best performance.
The most important factors are speed of deserialisation, and the size of the serialized structure in that order. Anyone know of something that might do the trick?
Storable is one way to dump and load perl data structures. I wouldn't actually recommend it for general usage though - it's handy in that it's part of core and easy to use.
But for multi-platform (and language) portability, it's far better to use a standard data representation. Which you choose is probably a matter of what sort of data you're holding in your structure, but core contenders are:
JSON - good for arrays and hashes (key-value).
YAML - Excellent for 'config file' style data (but extends in ways similar to JSON)
And if you must, XML - but bear in mind that XML is designed for documents-with-metadata, and so IMO isn't suitable for most of the applications it's used for.
As standards, they've got documented formatting and parsers are widely available. And implementing your own isn't too hard, if that's the route you want to go. Just make sure you follow the spec and you're good.
Note - that because XML and JSON (and I think YAML?) are recursive, you can parse as a stream, rather than a standalone object. (Trap, process and discard as you hit 'close brackets' in JSON, or 'close tags' in XML).
easy job.
I like perl , and I also like C/C++. To make the best of both,
I wrote a github project to solve this issue.
please see:
https://github.com/tlqtangok/perlcpp
a short example is here :
P_eval("$a=2;$a=$a**10;");
Int("a") ; // a= 1024
Clojure has this handy way to view a function's definition in REPL.
Is there one in Elixir REPL ?
No, there isn't a convenient way yet.
Well, I'm going to post this as an answer, even though it's not the same as "source". In IEX, you can type h function_name to see the documentation for function_name. It's basically Clojure REPL's doc feature. So far, in Elixir, this is good enough for most cases where I want source.
What kind of API or algorithm is normally used to generate session ids? For something like an online game?
Thanks
You can use UUID-v4. Implementations are available in pretty much any language. Here is one for C++:
http://sourceforge.net/projects/ooid/
i++?
And you can make it a string and hash (MD5) the result.
I'm trying to create a message validation program and would like to create easily modifiable rules that apply to certain message types. Due to the risk of the rules changing I've decided to define these validation rules external to the object code.
I've created a basic interface that defines a rule and am wondering what the best way to store this simple data would be. I was leaning towards XML but it seems like it might be too heavy.
Each rule would only need a very small set of data (i.e. type of rule, value, applicable mask, etc).
Does anyone know of a good resource that I could look at that would perform a similar functionality. I'd rather not dig too deep into XML on a problem that seems to barely need a subset of the functionality I see in most of the examples I bump into.
If I can find a concise example to examine I would be able to decide on whether or not to just go with a flat file.
Thanks in advance for your input!
Personally, for small, easily modifiable XML, I find TinyXML to be an excellent library. You can make each class understand it's own format, so your object hierarchy is represented directly in the XML.
However, if you don't think you need XML, you might want to go with a lighter storage like yaml. I find it is much easier to understand the underlying data, modify it and extend functionality.
(Also, boost::serialization has an XML archive, but it isn't what I'd call easily modifiable)
The simplest is to use a flat file designed to be easy to parse using the C++ >> operator. Just simple tokens separated by whitespace.
Well, if you want your rules to be human readable, XML is the way to go, and you can interface it nicely with c++ using xerces. If you want performance and or size, you could save the data as binaries using simple structs.
Another way to implement this would be to define your rules in XML Schema and then have an XML Data Binding tool generate the corresponding C++ object model along with the XML parsing and serialization code. One such tool (that I happen to be working on) is CodeSynthesis XSD:
http://www.codesynthesis.com/products/xsd/
For a 2-minutes overview of the idea, see the "Hello World" example in the C++/Tree mapping documentation.