I'd like to try FFI with JS.

For that purpose, I'd like to play with 
[crossfilter](https://github.com/crossfilter/crossfilter).

A minimal crossfilter example looks like `example.js`: 
    
    
    var livingThings = crossfilter([
      // Fact data.
      { name: "Rusty",  type: "human", legs: 2 },
      { name: "Alex",   type: "human", legs: 2 },
      { name: "Lassie", type: "dog",   legs: 4 },
      { name: "Spot",   type: "dog",   legs: 4 },
      { name: "Polly",  type: "bird",  legs: 2 },
      { name: "Fiona",  type: "plant", legs: 0 }
    ]);
    
    // How many living things are in my house?
    var n = livingThings.groupAll().reduceCount().value();
    console.log("There are " + n + " living things in my house.") // 6
    
    
    Run

With `index.html`: 
    
    
    <html>
    <body>
      <script type="text/javascript" src="crossfilter.min.js"></script>
      <script type="text/javascript" src="example.js"></script>
    </body>
    </html>
    
    
    Run

I don't know how should I send the data to the JavaScript function. I tried 
something like the following for the FFI part: 
    
    
    import json
    
    when not defined(js):
      {.error: "This module only works on the JavaScript platform".}
    
    type
      Crossfilter = ref object
    
    proc crossfilter*(a:JsonNode): Crossfilter {.importc:"crossfilter".}
    
    proc groupAll*(a:Crossfilter):Crossfilter {.importc:"crossfilter.groupAll".}
    
    proc reduceCount*(a:Crossfilter):Crossfilter 
{.importc:"crossfilter.reduceCount".}
    
    proc value*(a:Crossfilter):int {.importc:"crossfilter.value".}
    
    
    Run

and the following for the Nim replacement code: 
    
    
    import crossfilter, json
    
    let data = """
    [
      { name: "Rusty",  type: "human", legs: 2 },
      { name: "Alex",   type: "human", legs: 2 },
      { name: "Lassie", type: "dog",   legs: 4 },
      { name: "Spot",   type: "dog",   legs: 4 },
      { name: "Polly",  type: "bird",  legs: 2 },
      { name: "Fiona",  type: "plant", legs: 0 }
    ]
    """
    let livingThings = crossfilter(data.parseJson)
    
    var n = livingThings.groupAll().reduceCount().value()
    echo "There are " & $n & " living things in my house."
    
    
    Run

How should I send `data` to `crossfilter`? 

Reply via email to