closure for struct of structs

16 views
Skip to first unread message

Trip

unread,
Jun 4, 2020, 11:14:38 PM6/4/20
to framework-one
Hey there, 

I have a struct of structures - 

Modifiers = {}
modifiers.mod1 = {}
modifiers.mod1.isActive = 1;
modifiers.mod2 = {}
modifiers.mod2.isActive = 1;
modifiers.mod3 = {}
modifiers.mod3.isActive = 0;

There are actually about 20 structs though, and I am trying to write a closure that will loop over the struct with a function that will return all children structs where the key isActive is true. I can write this with a couple of loops but I know this can be done with either .filter or .each. I believe where I am stuck is the return on the closure has to be a single compassion.

here is what I have but the error is return type for filter is incorrect. If anyone has an example of a closure for a struct of structs or any hints, it would be greatly appreciated.

function filterMyStruct(struct afunction filter) {
        
        cfloop(collection=a , item="modifier"){
            // writeDump(var=a[modifier], label='label', top='5');abort;
            a[modifier].filterfunctionkv ) {
                if (k IS 'isActive' AND v IS TRUE) {
                     return true;
                } 
               
              } )

        }
        
    }

Shane Pitts

unread,
Jun 4, 2020, 11:52:57 PM6/4/20
to framew...@googlegroups.com
Probably because if the field doesn’t match, you aren’t returning anything from the function. It needs to either return true or false?

Try getting rid of the if statement and just 

return IS 'isActive'  AND IS TRUE

Shane Pitts

unread,
Jun 4, 2020, 11:56:08 PM6/4/20
to framew...@googlegroups.com
Also, if it’s a struct of structs do you need to use structFilter instead? It’s been a while since I’ve done any CF 
--
FW/1 documentation: http://framework-one.github.io
FW/1 source code: http://github.com/framework-one/fw1
FW/1 chat / support: https://gitter.im/framework-one/fw1
FW/1 mailing list: http://groups.google.com/group/framework-one
---
You received this message because you are subscribed to the Google Groups "framework-one" group.
To unsubscribe from this group and stop receiving emails from it, send an email to framework-on...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/framework-one/86bd3969-a485-4bc7-93bc-3f6d7cb24946o%40googlegroups.com.

Trip

unread,
Jun 5, 2020, 12:23:15 AM6/5/20
to framework-one
yup that got me through it, but i believe this is where my closure knowledge is lacking . . .  that works and will return true, but i want the method to return all the structs which have the kei is active true. So intuition says i need to do a structAppend because in a loop but it feels like i am defeating the purpose of the filter.

function filterMyStruct(struct afunction filter) {
        local.myRtn = {};
        
        cfloop(collection=a , item="modifier"){
            // writeDump(var=a[modifier], label='label', top='5');abort;
            a[modifier].filterfunctionkv ) {
                return k IS 'isActive' AND v IS TRUE;
              } )

        }
        return local.myRtn;
        
    }

Trip

unread,
Jun 5, 2020, 12:26:03 AM6/5/20
to framework-one
yes, I could use the StructFilter method as well, but opted for the member function. I could be wrong but i think they do they do the same thing.
To unsubscribe from this group and stop receiving emails from it, send an email to framew...@googlegroups.com.

Trip

unread,
Jun 5, 2020, 12:32:35 AM6/5/20
to framework-one
I also could be missing the boat completely and trying to filter isn't the right way to select structs where one key has a certain value.

Shane Pitts

unread,
Jun 5, 2020, 12:48:40 AM6/5/20
to framew...@googlegroups.com
How about 

myNewMap=myStruct.map(function(key,value){
 return value.isActive is 1;
});
To unsubscribe from this group and stop receiving emails from it, send an email to framework-on...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/framework-one/940c2c0f-3ae6-4347-aa16-fdcee29fc6b2o%40googlegroups.com.

Shane Pitts

unread,
Jun 5, 2020, 1:03:19 AM6/5/20
to framew...@googlegroups.com
This is what you get with me trying to help looking at docs on my phone at 11 at night :). the map will just set all the outer keys to true or false

Try

Modifiers = {}
modifiers.mod1 = {}
modifiers.mod1.name = 'foo';
modifiers.mod1.isActive = 1;
modifiers.mod2 = {}
modifiers.mod2.name = 'boo';
modifiers.mod2.isActive = 1;
modifiers.mod3 = {}
modifiers.mod3.name = 'bar';
modifiers.mod3.isActive = 0;

filteredModifiers = Modifiers.filter(function(mod){
return mod.isActiv == 1;
})

writeDump(filteredModifiers);

Trip

unread,
Jun 5, 2020, 1:12:08 AM6/5/20
to framework-one
yea, it is 1 am here . . i'm on it. getting a Invalid CFML construct on filteredModifiers = Modifiers.filter(function(mod

I trying to figure it out . . .cant beleive you are helping on a phone . . lol

Shane Pitts

unread,
Jun 5, 2020, 1:17:32 AM6/5/20
to framew...@googlegroups.com
Hmm odd. I don’t have a server setup that I can compile and run anything on, but that basically came from adobes docs https://coldfusion.adobe.com/2017/10/map-reduce-and-filter-functions-in-coldfusion/
To unsubscribe from this group and stop receiving emails from it, send an email to framework-on...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/framework-one/239a981c-82aa-4e70-b397-422fdc1e9f28o%40googlegroups.com.

Trip

unread,
Jun 5, 2020, 1:19:36 AM6/5/20
to framework-one
duuuude, you did it! I knew i was overthinking it - par for the course! I read the docs on map and couldn't seem to apply it to struct of structs.Totally appreciate the squinting and working through that! Happy Friday too

here's the final

myModifiers = {}
        myModifiers.mod1 = {}
        myModifiers.mod1.name = 'foo';
        myModifiers.mod1.isActive = 1;
        myModifiers.mod2 = {}
        myModifiers.mod2.name = 'boo';
        myModifiers.mod2.isActive = 1;
        myModifiers.mod3 = {}
        myModifiers.mod3.name = 'bar';
        myModifiers.mod3.isActive = 0;

        filteredModifiers = myModifiers.filter(function(modt) {
            return myModifiers[modt].isActive == 1;
        })
        writeDump(filteredModifiers);abort;

On Friday, June 5, 2020 at 1:03:19 AM UTC-4, shane wrote:

Shane Pitts

unread,
Jun 5, 2020, 1:20:05 AM6/5/20
to framew...@googlegroups.com
That’s probably array syntax. You probably need to pass in key, value and then return value.isActive == 1
To unsubscribe from this group and stop receiving emails from it, send an email to framework-on...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/framework-one/239a981c-82aa-4e70-b397-422fdc1e9f28o%40googlegroups.com.

Trip

unread,
Jun 5, 2020, 1:20:39 AM6/5/20
to framework-one
as an aside, the invalid construct was because CF has a function called mod - so reserved word.

Trip

unread,
Jun 5, 2020, 1:25:32 AM6/5/20
to framework-one
it was probably because it is 1:30, but when i read they were all simple values, i should have realized myModifiers.filter(function(modt) - modt is just the child struct

Trip

unread,
Jun 5, 2020, 1:27:59 AM6/5/20
to framework-one
not it worked, on the return line i just needed to reference the full struct. i used [ ] because modt is eq to the key name in the child struct
Reply all
Reply to author
Forward
0 new messages