Skip to content

Commit 5bfe17f

Browse files
committed
Reworked README
1 parent 13ef3d3 commit 5bfe17f

File tree

1 file changed

+101
-36
lines changed

1 file changed

+101
-36
lines changed

README.md

Lines changed: 101 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -123,13 +123,17 @@ Ranges are the building blocks of Funktion programming. They allow programmers t
123123

124124
These types of ranges will not stop until some goal condition is met. For example, a function with an infinite numerical range may end if the program will print on the first input value for the function that is divisible by 152, for instance. However, these types of functions will prematurely end if they reach or pass the bounds of the globally-defined range, if it is defined.
125125

126+
NOT IMPLEMENTED YET
127+
126128
##### Character Ranges
127129

128130
- `` `'a'..'e'` ```a b c d e`
129131
- `` `'y'..'t'` ```y x w v u t`
130132
- `` `'a'..'h' t2t` ```a c e g`
131133
- `` `'Y'..'c'` ```` Y Z [ \ ] ^ _ ` a b c ``
132134

135+
NOT IMPLEMENTED YET
136+
133137
#### Range Scoping
134138

135139
##### Global Scope
@@ -160,7 +164,7 @@ If a local scope exceeds the bounds of a given global scope, the program will le
160164
#### Calling Functions
161165

162166
- `f(x).step(3)` - Calls the function 3 times.
163-
- `print(f(x):2)` - Prints the output of the function until 2.
167+
- `print(x:2)` - Prints the output of the function until 2.
164168

165169
In Funktion, any time a function is called with `.step()`, the function will generate the next output over the given time step, moving forward from the first step to also generate the next step when initially called. By default, `.step()` will step one defined time step. If the time step is defined as 2, it will step through the input value by 2. If a function with a time step of 2 calls itself with `.step(2)`, it will step twice, generating twice, stepping through the input value by 4. The local defined time step takes precedence over the global time step for all functions in the given program, which is 1 by default if not defined at the start of the program. The step value need not align with the global or even local time step, but the function will end generation once the end value is either reached or passed.
166170
---
@@ -227,17 +231,17 @@ Program below prints the factorial of 5, which will output `120` after iterating
227231
228232
factorial(x) = ? x > 1 => x * factorial(x - 1) : 1
229233
230-
factorial(x).step(5)
231-
print(x:5)
234+
factorial(x).step(5) // Step through 5 times
235+
print(x:1)
232236
```
233237

234238
**Output:**
235239

236240
```
237-
5
238-
20
239-
60
240-
120
241+
1
242+
2
243+
6
244+
24
241245
120
242246
```
243247

