-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Request for Array method #114
Comments
The let int_arr = [ 17, 11, 3, 7, 5, 13 ]
let sum = int_arr.reduce(0, fn(sum : int, item: int) -> sum + item) but when removing the type-hints from the inner function (i.e. just As for the So something like this works: let int_arr : int[] = []
for(i in 0...4) {
int_arr += i
int_arr = int_arr + i // same as above, note: int_arr + i creates a new array
} I also added a int_arr.slice(0, to_remove) + int_arr.slice(to_remove + 1) If you really need a mutable array/list/queue I would rather like to add specific type for that |
As I come from old fashionned procedurals languages (C, ASM 68k, Cobol), even if I have fun with more recent approach, I still struggle to apprehend all subtilities of those. I think you are right, an array should be unmutable. Since it was the only collection, I was thinking about it only as collection, but if it's still in language phylosophy, an other kind of collection, as a list, or a set could be usefull, and it would preserve the unmutability of the array. (Unless unmutability is expected for all kind of collection ^^) Keep in mind that although I knew the existence of functionnal language, Kontrol is my first time encounter - I kinda like it :) - , so my request could be irrelevant : maybe it's on me to learn to work differently.
Thanks for the tips !
When I first used a
As expected :)
Imagine a black cat, all bristly fur, hissing while threatening with his claws. It's me when I code in javascript. The only times I approach this 'thing' is when I only have a highly restricted authority computer which don't allow me to install any sdk. I feel your misgivings. |
This should now be part of 0.5.2.4 const sum = int_arr.reduce(0, fn(sum, item) -> sum + item) also works, though the type-inference certainly needs more testing |
version 0.5.2.4 (Ckan) Need a dumb monkey doing dumb things to nice tools to see if they are dumbproof? Here I am! gitreport::reduce.to2 use { Vessel } from ksp::vessel
fn sumint() -> int = {
const arr_int = [1, 2, 3, 4, 5, 6]
const sum = arr_int.reduce(0, fn(sum, item) -> sum + item)
return sum
}
// Rebooted in 00:00:02.9630349
// No errors
// Doc refer to vessel.part as an array : 'parts | ksp::vessel::Part[] | R/O | Get a list of all vessel parts.'
fn fuel_flow(vessel: Vessel) -> float = {
return vessel.parts
.filter(fn(p) -> p.engine_module.defined)
.map(fn(p) -> p.engine_module.value.max_fuel_flow)
.reduce(0.0, fn(sum, f) -> sum += f)
}
// Rebooted in 00:00:02.5323374
// ERROR: [gitreport\reduce.to2(17, 36)] NoSuchVariable
// Local variable 'sum' is read-only (const)
// Also broke, but less intriging since I use a Range
fn factorial(n: int) -> int = {
return (1...n)
.reduce(1, fn(f, x) -> f *= x)
}
// Rebooted in 00:00:02.6255638
// ERROR: [gitreport\reduce.to2(27, 18)] NoSuchMethod
// Type 'Range' does not have a method or field 'reduce'
// ERROR: [gitreport\reduce.to2(26, 1)] IncompatibleTypes
// Function 'factorial' returns Unit but should return int
// Also broke, even if I trick the compiler converting the Range into an Array
fn factorial(n: int) -> int = {
return (1...n)
.map(fn(x) -> x)
.reduce(1, fn(f, x) -> f *= x)
}
// Rebooted in 00:00:02.6332422
// ERROR: [gitreport\reduce.to2(42, 32)] NoSuchVariable
// Local variable 'f' is read-only (const) Edit : misspelling in comments |
The In your example: If you just replace After all reduce is not a loop: |
Han..! Thank you ! |
Hello again :) |
I had some problems with an optional function parameter, so I added Example:
const sort_asc = struct.arr.sort_by(fn(t) -> t.field1)
|
version 0.5.2.5 (Ckan)
Since it's also about Arrays, I have a question.
I feel (maybe wrongly) than fn foo() -> (int, string)[] = {
const foo = [(0, "zero"), (1, "one"), (2, "two"), (3, "three")]
return foo
.filter(fn((number, letter)) -> number == 2)
}
fn bar() -> (number: int, letter: string)[] = {
const bar = [(number: 0, letter: "zero"), (number: 1, letter: "one"), (number: 2, letter: "two"), (number: 3, letter: "three")]
return bar
.filter(fn(rec) -> rec.number == 2)
} and I get this error from
Is that expected ? Anyway, as you can see, Edit : typo |
Sorry, overlooked this comment. Instead you could do it like this:
|
It happen. And I'm maybe a bit talkative, too ^^ |
This issue is stale because it has been open for 60 days with no activity. |
Hi ! It seems VSC extention have some trouble to understand the new property sync fn group_statics_as_rotaries(staticSolarPanels: Part[]) -> Part[][] = {
if(staticSolarPanels.length < 2)
return []
(0..staticSolarPanels.length)
.flat_map(fn(i) -> ((i + 1)..staticSolarPanels.length).map(fn(j) -> normal_for_partpair(staticSolarPanels[i], staticSolarPanels[j])))
.reduce(<(parts: Part[], normalId: GlobalVector)>[], group_by_normal)
.map(fn(r) -> r.parts)
.filter(exclude_asymetric_enlightenment)
.sort_by(fn(a) -> a.length)
.reverse()
// .reduce(<Part[]>[], deduplicate) // TODO
.reduce(<Part[]>[], fn(clean, partArr) -> {
if(clean.is_empty)
return clean + partArr
return clean + partArr.filter(fn(part) -> !clean.flat_map(fn(a) -> a).exists(fn(p) -> p.position == part.position)) // TODO prefer a part.id than a part.position when it will be available
}) // deduplicate
.filter(fn(a) -> a.length > Array.Empty)
} leads to
on Note : Same struggles happen with Records array as seen on this alert (or is it because it's somewhat matricial with an array in an other ?):
And since I find |
Still on arrays, but other anomaly, about pub struct Logger() {
blackboxes: (vesselUID: string, entries: LogEntry[])[] = []
}
impl Logger {
// some other methods...
fn new_entry(self, entry: LogEntry) -> Unit = {
self.blackboxes = self.blackboxes + (vesselUID: entry.vessel.id, entries: [entry])
}
} Everything goes fine. BUT ! fn new_entry(self, entry: LogEntry) -> Unit = {
self.blackboxes += (vesselUID: entry.vessel.id, entries: [entry])
} I got this error :
|
Arrays are somewhat special and mostly hardcoded ein the vscode extension, I just forgot to add the The |
Should work in 0.5.8.4 |
This issue is stale because it has been open for 60 days with no activity. |
This issue was closed because it has been inactive for 14 days since being marked as stale. |
Hi, it would be wonderfull if Array type implement those two methods :
(or OrderBy and Aggregate as said in .net framework)
And it would be nice we could edit collection, with some kind of
.Add
.Remove
.Kindly,
LeF.
Edit : typo
The text was updated successfully, but these errors were encountered: