I understand that their is Clojure the sits on the JVM in which you can use Java libraries. Also, there is ClojureScript which allows you to use JavaScript libraries, but is it possible to mix libraries from each in one Clojure file / project?
You can write Clojure in .cljc files which can be read by both Clojure and ClojureScript. Any platform specific code can be wrapped in Reader Conditionals so it is only read on the host platform. Many libraries support cljc, though they do need to be converted, and it may not always make sense to do so if there is not a lot of common code between the two. See http://clojure.org/guides/reader_conditionals for more information.
Related
I'm working through Web Development with Clojure and I noticed that the ClojureScript examples the author provides use the conventional app-name.core namespace, which is also being used by the Clojure part of the application for a lot of boilerplate code from Luminus.
ClojureScript and Clojure are often used to evaluate the same code for client and server sides of an app, respectively, so how does it differentiate between code in the ClojureScript core namespace and the Clojure core namespace? Does it use the directory structure? Can you use code from both w/o having to specify?
You program in JavaScript regardless of whether your code runs on Node.js or in a browser. Obviously depending on the platform, the API isn't the same.
My understanding is that ClojureScript is just a compiler. You write in plain Clojure (with a subset of the API) and then use ClojureScript to compile Clojure to JavaScript.
Therefore I don't think there's any difference in how namespaces are managed:
Replace . with /
Append clj or cljs
Replace - with _
Unless configured otherwise assume that src is the root directory
So app-name.core "resolves" to src/app_name/core.cljs for example.
I think (could be wrong) that on the JVM if a file doesn't exist on the disk, the compiler will look inside JARs (if any). Obviously that wouldn't happen with ClojureScript.
I have said in other answers that IMHO it is a mistake to include both frontend code (CLJS) and backend code (CLJ) in the same git repository. You fundamentally have two different programs that are running independently, are on separate hardware, and are communicating over a network. Any common utils used by both programs should be in a separate utility library, which would be a 3rd repo.
This organization provides the most independence and clarity for the 2 (or 3) codebases, independent of the language in use. It applies equally to any combination of languages used by the 2 programs:
Python & Python
Python & Java
JS (browser) & Python
CLJS (browser) & Java
CLJS (browser) & Clojure
any other combination...
So you use *.clj files for the backend code, and *.cljs files for the frontend code [1]. If you have any common utilities, make a 3rd repo and use *.cljc files for this shared library.
For frontend CLJS code, it is much easier IMHO to use the new figwheel-main and Clojure Deps tooling than the older lein tooling. Please see figwheel.org for details. This also makes it easier to keep the CLJ repl and the CLJS repl separate (no "piggieback" stuff required). It also keeps the CLJ compilation and CLJS compilation separate, which also simplifies things.
Having stated the above, I hope the answer to your original question becomes clearer. The CLJS code may have a namespace demo.core, that is compiled into JavaScript, that is running in a browser, on the user's computer. The CLJ code may also have a namespace demo.core, that is compiled into Java bytecode, that is running on a backend server in the cloud. So there is only one ns demo.core in each execution environment, so they never clash.
So, we see that the 2 compilers treat the CLJ and CLJS codebases as independent programs. This is why it is simpler IMHO to use two different git repositories to prevent conflating the two independent codebases.
Footnotes:
[1]. If you have macros in the CLJS frontend code, you will also need to define them in either *.clj or *.cljc files due to quirks in the CLJS compiler. We will ignore that complication here.
I have written a complex math library for JavaScript that features the ability to generate functions from strings of human-readable math expressions. Is there a way to achieve an equivalent of runtime-generated functions in C++?
FUZxxl's answer is right, and I recommend looking at the Clang/LLVM facility.
There is a basic (not so helpful) tutorial file here. And a broad tutorial on writing your Language on LLVM. You can load your generated library in your C++ App.
Unless, you have a performance critical component, you can employ the use of ChaiScript (NB: I am in no way affiliated to it or the authors)
You can execute the C++ compiler, let it generate a shared library and load that into your program to run C++ code at runtime. Note that the details depend on what platform you are working on as Windows and POSIX have different mechanisms to load shared libraries.
The 'compiled language way' is to define your grammar, build a parser, an AST (abstract syntax tree) and interpret/compile that. When you do this, you're essentially writing your own compiler/interpreter and it's a lot of fun. If you want to get it working easily, you might take a look at boost spirit.
Referring to the following library and my previous thread, I have two questions:
Question #1: I have decided not to mess up with Java Libraries and hence could anyone tell me if there is another way to figure out how to use the libphonenumber library in coldfusion?
Question #2: As discussed in my previous thread, that many people are porting it to different programming languages like JavaScript, Ruby, PHP as they are not written in Java. The google library libphonenumber is written in PHP and I am wondering why someone would port it to PHP language.
(This is more of a comment, but is a little too long)
Seems like this was already answered in the comments of your other thread, but to reiterate:
Is there another way to use a java library from CF?
No. There is basically only one way to use java libraries from CF. Add the jar(s) to the class path and use createObject. You could also use a dynamic class loader like Mark Mandel's JavaLoader.cfc (or the rip of that project baked into CF10+). However, ultimately they all do the same thing.
Since using java libraries is pretty simple in CF, I am curious why you do not want to use it. While I suppose you could rewrite it in pure CFML, I would ask why? The whole point of libraries is reuse, which saves development time. Since there already is a compatible library available to you, there is not much point in rewriting it. Not unless you are doing it as a learning exercise.
The google library libphonenumber is written in PHP
No. There is port that is written in PHP. The "official" project is "a Java, C++ and Javascript library.". So it sounds like your options are the java version (server side) or javascript version (client side use). That is it.
I am wondering why someone would port it to PHP language.
Because the java library is not compatible with every platform out there, PHP being one of them. If a developer on an unsupported platform wanted to use it, they have two choices: port it or write their own from scratch. Since the google project already did most of the heavy lifting, porting is simpler.
I searched for this particular question but could not come up with any results, neither here nor on-line in general (maybe because it is a little harder to phrase for me). If it has already been asked, please point me in the right direction.
I am at a point where I would like my libraries/software to be pluggable. I see all these various libraries and systems where plugins are used extensively and the authors boastfully point out (in a good way!) that their software has plugin support.
So my question is, where do I start? Are there any books/on-line-resources that break the ice and may guide one on the do's and dont's of making your library pluggable, define best practices etc.?
You have to understand some things before starting :
There is no support for modules (static or dynamic) in standard C++. Nope. Not yet. Maybe in 2015.
Dlls (or .so on unix-like systems) are dynamically loaded libraries that are compiler/os dependant. So it's a pragmatic solution that fill the need.
So, you'll have to use shared libraries (whatever the file extension, it's the keyword for searches about this subject) as plugin binaries. If your plugin should contain more than runtime code, like graphic resources, you can include your graphic resources in the binary, or have a file format or compressed archibe that contain the binary file.
Whatever the way you setup your plugin files, in C++ the problem is about the interface.
Depending on wich compiler you use, you'll have different ways to "tag" functions or classes as exported/imported (meaning your plugin source code export the code and the user of the plugin should import the code).
Setup clean and clear interface in C++ for the modules, with no templates (because they are compiler and compiler configuration dependant). Those interfaces should be function declarations and class declarations with no inline code and marked exported/imported.
Now, once you've got this, you can use OS-specific API to load/unload dynamic library binaries while the application is running. Once it's done, you can get pointers to functions, again using the OS-specific API. I let you search for it.
Now, there are libraries that provide ways to abstract this in a cross-platform way. I didn't use them yet and they are known to be unperfect because of lack of definitions in the C++ standard, but they could be useful if you're planning to have your application cross-platform:
boost::extension : it's not yet a boost library, nor even proposed yet, and it's developpement is in pause (until some new standard C++ implementations are done) so it might be a bad idea but a lot of people say they use it with success.
POCO libraries have a library for shared libraries that would be the equivalent of boost::extension. Again lot of people say it's useful so I guess it's good enough to be used.
The other alternative, that is easy to setup if you don't need to support tons of target platforms, is to just write some wrapper code around OS-Specific APIs. That's what I did before knowing about boost::extension for example.
I depend heavily on Python's standard library, both for useful data structures and manipulators (e.g., collections and itertools) and for utilities (e.g., optparse, json, and logging), to skip the boilerplate and just Get Things Done. Looking through documentation on the C++ standard library, it seems entirely about data structures, with little in the way of the "batteries included" in Python's standard library.
The Boost library is the only open-source C++ library collection I know of that resembles the Python standard library, however while it does have utility libraries such as Regular Expression support, most of it is also dedicated to data structures. I'm just really surprised that even something as simple as assured parsing and writing a CSV file, made delightfully simple with the Python csv module, looks to require rolling-your-own in C++ (even if you leverage some parsing library by Boost).
Are there other open-source libraries out there for C++ that provide "batteries"? If not, what do you do as a C++ programmer: hunt for individual utility libraries (and if so, how), or just roll your own (which seems annoying and wasteful)?
The Poco library is more like other languages' standard libraries.
Actually the Poco web site's logo says "C++ now comes with batteries included!", which seems to be precisely what you're asking for.
I didn't like it when I tried because I found it too C-like and with too many dependencies between parts (difficult to single out just the functionality you want).
But there are many people & firms using it, so it seems I'm in minority and you will perhaps find it very useful.
In addition, as others have mentioned, for data structures, parsers, and indeed an interface to Python!, and such stuff, check out Boost.
Cheers & hth.,
While C++ does offer many of the comforts extended by OO it keeps a very simple standard library. C++ has STL and Boost. These are very good, and have more then just datastructures.
If your needs are these sorts of higher order functions for prototyping or making application without intense (relative term) speed requirements then C/C++ is probably not the right choice for you. I believe you will find that for most projects that high level languages will be fast enough for your needs. If you are working on an application that requires C/C++ speed (and accompanying standard deviations) then you should probably invest your time carefully picking each individual library you will be using.
http://beta.boost.org/community/sandbox.html
http://www.boostpro.com/vault/
also you can google for "boost+bar", eg
boost log ->
http://boost-log.sourceforge.net/libs/log/doc/html/index.html
boost threadpool ->
http://threadpool.sourceforge.net/
http://www.boost.org/doc/libs/1_45_0/?view=categorized
Boost isn't just about data structures - it has lots of the batteries you want - parsing, threads, collections, logging, etc.
With C and C++ you typically won't find a "do it all" library, instead you'll use individual libraries that do different things. You can use one library that does JSON parsing, one that does crypto, one that does logging, etc.
Boost and Qt are the only ones that would be more of a "do it all" type library.