<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def flatten(arys, returns_shapes=True, hstack=np.hstack, ravel=np.ravel, shape=np.shape):
 """ Flatten a potentially recursive list of multidimensional objects. .. note:: Not to be confused with `np.ndarray.flatten()` (a more befitting might be `chain` or `stack` or maybe something else entirely since this function is more than either `concatenate` or `np.flatten` itself. Rather, it is the composition of the former with the latter. Parameters arys : list of objects One or more input arrays of possibly heterogenous shapes and sizes. returns_shapes : bool, optional Default is `True`. If `True`, the tuple `(flattened, shapes)` is returned, otherwise only `flattened` is returned. hstack : callable, optional a function that implements horizontal stacking ravel : callable, optional a function that flattens the object shape : callable, optional a function that returns the shape of the object Returns ------- flattened,[shapes] : {1dobject, list of tuples} Return the flat (1d) object resulting from the concatenation of flattened multidimensional objects. When `returns_shapes` is `True`, return a list of tuples containing also the shapes of each array as the second element. See Also -------- revrand.utils.unflatten : its inverse Examples -------- (array([9, 4, 7, 4, 5, 2, 7, 3, 1, 2, 6, 6, 6, 5, 5, 1, 6, 9, 3, 9, 1, 9, 4, 1]), [(), (5,), (2, 3), (2, 2, 3)]) Note that scalars and 0-dimensional arrays are treated differently from 1-dimensional singleton arrays. (array([ 3.14, 2.71, 1.61]), [(), (), (1,)]) array([9, 4, 7, 4, 5, 2, 7, 3, 1, 2, 6, 6, 6, 5, 5, 1, 6, 9, 3, 9, 1, 9, 4, 1]) True True True True (array([ 3.14, 2.71, 1.61]), [(), [(), (1,)]]) """

    if issequence(arys) and len(arys) > 0:

        flat = partial(flatten,
                       returns_shapes=True,
                       hstack=hstack,
                       ravel=ravel,
                       shape=shape
                       )

        flat_arys, shapes = zip(*map(flat, arys))
        flat_ary = hstack(flat_arys)
        shapes = list(shapes)

    else:

        flat_ary = ravel(arys)
        shapes = shape(arys)

    return (flat_ary, shapes) if returns_shapes else flat_ary