[Proposal] List.unwrap/1

296 views
Skip to first unread message

Chris Keele

unread,
Sep 25, 2016, 6:00:00 PM9/25/16
to elixir-lang-core
List.unwrap/1 is a simple compliment to List.wrap/1––it walks a nested list and unwraps any extraneous wrappers it finds, eg: [[:a]] -> [:a].

I mostly find it useful for cleaning up after nested list transforms, but always manage to implement it wrong the first few times, so I thought I'd throw it out there as a general list utility.

Louis Pilfold

unread,
Sep 25, 2016, 6:01:04 PM9/25/16
to elixir-l...@googlegroups.com

What's the difference between this and List.flatten ?

Cheers,
Louis


On 25 Sep 2016 23:00, "Chris Keele" <d...@chriskeele.com> wrote:
List.unwrap/1 is a simple compliment to List.wrap/1––it walks a nested list and unwraps any extraneous wrappers it finds, eg: [[:a]] -> [:a].

I mostly find it useful for cleaning up after nested list transforms, but always manage to implement it wrong the first few times, so I thought I'd throw it out there as a general list utility.

--
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/3da5323f-585f-4068-8ef0-1865d3a75fd0%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Chris Keele

unread,
Sep 25, 2016, 6:05:21 PM9/25/16
to elixir-l...@googlegroups.com

It respects the existence of lists as a general data structure––effectively only flattening wrapper lists.

nested = [ [], :a, [:b], [[:c]] ]
List.flatten nested
#=> [:a, :b, :c]
List.unwrap nested
#=> [ [], :a, [:b], [:c] ]

Chris Keele

unread,
Sep 25, 2016, 6:17:53 PM9/25/16
to elixir-lang-core
Simply put, if List.wrap ensures its arguments are wrapped in a list at least once, List.unwrap ensures its arguments are wrapped in a list only once.

Peter Hamilton

unread,
Sep 25, 2016, 6:28:50 PM9/25/16
to elixir-lang-core

> List.unwrap ensures its arguments are wrapped in a list only once.

Only once or at most once?

I think it should be a strict mathematical inverse so:

(List.wrap(foo) |> List.unwrap) == foo


On Sun, Sep 25, 2016, 3:17 PM Chris Keele <d...@chriskeele.com> wrote:
Simply put, if List.wrap ensures its arguments are wrapped in a list at least once, List.unwrap ensures its arguments are wrapped in a list only once.

--
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/msgid/elixir-lang-core/526062e7-6a91-4a54-9e02-d70c7bcfe572%40googlegroups.com.

Chris Keele

unread,
Sep 25, 2016, 6:46:58 PM9/25/16
to elixir-l...@googlegroups.com
If List.unwrap([1]) = 1, how would you handle List.unwrap([1, 2])? [1, 2] would be inconsistent. That’s more of a splat/unquote_splicing operation.

Peter Hamilton

unread,
Sep 25, 2016, 9:55:10 PM9/25/16
to elixir-l...@googlegroups.com

Is [1,2] valid output of List.wrap? If it's not in the range of output, I think we could error on it as input to List.unwrap.

Exactly once might be correct semantics, will have to play with some examples.


On Sun, Sep 25, 2016, 3:46 PM Chris Keele <d...@chriskeele.com> wrote:
If List.unwrap([1]) = 1, how would you handle List.unwrap([1, 2])? [1, 2] would be inconsistent. That’s more of a splat/unquote_splicing operation.

--
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.

Chris Keele

unread,
Sep 25, 2016, 10:12:32 PM9/25/16
to elixir-l...@googlegroups.com

I can see how List.unwrap is a confusing–it does sound like the mathematical opposite.

Perhaps List.strip/1 is a more intuitive name?



-- 
Chris Keele
Web Developer
You received this message because you are subscribed to a topic in the Google Groups "elixir-lang-core" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/elixir-lang-core/rvI1kE8xp3Q/unsubscribe.
To unsubscribe from this group and all its topics, send an email to elixir-lang-co...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/CAOMhEnwu0v%2B-GyT8_QWtLxxCtkiOT7GrowAgY81w0mbtgygbfw%40mail.gmail.com.

Peter Hamilton

unread,
Sep 25, 2016, 10:36:31 PM9/25/16
to elixir-l...@googlegroups.com
How is its implementation more than just Enum.map(list, &hd/1) ?

