Allowing null -> value to work

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

Allowing null -> value to work

Jolle Carlestam-3
One good thing about maps are that when you do
        #mymap -> find('key')
it will always work. Not always return a value, but it won't trow a hissyfit regardless if the key is found.
If you want to do the same with a staticarray or array the code gets much more complicated
        #myarray -> find('key') -> first -> value
will only work if the key is found. If not it will cause an error:
Definition Not Found: null->value()

Thus it would be great with a way to convert say a tie or a staticarray to a map with one swift stroke.
Or to allow null->value() to work, and return a void.

Wonder if that could have drawbacks?

HDB
Jolle

#############################################################
This message is sent to you because you are subscribed to
  the mailing list Lasso
[hidden email]
To unsubscribe, E-mail to: <[hidden email]>
Send administrative queries to  <[hidden email]>
Reply | Threaded
Open this post in threaded view
|

Re: Allowing null -> value to work

Bil Corry-3
Converting to a map will mean having to iterate through every item in the
array then inserting it into a map - it won't be fast.

Altering null->value may cause problems if someone wrote code that expects
an error, especially true if the underlying core Lasso features rely on
this behavior.

My suggestion is to add a new method to the array type that does what you
want, such as:

define array->findpair(key) => {
> protect
> return(.find(#key)->first->value)
> /protect
> return(void)
> }
> local(a) = array(1,date,'b'=1,'c'=3)
> '<br>find = '; #a->find('b')->first->value;
> '<br>findpair = ';#a->findpair('b');




- Bil



On Fri, Oct 11, 2013 at 11:45 PM, Jolle Carlestam <[hidden email]
> wrote:

> One good thing about maps are that when you do
>         #mymap -> find('key')
> it will always work. Not always return a value, but it won't trow a
> hissyfit regardless if the key is found.
> If you want to do the same with a staticarray or array the code gets much
> more complicated
>         #myarray -> find('key') -> first -> value
> will only work if the key is found. If not it will cause an error:
> Definition Not Found: null->value()
>
> Thus it would be great with a way to convert say a tie or a staticarray to
> a map with one swift stroke.
> Or to allow null->value() to work, and return a void.
>
> Wonder if that could have drawbacks?
>
> HDB
> Jolle
>
> #############################################################
> This message is sent to you because you are subscribed to
>   the mailing list Lasso
> [hidden email]
> To unsubscribe, E-mail to: <[hidden email]>
> Send administrative queries to  <[hidden email]>
>

#############################################################
This message is sent to you because you are subscribed to
  the mailing list Lasso
[hidden email]
To unsubscribe, E-mail to: <[hidden email]>
Send administrative queries to  <[hidden email]>
Reply | Threaded
Open this post in threaded view
|

Re: Allowing null -> value to work

Steve Upton
In reply to this post by Jolle Carlestam-3

On Oct 11, 2013, at 2:45 PM, Jolle Carlestam <[hidden email]> wrote:

> Thus it would be great with a way to convert say a tie or a staticarray to a map with one swift stroke.
> Or to allow null->value() to work, and return a void.
>

I've always wanted a better failing behavior than what's available today. I mean, stringing together a bunch of thing->thing('find')->thing->value functions is great until one of them fails and the rest are "errors". In order to have the great-looking code you have to surround it with a bunch of checking… it *really* reduces the value.

> Wonder if that could have drawbacks?

Well, one worth mentioning is that an array converted into a map can only have unique values (or at least *will* only have unique key values once it's done).

regards,

Steve


#############################################################
This message is sent to you because you are subscribed to
  the mailing list Lasso
[hidden email]
To unsubscribe, E-mail to: <[hidden email]>
Send administrative queries to  <[hidden email]>
Reply | Threaded
Open this post in threaded view
|

Re: Allowing null -> value to work

Bil Corry-3
That "failing behavior" is needed to prevent coding errors - you don't want
it silently failing.  I think the issue is that you don't know other ways
to more easily achieve the results you want.

For example, I think you're doing something like this:

local(a) = array('b' = array(1,2,3),'c' = array (4,5,6),'c' = array(7,8,9))

> local(co) = '(c) value if not found';
> local(c) = #a->find('c');
> if(#c->size > 1);
> #c = #c->get(2);
> if(#c->type == 'pair');
> #c = #c->value;
> if(#c->size > 2);
> #co = #c->get(3);
> /if;
> /if;
> /if;
> local(do) = '(d) value if not found';
> local(d) = #a->find('d');
> if(#d->size > 1);
> #d = #d->get(2);
> if(#d->type == 'pair');
> #d = #d->value;
> if(#d->size > 2);
> #do = #d->get(3);
> /if;
> /if;
> /if;
> #co;'<br>';#do;



But you can simplify that into this, which is entirely workable:

local(a) = array('b' = array(1,2,3),'c' = array (4,5,6),'c' = array(7,8,9))
> local(c) = '(c) value if not found';
> local(d) = '(d) value if not found';
> protect; #c = #a->find('c')->get(2)->value->get(3); /protect;
> protect; #d = #a->find('d')->get(2)->value->get(3); /protect;
> #c;'<br>';#d;



That said, the great thing about Lasso is that it's highly extensible.  If
you want different behavior, you can create your own data type that has the
properties, methods, and behaviors you want.


- Bil


On Wed, Oct 16, 2013 at 9:13 PM, Steve Upton <[hidden email]> wrote:

>
> On Oct 11, 2013, at 2:45 PM, Jolle Carlestam <[hidden email]>
> wrote:
>
> > Thus it would be great with a way to convert say a tie or a staticarray
> to a map with one swift stroke.
> > Or to allow null->value() to work, and return a void.
> >
>
> I've always wanted a better failing behavior than what's available today.
> I mean, stringing together a bunch of thing->thing('find')->thing->value
> functions is great until one of them fails and the rest are "errors". In
> order to have the great-looking code you have to surround it with a bunch
> of checking… it *really* reduces the value.
>
> > Wonder if that could have drawbacks?
>
> Well, one worth mentioning is that an array converted into a map can only
> have unique values (or at least *will* only have unique key values once
> it's done).
>
> regards,
>
> Steve
>
>
> #############################################################
> This message is sent to you because you are subscribed to
>   the mailing list Lasso
> [hidden email]
> To unsubscribe, E-mail to: <[hidden email]>
> Send administrative queries to  <[hidden email]>
>

#############################################################
This message is sent to you because you are subscribed to
  the mailing list Lasso
[hidden email]
To unsubscribe, E-mail to: <[hidden email]>
Send administrative queries to  <[hidden email]>
Reply | Threaded
Open this post in threaded view
|

Re: Allowing null -> value to work

Ke Carlton-3
In reply to this post by Bil Corry-3
Agreed. Extended null with a ->value method does not make much sense in
this situation.

I've used the below in the past for staticarrays — mainly cause they're
fast:

define staticarray->findfirstvalue(key::any) => {
local(r) = .find(#key)->first
#r->isa(::pair) ? return #r->second
return #r
}

define staticarray->findlastvalue(key::any) => {
local(r) = .find(#key)->last
#r->isa(::pair) ? return #r->second
return #r
}

The same or similar can be applied to an array — just call it ->findvalue
or similar...

Ke

On 16 October 2013 09:58, Bil Corry <[hidden email]> wrote:

>
> My suggestion is to add a new method to the array type that does what you
> want, such as:
>
> define array->findpair(key) => {
> > protect
> > return(.find(#key)->first->value)
> > /protect
> > return(void)
> > }
> > local(a) = array(1,date,'b'=1,'c'=3)
> > '<br>find = '; #a->find('b')->first->value;
> > '<br>findpair = ';#a->findpair('b');
>
> - Bil
>
>

#############################################################
This message is sent to you because you are subscribed to
  the mailing list Lasso
[hidden email]
To unsubscribe, E-mail to: <[hidden email]>
Send administrative queries to  <[hidden email]>
Reply | Threaded
Open this post in threaded view
|

Re: Allowing null -> value to work

Ke Carlton-3
In reply to this post by Bil Corry-3
On 16 October 2013 20:52, Bil Corry <[hidden email]> wrote:

> That "failing behavior" is needed to prevent coding errors - you don't want
> it silently failing.
>

++ There are time where code should fail — otherwise in many ways you're
coding blind.

#############################################################
This message is sent to you because you are subscribed to
  the mailing list Lasso
[hidden email]
To unsubscribe, E-mail to: <[hidden email]>
Send administrative queries to  <[hidden email]>
Reply | Threaded
Open this post in threaded view
|

Re: Allowing null -> value to work

Jolle Carlestam-3
All valuable insights to the issue at hand. Thank you for sharing!

HDB
Jolle

#############################################################
This message is sent to you because you are subscribed to
  the mailing list Lasso
[hidden email]
To unsubscribe, E-mail to: <[hidden email]>
Send administrative queries to  <[hidden email]>