Dev.to AI πŸ€– Ai πŸ‘ 0 πŸ“– 1 min read

Building a Numerical Analysis HTTP Endpoint with greenServeFE

I've been working with the gs_compute and server native modules in greenServeFE. Here's a small example that combines numerical computation with an HTTP endpoint. The program calculates statistical values from a sample

I've been working with the gs_compute and server native modules in greenServeFE. Here's a small example that combines numerical computation with an HTTP endpoint.

The program calculates statistical values from a sample dataset, solves a system of linear equations, and makes the results available through a GET endpoint.

Complete example (server.gsve)

import gs_compute;
import server;

define samples = [12, 18, 24, 30, 36];

define summary = gs_compute.stats(samples);

define coefficients = [
    [3, 2],
    [1, 2]
];

define constants = [18, 14];

define solution = gs_compute.solve(
    coefficients,
    constants
);

func analysis(req, res) {
    define result = {
        samples: samples,
        mean: summary.mean,
        variance: summary.variance,
        stddev: summary.stddev,
        minimum: summary.min,
        maximum: summary.max,
        solution: solution
    };

    res.json(result);
}

server.get("/api/analysis", analysis);

print("Analysis endpoint registered.");

server.serve(".", 8080, {
    static: false,
    host: "0.0.0.0"
});

What's happening?

  • gs_compute.stats(samples) calculates statistical information about the dataset.
  • gs_compute.solve(coefficients, constants) solves the system of linear equations.
  • server.get() registers a GET route at /api/analysis.
  • res.json(result) sends the calculated results as a JSON response.
  • server.serve() starts the HTTP server on port 8080.

The system of equations used in this example is:

3x + 2y = 18
 x + 2y = 14

The solution is x = 2 and y = 6.

Running the program

greenServeFE server.gsve

The endpoint is:

GET http://localhost:8080/api/analysis

This is a simple example of combining native numerical computing and HTTP server functionality in one greenServeFE program.

Project: greenServeFE - a programming language and bytecode virtual machine written in C.

Developer: Dominex Macedon

πŸ“° Read the original article on Dev.to AI

Originally published by Dev.to AI. Aggregated on AIWithGhost for educational purposes β€” full credit and traffic to the original publisher.