ReasonML sample web server with a db - database

I've been taking a look at ReasonML (https://reasonml.github.io/) and in general, as a 'loyal' ;) functional programmer, I like the idea. However, I believe there's a missing part in my reasoning about the project.
In particular I'm a bit confused if it comes what to search for. For example, I'd like to build a simple web server. Shall I use JS-related libraries (express, ...), OCaml technologies, or maybe yet something else ?
What I'm actually missing is a step-by-step guide that presents a way to build a full basic application (in this case: let's say a simple web server with db connection).
Last thing - forgive me imprecise language. As I said: I'm pretty sure there's a gap in my reasoning about ReasonML and I'd like to fill it ;).

If you want to write portable code, you should use the OCaml technologies, so Array.length (from OCaml core) instead of Js.Array.length (Bucklescript JS wrapper).
If you do not care about native code, but just want to target JS (node/browser), then you can use the FFI and leverage your existing knowledge of JS libraries.
IMHO, this FFI is one of the nicer things of reasonML. The resulting code is small and you can inspect the .bs.js files to see what it's doing.
But as said, you lose the ability to generate native code this way.
Here is an example,
https://github.com/wires/reason-ffi
Say I don't have a range function in OCaml or ReasonML and I don't want to write one, but I know ramda has one. Just write some JS,
// range.js
exports.range = require('ramda').range
Then wrap it with types, like
[#bs.module "./range.js"] external range' : (int,int) => array(int) = "range";
let range : (int,int) => list(int) = (a,b) => range'(a,b) |> Array.to_list
I'm not saying this is the ultimate way to use this tool, but I find it a very frictionless way to transition untyped garbage JS to something reasonably maintainable.
And you can leverage your existing JS library knowledge and keep building with reasonML, instead of spending your time writing a boring range function (which is also learning... of course)

Related

Terminology - one-time code generation directives

Is there a such thing as a preprocessor whose statements, once processed, disappear completely and get replaced by the target language syntax permanently?
I want to research it on the web but I don't know what term to search for. If I search for "code generator", "templating language", "preprocessor directives", "mixins", "annotations" I get generators whose input becomes the source of truth.
The closest thing I can think of is a macro.
What I'm trying to do
I often have to write code that is verbose and unnecessary manual labor and am looking for a smarter way to input at least the majority of it and have it automatically transformed and only source-control the output (and hand edit if necessary). For example:
Java code - Instead of writing getters/setters, javadoc (perhaps the transformer can be a maven plugin)
HTML - I just want to add URLs, and have my preprocessor automatically convert them to links, images, videos, audio etc. depending on the file extension with some regex substitution (currently I run a perl script via a cron job)
I just want to use it as my own shorthand and not enforce it in my project and make the output editable so that others have to learn a new framework or language (like Protobuf, Stringtemplate, GWT, C hash-defines, PHP, JSP etc).
There should be no direct clue that I used a template/preprocessor to generate it.
What you want is a "program transformation system". See https://en.wikipedia.org/wiki/Program_transformation. (This is a superset of "transpilers" [ugly term]).
A good source-to-source transformation system will let you apply rewrite rules of the form of:
if you see *this*, replace it by *that* if *this_condition*.
You can then take your source code, and run a set of rewrite rules across that code to change it.
The resulting code is "transformed"; the rewrite rules are not visible.
It seems like Transpiler is one way to describe it.

Setup for enhancing the C language using mbeddr and mps

I'm trying to write some new statements for an internal C DSL.
The tool of my choice is MPS and the base is the c-core of mbeddr.
I found this tutorial on the internet. Since it is a little old, I am not able to duplicate the exact structure of the setup itself.
What I want to do is to create a new language. This language must be extended by com.mbeddr.core. I found, how to extend the language and did so. I needed to extend every single language com.mbeddr.core.* by its own, everything else did not work (it does not even work properly now).
The I created a new statement in a structure module. There I extended the concept to a Statement (c.m.c.statements.structure is shown). Then, following the video tutorial, I wanted to add an expression as a child, this is where it fails.
I am not sure where I went wrong here. It is very difficult to set it up properly, since all tutorials/guides are outdated.
Thanks for your help,
Best Simon

Pretty Tables in C

Are there any libraries/function available in C to pretty print tabular data. I am looking for something like this : http://code.google.com/p/prettytable/
If you really want to implement something as generic-looking as the Python module seems to be, one problem is going to be data storage. It's not so easy to just throw stuff into a list in C, of course. You might want to look at glib's GVariant solution for instance. It would be pretty easy to build a table-formatter handling any reasonable type of value based on that.

Compiling C# code from txt file to interface with running wpf application

I have been searching online for a neat way to compile code at runtime and manipulate the running application's objects (properties etc.). I have come across Snippy, CodeDom and CSharpCodeProvider but I didn't completely understood how I can use those solutions in my application to do what I want.
Bottom line is, I want to have a small portion of the code in an external file so I can swap out different code during run time (e.g. Formulas to manipulate Data and etc.) Could somebody explain to me how exactly can I implement this in a WPF application in a neat fashion? I just need to pass the external code some data and after execution it will return me some data that I can populate an object with.
P.S: I also thought about parsing Mathematical expression from String and manipulate my data that way but if I can parse and execute C# code externally at run time, it will give me much more freedom and control over the formula and data. Thanks in advance.
I think you would do better using .NET compatible dynamic language like IronPython or IronRuby. Those integrate pretty much seamlessly with C#/.NET and are actually designed to provide execution-time scriptability.
Trivial example of IronPython usage from Jon Skeet's C# in Depth:
// the python code
string python = #"
text = 'hello'
output = input + 1
";
// setup the scripting engine
ScriptEngine engine = Python.CreateEngine();
ScriptScope scope = engine.CreateScope();
scope.SetVariable("input", 10);
engine.Execute(python, scope);
// the results
Console.WriteLine(scope.GetVariable("text"));
Console.WriteLine(scope.GetVariable("input"));
Console.WriteLine(scope.GetVariable("output"));
Needless to say, the scripts can use .NET Base Class Library and any extra methods you expose to them.

XPathNavigator in Silverlight

I have a code library that makes heavy use of XPathNavigator to parse some specific xml document. The xml document is cross-referenced, meaning that an element can reference another which has not yet been encountered during parsing:
<ElementA ...>
<DependentElementX id="1234">
</ElementA>
<ElementX id="1234" .../>
The document doesn't really look like this, but the point is that 1) there is an xml schema that enforces the overall document structure, 2) elements inside the document can reference each other using some IDs, and 3) there is quite a few such cross references between different elements in the document.
The document is parsed in two phases. In the first pass I walk through the document
XPathDocument doc = ...;
XPathNavigator nav = doc.CreateNavigator();
nav.MoveToRoot();
nav.MoveToFirstChild()...
and occasionally 'bookmark' the current position (element) in the document using XPathNavigator.Clone() method. This gives me a lightweight instance of an XPathNavigator which I can store somewhere and use later to jump back to a particular place (element) in my document.
Once I have enough information collected in the first pass (for example, I have made sure there is indeed an ElementX with an id='1234'), I jump back to saved bookmarks (using those saved XPathNavigators) and complete the parsing.
Well, now I'm about to use this library in Silverlight 3.0 and to my horror the XPathNavigator is not in the System.Xml assembly.
Questions:
1) Am I missing something obvious (i.e. XPathNavigator does exist in some shape or form, for example in a toolkit or a freeware library)?
2) If I do have to make modifications in the code, what would be the best way to go? Ideally, I would like to make minimal changes, not to rewrite 80% of the code just to be able to use something like XLinq.
To resume, in case I have to give up XPathNavigator, all I need is a way to bookmark places in my document and to get back to them so that I can continue to iterate from where I left off.
Thanks in advance for any help/ideas.
You are not missing something obvious, there is no implementation of XPathNavigator or XPathDocument in the Silverlight versions of the libraries.
The "best way to go" is highly subjective and would really depend on how many lines of code are really depending on XPathNavigator. However I see a couple of choices.
Go ahead and re-write the code using XDocument, XElement etc from the System.Xml.Linq namepsace. This may not be as bad a choice as you might think.
Wrap Xml-to-Linq objects in your own implementation of those properties and methods of the XPathNavigator that you are actually using. It shouldn't be too hard re-create most the features of the XPathNavigator against the Xml-to-Linq objects. You can then run your existing code against your own XPathNavigator.
XPath (xdoc.XPathSelectElements) is available in Silverlight 4: here's an online test tool.
There are tons of ways:
How to deal with XML in C#
You can still use Linq to XML just minus the linq syntax and use the Linq Extension methods.

Resources