Questions about introduction of List.starts_with?/2

53 views
Skip to first unread message

eksperimental

unread,
Feb 21, 2017, 9:39:04 AM2/21/17
to elixir-l...@googlegroups.com
Hi, List.starts_with?/2 have recently been introduced
https://github.com/elixir-lang/elixir/pull/5789

I was working on improving the specs and the code,
and I have realized there are a few issues I would to work on.

Initially would like to to know whether we should support improper lists, that would determine how
the code can be optimized.

I would say yes, we should support improper lists.
But we also need to keep in mind that we will probably will end up adding List.ends_with?/2 too,
so we need to keep that in mind

If we support improper lists, prefix could be any term, not just a list.

José Valim

unread,
Feb 21, 2017, 10:03:20 AM2/21/17
to elixir-l...@googlegroups.com
Excellent questions!

I don't think we should support improper lists because there is no notion of prefix for improper list. For instance, I would not expect this to match:

List.starts_with?([1, 2], [1 | 2])

Because we would compare 1 == 1 and then [2] to 2. The only situation they would match is if they are exactly equal and then the function is no longer necessary.


José Valim
Skype: jv.ptec
Founder and Director of R&D


--
You received this message because you are subscribed to the Google Groups "elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-core+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/20170221213857.6c3a8f4f.eksperimental%40autistici.org.
For more options, visit https://groups.google.com/d/optout.

Michał Muskała

unread,
Feb 21, 2017, 10:06:43 AM2/21/17
to elixir-l...@googlegroups.com
I think it would be nice to have starts_with?, ends_with? and contains? similar to the String module.

Michał.
--
You received this message because you are subscribed to the Google Groups "elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-co...@googlegroups.com.

eksperimental

unread,
Feb 21, 2017, 7:48:05 PM2/21/17
to elixir-l...@googlegroups.com
Sorry José, I was wrong when i wrote
> If we support improper lists, prefix could be any term, not just a list.

what meant is that if we support List.ends_with?/2, then suffix could be any term (but I will show
that this is still wrong)

List.ends_with?([1, 2 | 3], 3)
#=> true

but that could be ambiguous,
List.ends_with?([1, 2, 3], [2, 3])
#=> true

List.ends_with?([1 | [2, 3]], [2, 3])
#=> true

So suffix could ONLY be a list or an improper list.

List.ends_with?([1, 2 | 3]], [2 | 3])
#=> true

List.ends_with?([1, 2 | 3]], [2 | 3])
#=> true

Regarding prefixes.
We could still support improper lists as the first argument,

List.starts_with?([1, 2 | 3], [1, 2])
#=> true

and improper lists as the prefix (returns true only if both lists are the same)
List.starts_with?([1, 2 | 3], [1, 2 | 3])
#=> true

So wrapping up,
lists, prefixes and suffixes could be all maybe_improper_list


On Tue, 21 Feb 2017 16:02:58 +0100
José Valim <jose....@plataformatec.com.br> wrote:

> Excellent questions!
>
> I don't think we should support improper lists because there is no notion
> of prefix for improper list. For instance, I would not expect this to match:
>
> List.starts_with?([1, 2], [1 | 2])
>
>
> Because we would compare 1 == 1 and then [2] to 2. The only situation they
> would match is if they are exactly equal and then the function is no longer
> necessary.
>
>
> *José Valim*
> www.plataformatec.com.br
> Skype: jv.ptec
> Founder and Director of R&D
>
> On Tue, Feb 21, 2017 at 3:38 PM, eksperimental <eksper...@autistici.org>
> wrote:
>
> > Hi, List.starts_with?/2 have recently been introduced
> > https://github.com/elixir-lang/elixir/pull/5789
> >
> > I was working on improving the specs and the code,
> > and I have realized there are a few issues I would to work on.
> >
> > Initially would like to to know whether we should support improper lists,
> > that would determine how
> > the code can be optimized.
> >
> > I would say yes, we should support improper lists.
> > But we also need to keep in mind that we will probably will end up adding
> > List.ends_with?/2 too,
> > so we need to keep that in mind
> >
> > If we support improper lists, prefix could be any term, not just a list.
> >
> > --
> > You received this message because you are subscribed to the Google Groups
> > "elixir-lang-core" group.
> > To unsubscribe from this group and stop receiving emails from it, send an
> > email to elixir-lang-co...@googlegroups.com.

eksperimental

unread,
Feb 21, 2017, 7:55:25 PM2/21/17
to elixir-l...@googlegroups.com
yes, List.contains?/2 will be a really good addition too.
I can contribute the code for all of them once agreed on their behaviour and need.

José Valim

unread,
Feb 22, 2017, 5:57:51 AM2/22/17
to elixir-l...@googlegroups.com
I would prefer to not support improper lists as second argument for now. It is unnecessary complexity which doesn't seem to be needed yet. However, note we end-up supporting them as first argument of starts_with? by definition, since we don't want to traverse the whole list.


José Valim
Skype: jv.ptec
Founder and Director of R&D

> > To view this discussion on the web visit https://groups.google.com/d/ms
> > gid/elixir-lang-core/20170221213857.6c3a8f4f.eksperimental%40autistici.org
> > .
> > For more options, visit https://groups.google.com/d/optout.
> >
>
--
You received this message because you are subscribed to the Google Groups "elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-core+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/20170222010356.6eaf2436.eksperimental%40autistici.org.

eksperimental

unread,
Feb 22, 2017, 8:26:41 AM2/22/17
to elixir-l...@googlegroups.com
Great,
I will send a PR for List.starts_with?/2
and then we can move forward to ends_with?/2 and contains?/2

Thank you for your insights

On Wed, 22 Feb 2017 11:57:27 +0100
José Valim <jose....@plataformatec.com.br> wrote:

> I would prefer to not support improper lists as second argument for now. It
> is unnecessary complexity which doesn't seem to be needed yet. However,
> note we end-up supporting them as first argument of starts_with? by
> definition, since we don't want to traverse the whole list.
>
>
> > > > email to elixir-lang-co...@googlegroups.com.
> > > > To view this discussion on the web visit
> > https://groups.google.com/d/ms
> > > > gid/elixir-lang-core/20170221213857.6c3a8f4f.eksperimental%4
> > 0autistici.org
> > > > .
> > > > For more options, visit https://groups.google.com/d/optout.
> > > >
> > >
> >
> > --
> > You received this message because you are subscribed to the Google Groups
> > "elixir-lang-core" group.
> > To unsubscribe from this group and stop receiving emails from it, send an
> > email to elixir-lang-co...@googlegroups.com.
> > To view this discussion on the web visit https://groups.google.com/d/ms
> > gid/elixir-lang-core/20170222010356.6eaf2436.eksperimental%40autistici.org
Reply all
Reply to author
Forward
0 new messages