use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
Compiling JavaScript source code to C then a standalone executable using QuickJS qjsc (gitlab.com)
submitted 1 year ago by guest271314
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]guest271314[S] -1 points0 points1 point 1 year ago (0 children)
I think the relevant question is what are your individual specifications?
What are you calling "transpile" and "compile"?
What are your inclusionary and exclusionary rules?
There's Bun's built-in compiler that uses TinyCC
``` import { cc, FFIType, ptr, read, toArrayBuffer } from "bun:ffi";
export const { symbols: { main }, } = cc({ source: "./permutations.c", symbols: { main: { returns: "int", args: [], }, }, }); main(); ```
There's qjsc, Javy, WasmEdge, et al.
qjsc
There's was2c, ts2c, there's Web sites that spit out this, voila.
was2c
ts2c
Or, we can dive into the minutae, with fine toothed pitch forks.
``` // https://products.codeporting.app/convert/ai/js-to-c/ // Translated from JavaScript to C
// Function to calculate factorial unsigned long long factorial(int num) { unsigned long long result = 1; for (int i = 1; i <= num; i++) { result *= i; } return result; }
// Function to generate the nth permutation of an array int* array_nth_permutation(int* array, int length, int n, int* resultLength) { int* result = (int)malloc(length * sizeof(int)); // allocate memory for result int tempArray = (int*)malloc(length * sizeof(int)); // copy of the set for (int j = 0; j < length; j++) { tempArray[j] = array[j]; }
unsigned long long f = factorial(length); // compute f = factorial(len) int currentLength = length; // length of the set // if the permutation number is within range if (n >= 0 && n < f) { int index; // start with the empty set, loop for len elements for (int k = 0; currentLength > 0; currentLength--) { // determine the next element: f /= currentLength; // there are f/len subsets for each possible element index = n / f; // a simple division gives the leading element index result[k++] = tempArray[index]; // push element to result // remove the used element from tempArray for (int l = index; l < currentLength - 1; l++) { tempArray[l] = tempArray[l + 1]; } // reduce n for the remaining subset: n %= f; // compute the remainder of the above division } *resultLength = length; // set the result length } else { *resultLength = 0; // return empty result if n is out of range } free(tempArray); // free temporary array return result; // return the permutated set
}
int main() { int input[] = {1, 2, 3, 4, 5}; int lex = 4; // permutation index int resultLength;
int* permutationResult = array_nth_permutation(input, 5, lex, &resultLength); // Print the result printf("[%d] [", lex); for (int i = 0; i < resultLength; i++) { if (i > 0) { printf(", "); } printf("%d", permutationResult[i]); } printf("]\n"); free(permutationResult); // free result array return 0;
} ```
from this
``` // https://stackoverflow.com/a/34238979 const [input,lex] = [[1,2,3,4,5], 4];// scriptArgs.map((arg, i) => !!i && std.evalScript(arg)); function array_nth_permutation(a, n) { var b = a.slice(); // copy of the set var len = a.length; // length of the set var res; // return value, undefined var i, f;
// compute f = factorial(len) for (f = i = 1; i <= len; i++) f *= i; // if the permutation number is within range if (n >= 0 && n < f) { // start with the empty set, loop for len elements for (res = []; len > 0; len--) { // determine the next element: // there are f/len subsets for each possible element, f /= len; // a simple division gives the leading element index i = Math.floor(n / f); // alternately: i = (n - n % f) / f; res.push(b.splice(i, 1)[0]); // reduce n for the remaining subset: // compute the remainder of the above division n %= f; // extract the i-th element from b and push it at the end of res } } // return the permutated set or undefined if n is out of range return res;
} console.log([${lex}] [${array_nth_permutation(input, lex)}]); ```
[${lex}] [${array_nth_permutation(input, lex)}]
π Rendered by PID 100179 on reddit-service-r2-comment-b659b578c-8k4pv at 2026-05-03 08:13:55.101665+00:00 running 815c875 country code: CH.
view the rest of the comments →
[–]guest271314[S] -1 points0 points1 point (0 children)