I imagine we only conditionally apply hd if it's an element is a list of length 1. Does it do anything else?

Chris Keele

unread,
Sep 25, 2016, 11:00:04 PM9/25/16
to elixir-l...@googlegroups.com

That’s about it. And it recurses. Check out the implementation I linked to, with examples: https://github.com/christhekeele/elixir/commit/2a951a8a8a5b5eeb636fe00ca1ce11e8da58bb5f



-- 
Chris Keele
Web Developer

Peter Hamilton

unread,
Sep 26, 2016, 12:28:47 PM9/26/16
to elixir-l...@googlegroups.com

I'm having trouble seeing the utility. Could I see a few examples of how you got into that nested list state on the first place?


José Valim

unread,
Sep 26, 2016, 3:03:45 PM9/26/16
to elixir-l...@googlegroups.com
I echo Peter's concerns. It seems to be a very specific use case to justify its inclusion in the standard library.



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

On Mon, Sep 26, 2016 at 7:28 PM, Peter Hamilton <petergh...@gmail.com> wrote:

I'm having trouble seeing the utility. Could I see a few examples of how you got into that nested list state on the first place?


On Sun, Sep 25, 2016, 8:00 PM Chris Keele <d...@chriskeele.com> wrote:

That’s about it. And it recurses. Check out the implementation I linked to, with examples: https://github.com/christhekeele/elixir/commit/2a951a8a8a5b5eeb636fe00ca1ce11e8da58bb5f



-- 
Chris Keele
Web Developer

On September 25, 2016 at 7:36:32 PM, Peter Hamilton (petergh...@gmail.com) wrote:

How is its implementation more than just Enum.map(list, &hd/1) ?

I imagine we only conditionally apply hd if it's an element is a list of length 1. Does it do anything else?

On Sun, Sep 25, 2016, 7:12 PM Chris Keele <d...@chriskeele.com> wrote:

I can see how List.unwrap is a confusing–it does sound like the mathematical opposite.

Perhaps List.strip/1 is a more intuitive name?



-- 
Chris Keele
Web Developer

On September 25, 2016 at 6:55:11 PM, Peter Hamilton (petergh...@gmail.com) wrote:

Is [1,2] valid output of List.wrap? If it's not in the range of output, I think we could error on it as input to List.unwrap.

Exactly once might be correct semantics, will have to play with some examples.


On Sun, Sep 25, 2016, 3:46 PM Chris Keele <d...@chriskeele.com> wrote:
If List.unwrap([1]) = 1, how would you handle List.unwrap([1, 2])? [1, 2] would be inconsistent. That’s more of a splat/unquote_splicing operation.
--
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.
--
You received this message because you are subscribed to a topic in the Google Groups "elixir-lang-core" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/elixir-lang-core/rvI1kE8xp3Q/unsubscribe.
To unsubscribe from this group and all its topics, send an email to elixir-lang-core+unsubscribe@googlegroups.com.

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.
--
You received this message because you are subscribed to a topic in the Google Groups "elixir-lang-core" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/elixir-lang-core/rvI1kE8xp3Q/unsubscribe.
To unsubscribe from this group and all its topics, send an email to elixir-lang-core+unsubscribe@googlegroups.com.

--
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.

--
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/CAOMhEnzdoCjFTu65aCot-5t6bFgxgrroSH_FKaUg1qi%3DSsG1%2BA%40mail.gmail.com.

Onorio Catenacci

unread,
Sep 27, 2016, 1:45:03 PM9/27/16
to elixir-lang-core
+1 Peter.  This seems like a corner case.  While I can see the utility of your code I can't see _general_ utility which would warrant inclusion in the standard library.

I would say that it might make some sense to create a utility/extension library for Elixir but we've already got several packages trying to do just that on Hex.pm as far as I know.

oc

Chris Keele

unread,
Sep 27, 2016, 1:48:53 PM9/27/16
to elixir-l...@googlegroups.com

Sounds like a resounding YAGNI! S’what proposals are good for.

On utility library front, I’m pretty happy with the pragmatism the community has exhibited so far. No left-pads, no active-supports.



-- 
Chris Keele
Web Developer

--
You received this message because you are subscribed to a topic in the Google Groups "elixir-lang-core" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/elixir-lang-core/rvI1kE8xp3Q/unsubscribe.
To unsubscribe from this group and all its topics, send an email to elixir-lang-co...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages