Avatar of 4d47

4d47's solution

to Flatten Array in the Perl 6 Track

Published at Jul 13 2018 · 4 comments
Instructions
Test suite
Solution

Take a nested list and return a single flattened list with all values except nil/null.

The challenge is to write a function that accepts an arbitrarily-deep nested list-like structure and returns a flattened structure without any nil/null values.

For Example

input: [1,[2,3,null,4],[null],5]

output: [1,2,3,4,5]

Resources

Remember to check out the Perl 6 documentation and resources pages for information, tips, and examples if you get stuck.

Running the tests

There is a test suite and module included with the exercise. The test suite (a file with the extension .t) will attempt to run routines from the module (a file with the extension .pm6). Add/modify routines in the module so that the tests will pass! You can view the test data by executing the command perl6 --doc *.t (* being the name of the test suite), and run the test suite for the exercise by executing the command prove6 . in the exercise directory.

Source

Interview Question https://reference.wolfram.com/language/ref/Flatten.html

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

flatten-array.t

#!/usr/bin/env perl6
use v6;
use Test;
use JSON::Fast;
use lib $?FILE.IO.dirname;
use FlattenArray;
plan 6;

my $c-data = from-json $=pod.pop.contents;
is-deeply flatten-array(.<input><array>), |.<expected description> for @($c-data<cases>);

=head2 Canonical Data
=begin code
{
  "exercise": "flatten-array",
  "version": "1.2.0",
  "cases": [
    {
      "description": "no nesting",
      "property": "flatten",
      "input": {
        "array": [0, 1, 2]
      },
      "expected": [0, 1, 2]
    },
    {
      "description": "flattens array with just integers present",
      "property": "flatten",
      "input": {
        "array": [1, [2, 3, 4, 5, 6, 7], 8]
      },
      "expected": [1, 2, 3, 4, 5, 6, 7, 8]
    },
    {
      "description": "5 level nesting",
      "property": "flatten",
      "input": {
        "array": [0, 2, [[2, 3], 8, 100, 4, [[[50]]]], -2]
      },
      "expected": [0, 2, 2, 3, 8, 100, 4, 50, -2]
    },
    {
      "description": "6 level nesting",
      "property": "flatten",
      "input": {
        "array": [1, [2, [[3]], [4, [[5]]], 6, 7], 8]
      },
      "expected": [1, 2, 3, 4, 5, 6, 7, 8]
    },
    {
      "description": "6 level nest list with null values",
      "property": "flatten",
      "input": {
        "array": [0, 2, [[2, 3], 8, [[100]], null, [[null]]], -2]
      },
      "expected": [0, 2, 2, 3, 8, 100, -2]
    },
    {
      "description": "all values in nested list are null",
      "property": "flatten",
      "input": {
        "array": [null, [[[null]]], null, null, [[null, null], null], null]
      },
      "expected": []
    }
  ]
}
=end code
unit module FlattenArray:ver<1>;

sub flatten-array(@input --> Array) is export {
    @input[**].grep(Any:D).Array
}
sub postcircumfix:<[ ]>(@list, HyperWhatever --> Seq) {
    gather @list.deepmap(*.take)
}

Community comments

Find this solution interesting? Ask the author a question to learn more.
Avatar of wenjie1991

It's a wonderful solution. You defined a [] to flat the array. I can not understand the usage of the ** inside the []. Could you tell me?

Avatar of wenjie1991

@wenjie1991 commented:

It's a wonderful solution. You defined a [] to flat the array. I can not understand the usage of the ** inside the []. Could you tell me?

I see. The ** means HyperWhatever. Here comes another question. Where can I find it in the perl6 document?

Avatar of mienaikage

HyperWhatever isn't on docs.perl6.org yet unfortunately. Looks like there's an issue open to put it in there: https://github.com/perl6/doc/issues/433

It can be found in the design documents, however: https://design.perl6.org/S02.html#The_HyperWhatever_Type

Avatar of wenjie1991

Thank you @mienaikage. I find it in the design documents.

    • 1 means -> $x { $x - 1 } ** - 1 means -> *@x { map -> $x { $x - 1 }, @x }

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?