Ajaxdone
How to check if a list is a sublist(out of order list) of another list
l1 = ["1","2","5","9"]
l2 = ["1","9"]
List.contains?(l1,l2)
Anything like this? to check if l2 is a sublist of l1.
I know can use Enum.each and check one by one, just want to know if anything like that. thanks
Marked As Solved
emoragaf
maybe using MapSet.subset?/2
6
Also Liked
NobbZ
l2 is not a sublist of l1.
But in general, sublist checking is quite easy to do.
def sublist?([], _), do: false
def sublist?(l1 = [_|t], l2) do
List.starts_with?(l1, l2) or sublist?(t, l2)
end
5
hauleth
def subset(_, []), do: true
def subset([], _), do: false
def subset([h | t1], [h | t2]), do: subset(t1, t2)
def subset([_ | t], list), do: subset(t, list)
3
Ajaxdone
gregvaughn
Depending on the relative sizes of your lists, this could also be a viable option:
Enum.all?(sub_out_of_order_list, &Enum.member?(big_list, &1))
3
Ajaxdone
Maybe I need to clarify a bit, actually I don’t mean sublist, I mean a list’s all items in another list, don’t care order.
2
Popular in Questions
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
Hi, I’m using Ubuntu 18.04 and after updating to OTP-24.0 yesterday i have this warning when I run “mix local.hex”:
14:57:30.512 [warn] ...
New
i’m a new one to elixir
which editor can i use
vs code? or atom?
Thanks! :smiley:
New
The Elixir Typespec docs show the following syntax for keyword lists in typespecs:
# ...
| [key: type] # keyword lis...
New
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
Hi,
I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
Hi guys, i’m new in the Elixir world, and i have to say, that i love it!
i’m having some problem to understand anonymous functions with ...
New
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
Hi everyone!
I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
Other popular topics
Here are few pieces of (common) Linux knowledge that we use for reasonably small one server apps. We use Ubuntu but this should work for ...
New
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
Good day to you all.
I have been struggling to get a query involving like and ilike to work.
Can anyone assist me on this, please?
pro...
New
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
I have followed this StackOverflow post to install the specific version of Erlang.
And When I am running mix ecto.setup then getting fol...
New
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine)
This is a plugin that adds support for Elixir to JetBrains IntelliJ...
New
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
Hi,
I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
Hey :wave:t3: Elixir community,
I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New