@@ -250,14 +254,17 @@ print(x:5)
250254
251255
f(x) = x
252256
f(x).step(2)
253-
G(x) = number(input("Give me a value and I will multiply that sequence with that value."))
257+
G(x) = input("Give me a value and I will multiply that sequence with that value.")
254258
255-
(f(x) * G(x)).step(2)
256-
print(f(x):5)
259+
h(x) = f(x) * G(x)
260+
h(x).step(2)
261+
print(x:5)
257262
```
258263

259264
**Expected Output:**
260265

266+
User Input: 5
267+
261268
```
262269
1
263270
2
@@ -274,10 +281,13 @@ In this example, `f(x)` generates a sequence of numbers from 1 to 5. When combin
274281
`15..1` t1t
275282
276283
fizzbuzz(x) =
277-
? x % 15 == 0 => print("fizzbuzz"), fizzbuzz(x).step()
278-
? x % 3 == 0 => print("fizz"), fizzbuzz(x).step()
279-
? x % 5 == 0 => print("buzz"), fizzbuzz(x).step()
280-
: print(x), fizzbuzz(x).step()
284+
? x % 15 == 0 => "fizzbuzz":
285+
? x % 3 == 0 => "fizz":
286+
? x % 5 == 0 => "buzz":
287+
x
288+
289+
fizzbuzz(x).step(15)
290+
print(x)
281291
```
282292

283293
**Expected Output:**
@@ -369,50 +379,105 @@ InputStmt(_input, _open, prompt, _close) {
369379
## Generated Code
370380
This language is also capable of generating JavaScript code!
371381
382+
The example codes below are reformatted for readability. The generator will produce these JavaScript code, but without the nice formatting. Note that unecessary and boilerplate code are removed as well.
383+
372384
### Transpiled Function
373-
These functions will always appear in the transpiled JavaScript code regardless what file is being translated. These functions are used as Funktion has completely
385+
These functions will always appear in the transpiled JavaScript code regardless what file is being translated. These functions are used as Funktion and JavaScript differ is expressiveness.
374386
375387
```js
376-
function generateRange(start, end, step) {
377-
const range = [];
378-
if (step === 0) step = 1;
379-
if (start <= end) {
380-
for (let i = start; i <= end; i += step) {
381-
range.push(i);
388+
import { createInterface } from "node:readline/promises";
389+
import { stdin as input, stdout as output } from "node:process";
390+
const rl = createInterface({ input, output });
391+
392+
function generateRange(start = 5, end = 1, step = 1) {
393+
if (end < start) step *= -1;
394+
return {
395+
start,
396+
end,
397+
step
398+
};
399+
}
400+
401+
function initializeMutableRange(timestepRange = generateRange()) {
402+
return {
403+
timestepRange,
404+
values: [],
405+
index: -1,
406+
size: 0
407+
};
408+
}
409+
410+
function getSlice(value, limit) {
411+
const list = []
412+
let index = 0;
413+
if (value.timestepRange.step > 0) {
414+
for (let i = value.timestepRange.start ; i <= limit && i <= value.timestepRange.end ; i += value.timestepRange.step ) {
415+
list.push(value.values[index++]);
382416
}
383-
}
384-
else {
385-
for (let i = start; i >= end; i -= step) {
386-
range.push(i);
417+
} else {
418+
for (let i = value.timestepRange.start ; i >= limit && i >= value.timestepRange.end ; i += value.timestepRange.step ) {
419+
list.push(value.values[index++]);
387420
}
388421
}
389-
return range;
422+
return list;
390423
}
391424

392425
function funktionPrint(value) {
393426
if (Array.isArray(value)) {
394-
console.log(value.join('\\n'));
427+
console.log(value.join('\n'));
428+
}
429+
else if (typeof value === "object") {
430+
console.log(value.values.join('\n'));
395431
}
396432
else {
397433
console.log(value);
398434
}
399435
}
436+
437+
function applyFunction(gen, iterations, f) {
438+
let currentVal = gen.timestepRange.start + gen.timestepRange.step * (gen.index + 1);
439+
if (gen.size === 0) {
440+
gen.size++;
441+
gen.index++;
442+
const result = f(currentVal);
443+
gen.values.push(Array.isArray(result) ? result.join(' ') : result);
444+
currentVal += gen.timestepRange.step;
445+
}
446+
if (gen.timestepRange.step > 0) {
447+
while (currentVal <= gen.timestepRange.end && iterations > 0) {
448+
gen.size++;
449+
gen.index++;
450+
const result = f(currentVal);
451+
gen.values.push(Array.isArray(result) ? result.join(' ') : result);
452+
currentVal += gen.timestepRange.step;
453+
iterations--;
454+
}
455+
} else {
456+
while (currentVal >= gen.timestepRange.end && iterations > 0) {
457+
gen.size++;
458+
gen.index++;
459+
const result = f(currentVal);
460+
gen.values.push(Array.isArray(result) ? result.join(' ') : result);
461+
currentVal += gen.timestepRange.step;
462+
iterations--;
463+
}
464+
}
465+
}
400466
```
401467
402468
### Generated "Hello World!" Code
403469
```js
404-
const globalRange = [];
405470
funktionPrint("Hello, World!");
406471
```
407472
408473
### Generated Factorial Code
474+
The code is reformatted for readability. The generator will produce this code, but without the nice formatting. Note that unecessary and boilerplate code are removed as well.
409475
```js
410-
const globalRange = generateRange(5, 1, 1);
411-
const factorial_1 = [];
412-
let previous_factorial_1 = 1;
413-
for (const x of globalRange) {
414-
factorial_1.push((x * previous_factorial_1));
415-
previous_factorial_1 = factorial_1[factorial_1.length - 1];
416-
}
417-
funktionPrint(factorial_1(x));
476+
function factorial_1(x_2) {
477+
return [( x_2 > 1 ? (x_2 * factorial_1((x_2 - 1))) : 1)];
478+
}
479+
480+
let x_2 = initializeMutableRange();
481+
applyFunction(x_2, 5, factorial_1);
482+
funktionPrint(getSlice(x_2, 1));
418483
```

0 commit comments

Comments
 (0)