www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - python like datastructures as databases code examples

reply monkyyy <crazymonkyyy gmail.com> writes:
`shapes.filter(isnt:shapeenum.isstatic)`
https://forum.dlang.org/thread/apbcqxiifbsqdlrsluol forum.dlang.org

I know its possible to make complex, datastructure aware filters, 
but I never done it

what patterns do people use? lets say you have an always sorted 
array and the user is asking for values between 100 and 200 
`array.filter(above:100,below:200)`; obviously possible but maybe 
theres some details that will make my life easier

how yall implement such things?
Jul 18
parent reply Serg Gini <kornburn yandex.ru> writes:
On Friday, 18 July 2025 at 18:35:40 UTC, monkyyy wrote:
 `shapes.filter(isnt:shapeenum.isstatic)`
 https://forum.dlang.org/thread/apbcqxiifbsqdlrsluol forum.dlang.org

 I know its possible to make complex, datastructure aware 
 filters, but I never done it

 what patterns do people use? lets say you have an always sorted 
 array and the user is asking for values between 100 and 200 
 `array.filter(above:100,below:200)`; obviously possible but 
 maybe theres some details that will make my life easier

 how yall implement such things?
```d bool between(T)(T element, T before, T after) { return (before < element) & (element < after); } void main() { auto arr = [1,2,4,6,7,23,3,7,8,23,1,7,8]; writeln(arr.filter!(a => between(a,5,9))); } ```
Jul 21
parent reply Monkyyy <crazymonkyyy gmail.com> writes:
On Monday, 21 July 2025 at 08:31:42 UTC, Serg Gini wrote:
 On Friday, 18 July 2025 at 18:35:40 UTC, monkyyy wrote:
 `shapes.filter(isnt:shapeenum.isstatic)`
 https://forum.dlang.org/thread/apbcqxiifbsqdlrsluol forum.dlang.org

 I know its possible to make complex, datastructure aware 
 filters, but I never done it

 what patterns do people use? lets say you have an always 
 sorted array and the user is asking for values between 100 and 
 200 `array.filter(above:100,below:200)`; obviously possible 
 but maybe theres some details that will make my life easier

 how yall implement such things?
```d bool between(T)(T element, T before, T after) { return (before < element) & (element < after); } void main() { auto arr = [1,2,4,6,7,23,3,7,8,23,1,7,8]; writeln(arr.filter!(a => between(a,5,9))); } ```
That filter is not aware of the data structure nor is that array maintaining a sort
Jul 21
parent reply Serg Gini <kornburn yandex.ru> writes:
On Monday, 21 July 2025 at 14:15:07 UTC, Monkyyy wrote:
 On Monday, 21 July 2025 at 08:31:42 UTC, Serg Gini wrote:
 That filter is not aware of the data structure nor is that 
 array maintaining a sort
I'm not sure what you are trying to do. But array doesn't look right.. If you want a balanced ("always sorted") structure with "filter" (ability to make some requests for the data) - this looks more like some Tree structure
Jul 21
parent reply monkyyy <crazymonkyyy gmail.com> writes:
On Monday, 21 July 2025 at 14:56:41 UTC, Serg Gini wrote:
 But array doesn't look right..
 If you want a balanced ("always sorted") structure with 
 "filter" (ability to make some requests for the data) - this 
 looks more like some Tree structure
Your thinking in classical theory and textbook read`n; practice has no such restriction ```d struct sortedarray(A){ A array; bool issorted; void opOpAssign(string s:"~",T)(T t){ issorted=false; array~=t; } alias get this; ref get(){ if( ! issorted){ import std; array.sort; } return array; }} unittest{ sortedarray!(int[]) array; array~=[1,4,5,7,8,2]; import std; array.writeln; } ```
 I'm not sure what you are trying to do.
syntax test, concept generation, gather'n pieces
Jul 21
parent reply Serg Gini <kornburn yandex.ru> writes:
On Monday, 21 July 2025 at 16:13:44 UTC, monkyyy wrote:
 On Monday, 21 July 2025 at 14:56:41 UTC, Serg Gini wrote:
 But array doesn't look right..
 If you want a balanced ("always sorted") structure with 
 "filter" (ability to make some requests for the data) - this 
 looks more like some Tree structure
Your thinking in classical theory and textbook read`n; practice has no such restriction
If performance is not required - of course you can sort every time when you appending new elements. classical theory and textbooks were created to do that in a not wacky way.
Jul 22
parent monkyyy <crazymonkyyy gmail.com> writes:
On Tuesday, 22 July 2025 at 07:32:00 UTC, Serg Gini wrote:
 On Monday, 21 July 2025 at 16:13:44 UTC, monkyyy wrote:
 On Monday, 21 July 2025 at 14:56:41 UTC, Serg Gini wrote:
 But array doesn't look right..
 If you want a balanced ("always sorted") structure with 
 "filter" (ability to make some requests for the data) - this 
 looks more like some Tree structure
Your thinking in classical theory and textbook read`n; practice has no such restriction
If performance is not required - of course you can sort every time when you appending new elements. classical theory and textbooks were created to do that in a not wacky way.
 wacky
This is simpler then trees, by allot; its a bool of overhead and 1 piece of hidden control flow. Of all the things Ive posted, this isnt wacky. If you add n elements then iterate on the data in a sensible way, big O will be the same as most trees while being better at using real hardware, theory only gets you more promises around random or hostile usage patterns. Then consider the upgrade to radix sort, or grabbing some other statistic
Jul 22