Lasso 9 Conditional for Existence in Nested Maps & Arrays

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Lasso 9 Conditional for Existence in Nested Maps & Arrays

Justin Dennis-3
Hi all -

http://stackoverflow.com/questions/25163954/lasso-9-conditional-for-existence-in-nested-maps-arrays

I frequently run into a situation where I am testing for the existence of a
value in a nested map or array. To prevent verbose conditionals, I would
like to simplify the code to not test for existence at each level of the
node, instead going right after what I want.

For example:

    local(mymap = map('a' = (:1,2,3), 'b' = (:4,5,6)))

    if (#mymap->find('c')->contains(9) ) => {}

If key 'c' does not exist in #mymap, then the contains() method throws an
error.

Would it be foolish of me to define this in Lasso Startup?

    define void->contains(...) => false

That would allow the above conditional to work, without having to add
compound expressions to first test if 'c' exists. Am I missing some
unintended consequences? Am I overlooking a more efficient way to do this?

Thanks in advance.

- Justin
#############################################################
Attend the Lasso Developer Conference 2014!
October 1-3, 2014 at Treefrog HQ, Newmarket, Ontario, Canada
http://www.lassosoft.com/LDC-newmarket-2014

#############################################################

This message is sent to you because you are subscribed to
  the mailing list Lasso [hidden email]
Official list archives available at http://www.lassotalk.com
To unsubscribe, E-mail to: <[hidden email]>
Send administrative queries to  <[hidden email]>
Reply | Threaded
Open this post in threaded view
|

Re: Lasso 9 Conditional for Existence in Nested Maps & Arrays

Jonathan Guthrie-3
Hi Justin

Try:

local(mymap = map('a' = (:1,2,3), 'b' = (:4,5,6)))
protect => {
        if(#mymap->find('c') >> 9 ) => { '...' }
}

And you can throw a handle_error container in to do something else if it's not found :)
protect => {
        handle_error => {^
                not #mymap->keys >> 'c' ? 'This is an error!'
        ^}
        if(#mymap->find('c') >> 9 ) => { '...' }
}



On Aug 6, 2014, at 11:11 AM, Justin Dennis <[hidden email]> wrote:

> Hi all -
>
> http://stackoverflow.com/questions/25163954/lasso-9-conditional-for-existence-in-nested-maps-arrays
>
> I frequently run into a situation where I am testing for the existence of a
> value in a nested map or array. To prevent verbose conditionals, I would
> like to simplify the code to not test for existence at each level of the
> node, instead going right after what I want.
>
> For example:
>
>    local(mymap = map('a' = (:1,2,3), 'b' = (:4,5,6)))
>
>    if (#mymap->find('c')->contains(9) ) => {}
>
> If key 'c' does not exist in #mymap, then the contains() method throws an
> error.
>
> Would it be foolish of me to define this in Lasso Startup?
>
>    define void->contains(...) => false
>
> That would allow the above conditional to work, without having to add
> compound expressions to first test if 'c' exists. Am I missing some
> unintended consequences? Am I overlooking a more efficient way to do this?
>
> Thanks in advance.
>
> - Justin

Jono

----------------------------
Jonathan Guthrie
[hidden email]
@iamjono
LassoSoft Inc.
AIM Chatroom: lassochat

#############################################################
Attend the Lasso Developer Conference 2014!
October 1-3, 2014 at Treefrog HQ, Newmarket, Ontario, Canada
http://www.lassosoft.com/LDC-newmarket-2014

#############################################################

This message is sent to you because you are subscribed to
  the mailing list Lasso [hidden email]
Official list archives available at http://www.lassotalk.com
To unsubscribe, E-mail to: <[hidden email]>
Send administrative queries to  <[hidden email]>
Reply | Threaded
Open this post in threaded view
|

Re: Lasso 9 Conditional for Existence in Nested Maps & Arrays

Brad Lindsay
Another way to go about this is to use an or:

if((#mymap->find('c') || (:)) >> 9) => {}

What happens here is that if #mymap->find('c') produces a non-false
value, it's used for the contains, otherwise the empty staticarray is
use for the contains.

HTH,
Brad


On 8/6/14, 11:20 AM, Jonathan Guthrie wrote:

> Hi Justin
>
> Try:
>
> local(mymap = map('a' = (:1,2,3), 'b' = (:4,5,6)))
> protect =>  {
> if(#mymap->find('c')>>  9 ) =>  { '...' }
> }
>
> And you can throw a handle_error container in to do something else if it's not found :)
> protect =>  {
> handle_error =>  {^
> not #mymap->keys>>  'c' ? 'This is an error!'
> ^}
> if(#mymap->find('c')>>  9 ) =>  { '...' }
> }
>
>
>
> On Aug 6, 2014, at 11:11 AM, Justin Dennis<[hidden email]>  wrote:
>
>> Hi all -
>>
>> http://stackoverflow.com/questions/25163954/lasso-9-conditional-for-existence-in-nested-maps-arrays
>>
>> I frequently run into a situation where I am testing for the existence of a
>> value in a nested map or array. To prevent verbose conditionals, I would
>> like to simplify the code to not test for existence at each level of the
>> node, instead going right after what I want.
>>
>> For example:
>>
>>     local(mymap = map('a' = (:1,2,3), 'b' = (:4,5,6)))
>>
>>     if (#mymap->find('c')->contains(9) ) =>  {}
>>
>> If key 'c' does not exist in #mymap, then the contains() method throws an
>> error.
>>
>> Would it be foolish of me to define this in Lasso Startup?
>>
>>     define void->contains(...) =>  false
>>
>> That would allow the above conditional to work, without having to add
>> compound expressions to first test if 'c' exists. Am I missing some
>> unintended consequences? Am I overlooking a more efficient way to do this?
>>
>> Thanks in advance.
>>
>> - Justin
>
> Jono
>
> ----------------------------
> Jonathan Guthrie
> [hidden email]
> @iamjono
> LassoSoft Inc.
> AIM Chatroom: lassochat
>
> #############################################################
> Attend the Lasso Developer Conference 2014!
> October 1-3, 2014 at Treefrog HQ, Newmarket, Ontario, Canada
> http://www.lassosoft.com/LDC-newmarket-2014
>
> #############################################################
>
> This message is sent to you because you are subscribed to
>    the mailing list Lasso [hidden email]
> Official list archives available at http://www.lassotalk.com
> To unsubscribe, E-mail to:<[hidden email]>
> Send administrative queries to<[hidden email]>
#############################################################
Attend the Lasso Developer Conference 2014!
October 1-3, 2014 at Treefrog HQ, Newmarket, Ontario, Canada
http://www.lassosoft.com/LDC-newmarket-2014

#############################################################

This message is sent to you because you are subscribed to
  the mailing list Lasso [hidden email]
Official list archives available at http://www.lassotalk.com
To unsubscribe, E-mail to: <[hidden email]>
Send administrative queries to  <[hidden email]>
Reply | Threaded
Open this post in threaded view
|

Re: Lasso 9 Conditional for Existence in Nested Maps & Arrays

Bil Corry-3
In reply to this post by Justin Dennis-3
I do it this way (seek looks for the item, seeki looks and inserts it if
not found):

[
/*
define map->seek(key::string,default) => {
.keys !>> #key ? return(#default)
return(.find(#key))
}

define map->seeki(key::string,default) => {
.keys !>> #key ? .insert(#key = #default)
return(.find(#key))
}
*/

local(mymap = map('a' = array(1,2,3), 'b' = array(4,5,6)));

if (#mymap->seek('a',array)->contains(4) );
'a contains 4';
/if;

if (#mymap->seek('b',array)->contains(6) );
'b contains 6';
/if;

if (#mymap->seek('c',array)->contains(9) );
'c contains 9<br>';
/if;

if (#mymap->seeki('d',array)->contains(12) );
'd contains 12<br>';
/if;

'<br>';
#mymap;

]


returns:

    b contains 6
    map(a = array(1, 2, 3), b = array(4, 5, 6), d = array())


- Bil



On Wed, Aug 6, 2014 at 5:11 PM, Justin Dennis <[hidden email]> wrote:

> Hi all -
>
>
> http://stackoverflow.com/questions/25163954/lasso-9-conditional-for-existence-in-nested-maps-arrays
>
> I frequently run into a situation where I am testing for the existence of a
> value in a nested map or array. To prevent verbose conditionals, I would
> like to simplify the code to not test for existence at each level of the
> node, instead going right after what I want.
>
> For example:
>
>     local(mymap = map('a' = (:1,2,3), 'b' = (:4,5,6)))
>
>     if (#mymap->find('c')->contains(9) ) => {}
>
> If key 'c' does not exist in #mymap, then the contains() method throws an
> error.
>
> Would it be foolish of me to define this in Lasso Startup?
>
>     define void->contains(...) => false
>
> That would allow the above conditional to work, without having to add
> compound expressions to first test if 'c' exists. Am I missing some
> unintended consequences? Am I overlooking a more efficient way to do this?
>
> Thanks in advance.
>
> - Justin
> #############################################################
> Attend the Lasso Developer Conference 2014!
> October 1-3, 2014 at Treefrog HQ, Newmarket, Ontario, Canada
> http://www.lassosoft.com/LDC-newmarket-2014
>
> #############################################################
>
> This message is sent to you because you are subscribed to
>   the mailing list Lasso [hidden email]
> Official list archives available at http://www.lassotalk.com
> To unsubscribe, E-mail to: <[hidden email]>
> Send administrative queries to  <[hidden email]>
>
#############################################################
Attend the Lasso Developer Conference 2014!
October 1-3, 2014 at Treefrog HQ, Newmarket, Ontario, Canada
http://www.lassosoft.com/LDC-newmarket-2014

#############################################################

This message is sent to you because you are subscribed to
  the mailing list Lasso [hidden email]
Official list archives available at http://www.lassotalk.com
To unsubscribe, E-mail to: <[hidden email]>
Send administrative queries to  <[hidden email]>
Reply | Threaded
Open this post in threaded view
|

Re: Lasso 9 Conditional for Existence in Nested Maps & Arrays

Ke Carlton-3
In reply to this post by Brad Lindsay
+1 for this; I would take that approach if I didn't need to use the data.

Otherwise I would do this (which is also faster):

    if( local(c) := #mymap->find('c') && #c >> 9 ) => { /* do something
with #c */}

Another example:

    local(mymap = map('a' = map('x'=1,'y'=2,'z'=3), 'b' =
map('x'=4,'y'=5,'z'=6)))

    if( local(b) := #mymap->find(b') && #b >> 'z' ) => { /* do something
with #b */ }

Ke

On 7 August 2014 03:32, Brad Lindsay <[hidden email]> wrote:
>
>
> if((#mymap->find('c') || (:)) >> 9) => {}
>
>
#############################################################
Attend the Lasso Developer Conference 2014!
October 1-3, 2014 at Treefrog HQ, Newmarket, Ontario, Canada
http://www.lassosoft.com/LDC-newmarket-2014

#############################################################

This message is sent to you because you are subscribed to
  the mailing list Lasso [hidden email]
Official list archives available at http://www.lassotalk.com
To unsubscribe, E-mail to: <[hidden email]>
Send administrative queries to  <[hidden email]>