I've been using min() and max() user functions all over the place. (like these, and the ones needed in this function)
Mine uses a variadic param and loops through it, however Lexikos has suggested alternate method of passing a single array instead. Really, six one way, half a dozen another.
However I think it would be actually useful to have built in ObjMax() and ObjMin() that return the maximum and minimum values (not indexes) of an object and can be accessible with array-dot notation like: [1,2,3,"abc"].max()
array.max() / array.min()
Started by
infogulch
, Sep 05 2011 08:06 PM
24 replies to this topic
#1
-
Posted 05 September 2011 - 08:06 PM
On a partially related note, I'd also like to see ObjCount() to get the count of objects not using integer keys...
Yes, I'd like to see ObjMin() and friends built in too.
Yes, I'd like to see ObjMin() and friends built in too.
#2
-
Posted 05 September 2011 - 09:00 PM
I like the idea of ObjCount() / myobject.count() to give the count of all keys, not just integer keys.
#3
-
Posted 05 September 2011 - 09:33 PM
Min() and Max() would be handy, Count() even more so, even if just for arrays.
#5
-
Posted 05 September 2011 - 10:43 PM
+1
#6
-
Posted 06 September 2011 - 12:22 AM
I would like to see real-world examples of where these functions would be useful, especially since we're missing other functions which I think would be more commonly used (such as IndexOf/Find). How often does one need to know the number of non-numeric keys?
#7
-
Posted 06 September 2011 - 12:27 AM
just last week I needed the count of all array items. though the script no longer exists....
on the other hand, who would just need the count numeric keys? what about only the string cheese (sorry, couldn't resist.)?
why just limit the users to numbers?
on the other hand, who would just need the count numeric keys? what about only the string cheese (sorry, couldn't resist.)?
why just limit the users to numbers?
#8
-
Posted 06 September 2011 - 02:02 AM
rawr. be very afraid
*poke*
. Populate the AutoHotkey city. Pointless but somewhat fun. .
How about initializing a binary variable to put pointers from each element in an object into just before a for k,v in obj which gets both numeric and non-numeric keys.
#9
-
Posted 06 September 2011 - 02:30 AM
Along the same line of thought, why not limit the users to numbers?why just limit the users to numbers?
All of the practical uses I've had for knowing the number of items in an array have been with items numbered 1..N, and knowing N was more important than knowing the actual number of items in the array (when there may be extra information stored as string key-value pairs which aren't supposed to be part of the array data).
Without sufficient context, I cannot consider that a real-world example.How about initializing a binary variable to put pointers from each element in an object ...
#10
-
Posted 06 September 2011 - 03:39 AM
I tend to agree with Lexikos that an integrated IndexOf() function would be more important.
If one wants to extend on this, something in the direction of query syntax like LINQ in C# would be very nice. Or maybe even just supporting finding the index of an object that has a key with a specific value.
If one wants to extend on this, something in the direction of query syntax like LINQ in C# would be very nice. Or maybe even just supporting finding the index of an object that has a key with a specific value.
#11
-
Posted 06 September 2011 - 01:49 PM
Hmm, I think IndexOf() / Find() for objects is important but I also think that ObjCount() is more generically important.
There have been several times when it would have been nice to have the count of non-numeric keys, which could be found by: obj.count() - (obj.maxindex() OR 0) (v2) as long as there are actually maxindex() numeric keys.
Along those lines, ObjCount() would give essential information about objects with sparsely populated numeric keys: {10: "abc", 100: "def", 1000: "ghi", 10000: "jkl"}
One sees throughout the forums loop % obj.maxindex() (me included) which in the case of the object above, would make the code iterate 9996 more times than necessary, not only being detrimental to performance but also possibly yielding crazy results because all those keys that don't exist would unexpectedly be blank. ObjCount() would give us a tool to find these cases and code around them.
There have been several times when it would have been nice to have the count of non-numeric keys, which could be found by: obj.count() - (obj.maxindex() OR 0) (v2) as long as there are actually maxindex() numeric keys.
Along those lines, ObjCount() would give essential information about objects with sparsely populated numeric keys: {10: "abc", 100: "def", 1000: "ghi", 10000: "jkl"}
One sees throughout the forums loop % obj.maxindex() (me included) which in the case of the object above, would make the code iterate 9996 more times than necessary, not only being detrimental to performance but also possibly yielding crazy results because all those keys that don't exist would unexpectedly be blank. ObjCount() would give us a tool to find these cases and code around them.
#12
-
Posted 06 September 2011 - 03:59 PM
In most cases it is possible to use ObjGetCapacity() to get the number of keys, but when an object is explicitly resized using ObjSetCapacity() scripts may fail. In my tests objects automatically expand just enough to hold the additional keys added but I am not sure about larger objects, and depending on this behavior is probably not very forward compatible.
Are sizes stored in objects? If so, ObjCount() potentially has a large performance benefit over creating an enumeration and iterating through it.
Are sizes stored in objects? If so, ObjCount() potentially has a large performance benefit over creating an enumeration and iterating through it.
#13
-
Posted 06 September 2011 - 07:56 PM
On the contrary - if you don't shrink the object to its minimum required capacity (i.e. the number of valid key-value pairs) first, there's likely to be excess. Objects usually start with a capacity of 4 and double in capacity each time more is needed, but this can vary depending on how the object is populated.In most cases it is possible to use ObjGetCapacity() to get the number of keys, but when an object is explicitly resized using ObjSetCapacity() scripts may fail.
SetCapacity: "If less than the current number of key-value pairs, the object is shrunk to fit." So if you do SetCapacity(0) then GetCapacity(), you should be safe.... and depending on this behavior is probably not very forward compatible.
Yes, which also means that Count() would be very simple.Are sizes stored in objects?
OTOH, it would be easier to just use a for-loop in the first place. I generally only use Loop % obj.MaxIndex() when I'm aware that either there are non-numeric keys I want to avoid, or the array is sparsely populated but I still want to iterate once per possible index (not per actual index).ObjCount() would give us a tool to find these cases and code around them.
Enough about Count(). What about Min() and Max()?
Searching for a value: Considering how MinIndex() and MaxIndex() define an "index", IndexOf() might be inappropriate. I might go with KeyOf(), since Find() is a bit too generic. However, "KeyOf" seems a bit unconventional. I did some searching, and PHP's array_search() was the only equivalent function I could find, although I found a few languages with methods like ContainsValue(). :?
#14
-
Posted 06 September 2011 - 11:13 PM
Old topic, I know.
For a search function, I like "Contains". It's similar to ahk B/L's if var contains convention and would bring similar functionality to v2 where all non-expression if's have been removed.
I'm currently using these for Min() and Max() as standalone functions. They allow both syntaxes: min/max of arg values AND min/max of object values. I.e. if only one arg is passed and the one arg is an object, then it returns the max value of the object.
I'm sure if they were built-in they would be significantly faster, but perhaps this or something similar could be included in the stdlib.
For a search function, I like "Contains". It's similar to ahk B/L's if var contains convention and would bring similar functionality to v2 where all non-expression if's have been removed.
I'm currently using these for Min() and Max() as standalone functions. They allow both syntaxes: min/max of arg values AND min/max of object values. I.e. if only one arg is passed and the one arg is an object, then it returns the max value of the object.
min(n, x*) { if ObjMaxIndex(x) == 0 && IsObject(n) x := n, x._NewEnum().next(k,n) for k,v in x if (v < n) n := v return n } max(n, x*) { if ObjMaxIndex(x) == 0 && IsObject(n) x := n, x._NewEnum().next(k,n) for k,v in x if (v > n) n := v return n }
I'm sure if they were built-in they would be significantly faster, but perhaps this or something similar could be included in the stdlib.
#15
-
Posted 04 January 2012 - 10:06 PM