Kwa loop should have a range function
Describe the feature
I have noticed that kwa loop works similarly to for loop in Python. The for loop in Python has a range function that enables you to loop through a set of code a number of times. It would also be beneficial to have this feature in Nuru to make the kwa loop even more versatile.
Use Case
To loop through a set of code a specified number of times
Proposed Solution
Kiswahili keywords could be masafa au umbali
Other Information
No response
Acknowledgements
- [ ] I may be able to implement this feature request
- [ ] This feature might incur a breaking change
Version used
v0.2.0
Environment details (OS name and version, etc.)
OS: Linux 6.1 Manjaro Linux
CPU: (8) x64 11th Gen Intel(R) Core(TM) i5-1135G7 @ 2.40GHz
Memory: 6.19 GB / 15.31 GB
Container: Yes
Shell: 5.1.16 - /bin/bash
Alright. Btw we are on v0.5.14.
Cool, I will upgrade, I think the URL in the download curl should be the latest, let me research if that's possible:
No worries, updated it this morning
Currently, if one needs to iterate through a range, a function under the "hesabu" module named list(first, last, interval) can be utilized. The usage example below demonstrates its application to calculate a cosine of a radian:
cos = unda(x) {
fanya result = 1; // Initialize the result
fanya term = 1;
kwa i ktk list(2, 101, 2) {
term = (-term * x * x) / (i * (i - 1));
result += term;
}
rudisha result;
}
In this example, the list function generates a list of numbers with the specified interval between them. The first number is inclusive, while the last number is not included. For instance, the usage above generates a list that looks like [2, 4, 6...100].
Here is the implementation of the list function:
list = unda(first, last, interval){
fanya list = [first];
fanya i = first + interval;
wakati(i < last){
list.sukuma(i);
i += interval;
}
rudisha list;
}
I believe this approach offers a solution for iterating through a specified range in a manner similar to Python's range function, enhancing the versatility of the language.
Please feel free to discuss further or provide any feedback on this proposal. I am eager to collaborate and refine this feature for potential integration into Nuru.
Alright sir, will work on this.
I tried implementing it nikapata I had to update some core functionalities nikaachana nayo ju sijui dependency yake inakaa aje?.
Kama kupass in a callback function in nuru currently unda can't take in another unda as an argument. So hapo singe guza
From: Avicenna @.> Sent: Wednesday, November 22, 2023 7:15:23 PM To: AvicennaJr/Nuru @.> Cc: Victor Kariuki @.>; Comment @.> Subject: Re: [AvicennaJr/Nuru] Kwa loop should have a range function (Issue #69)
Alright sir, will work on this.
— Reply to this email directly, view it on GitHubhttps://github.com/AvicennaJr/Nuru/issues/69#issuecomment-1823072538, or unsubscribehttps://github.com/notifications/unsubscribe-auth/ATTZYIYNDL7EYRFPPHGYACTYFYQJXAVCNFSM6AAAAAA6BZ3BFKVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTQMRTGA3TENJTHA. You are receiving this because you commented.Message ID: @.***>
I tried implementing it nikapata I had to update some core functionalities nikaachana nayo ju sijui dependency yake inakaa aje?. Kama kupass in a callback function in nuru currently unda can't take in another unda as an argument. So hapo singe guza … ________________________________ From: Avicenna @.> Sent: Wednesday, November 22, 2023 7:15:23 PM To: AvicennaJr/Nuru @.> Cc: Victor Kariuki @.>; Comment @.> Subject: Re: [AvicennaJr/Nuru] Kwa loop should have a range function (Issue #69) Alright sir, will work on this. — Reply to this email directly, view it on GitHub<#69 (comment)>, or unsubscribehttps://github.com/notifications/unsubscribe-auth/ATTZYIYNDL7EYRFPPHGYACTYFYQJXAVCNFSM6AAAAAA6BZ3BFKVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTQMRTGA3TENJTHA. You are receiving this because you commented.Message ID: @.***>
I think it will be better to add it into the actual source code (ie in go). I did make an attempt some time back, but got some weird bug back then and left it. Check evaluator.go from line 334:
func evalForExpression(fe *ast.For, env *object.Environment) object.Object {
obj, ok := env.Get(fe.Identifier)
defer func() { // stay safe and not reassign an existing variable
if ok {
env.Set(fe.Identifier, obj)
}
}()
val := Eval(fe.StarterValue, env)
if isError(val) {
return val
}
env.Set(fe.StarterName.Value, val)
// err := Eval(fe.Starter, env)
// if isError(err) {
// return err
// }
for {
evaluated := Eval(fe.Condition, env)
if isError(evaluated) {
return evaluated
}
if !isTruthy(evaluated) {
break
}
res := Eval(fe.Block, env)
if isError(res) {
return res
}
if res.Type() == object.BREAK_OBJ {
break
}
if res.Type() == object.CONTINUE_OBJ {
err := Eval(fe.Closer, env)
if isError(err) {
return err
}
continue
}
if res.Type() == object.RETURN_VALUE_OBJ {
return res
}
err := Eval(fe.Closer, env)
if isError(err) {
return err
}
}
return NULL
}
Okay
Sent from Outlook for Androidhttps://aka.ms/AAb9ysg
From: Avicenna @.> Sent: Friday, November 24, 2023 9:53:52 PM To: AvicennaJr/Nuru @.> Cc: Victor Kariuki @.>; Comment @.> Subject: Re: [AvicennaJr/Nuru] Kwa loop should have a range function (Issue #69)
I tried implementing it nikapata I had to update some core functionalities nikaachana nayo ju sijui dependency yake inakaa aje?. Kama kupass in a callback function in nuru currently unda can't take in another unda as an argument. So hapo singe guza … ________________________________ From: Avicenna @.> Sent: Wednesday, November 22, 2023 7:15:23 PM To: AvicennaJr/Nuru @.> Cc: Victor Kariuki @.>; Comment @.> Subject: Re: [AvicennaJr/Nuru] Kwa loop should have a range function (Issue #69https://github.com/AvicennaJr/Nuru/issues/69) Alright sir, will work on this. — Reply to this email directly, view it on GitHub<#69 (comment)https://github.com/AvicennaJr/Nuru/issues/69#issuecomment-1823072538>, or unsubscribehttps://github.com/notifications/unsubscribe-auth/ATTZYIYNDL7EYRFPPHGYACTYFYQJXAVCNFSM6AAAAAA6BZ3BFKVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTQMRTGA3TENJTHA. You are receiving this because you commented.Message ID: @.***>
I think it will be better to add it into the actual source code (ie in go). I did make an attempt some time back, but got some weird bug back then and left it. Check evaluator.go from line 334:
func evalForExpression(fe *ast.For, env *object.Environment) object.Object { obj, ok := env.Get(fe.Identifier) defer func() { // stay safe and not reassign an existing variable if ok { env.Set(fe.Identifier, obj) } }() val := Eval(fe.StarterValue, env) if isError(val) { return val }
env.Set(fe.StarterName.Value, val)
// err := Eval(fe.Starter, env)
// if isError(err) {
// return err
// }
for {
evaluated := Eval(fe.Condition, env)
if isError(evaluated) {
return evaluated
}
if !isTruthy(evaluated) {
break
}
res := Eval(fe.Block, env)
if isError(res) {
return res
}
if res.Type() == object.BREAK_OBJ {
break
}
if res.Type() == object.CONTINUE_OBJ {
err := Eval(fe.Closer, env)
if isError(err) {
return err
}
continue
}
if res.Type() == object.RETURN_VALUE_OBJ {
return res
}
err := Eval(fe.Closer, env)
if isError(err) {
return err
}
}
return NULL
}
— Reply to this email directly, view it on GitHubhttps://github.com/AvicennaJr/Nuru/issues/69#issuecomment-1825997845, or unsubscribehttps://github.com/notifications/unsubscribe-auth/ATTZYIZX74TBYZHNTOMLXGDYGDUMBAVCNFSM6AAAAAA6BZ3BFKVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTQMRVHE4TOOBUGU. You are receiving this because you commented.Message ID: @.***>
This was added in commit https://github.com/NuruProgramming/Nuru/commit/de38a577bdc1d70db71ccea042e8a80e94d7d8d8