WeixiangYan/CodeTransOcean huggingface dataset:
Task: Stem-and-leaf plot





Java: import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

public class StemAndLeaf {
	private static int[] data = { 12, 127, 28, 42, 39, 113, 42, 18, 44, 118,
			44, 37, 113, 124, 37, 48, 127, 36, 29, 31, 125, 139, 131, 115, 105,
			132, 104, 123, 35, 113, 122, 42, 117, 119, 58, 109, 23, 105, 63,
			27, 44, 105, 99, 41, 128, 121, 116, 125, 32, 61, 37, 127, 29, 113,
			121, 58, 114, 126, 53, 114, 96, 25, 109, 7, 31, 141, 46, 13, 27,
			43, 117, 116, 27, 7, 68, 40, 31, 115, 124, 42, 128, 52, 71, 118,
			117, 38, 27, 106, 33, 117, 116, 111, 40, 119, 47, 105, 57, 122,
			109, 124, 115, 43, 120, 43, 27, 27, 18, 28, 48, 125, 107, 114, 34,
			133, 45, 120, 30, 127, 31, 116, 146 };
	
	public static Map<Integer, List<Integer>> createPlot(int... data){
		Map<Integer, List<Integer>> plot = new TreeMap<Integer, List<Integer>>();
		int highestStem = -1; 
		for(int datum:data){
			int leaf = datum % 10;
			int stem = datum / 10; 
			if(stem > highestStem){
				highestStem = stem;
			}
			if(plot.containsKey(stem)){
				plot.get(stem).add(leaf);
			}else{
				LinkedList<Integer> list = new LinkedList<Integer>();
				list.add(leaf);
				plot.put(stem, list);
			}
		}
		if(plot.keySet().size() < highestStem + 1  ){
			for(int i = 0; i <= highestStem; i++){
				if(!plot.containsKey(i)){
					LinkedList<Integer> list = new LinkedList<Integer>();
					plot.put(i, list);
				}
			}
		}
		return plot;
	}
	
	public static void printPlot(Map<Integer, List<Integer>> plot){
		for(Map.Entry<Integer, List<Integer>> line : plot.entrySet()){
			Collections.sort(line.getValue());
			System.out.println(line.getKey() + " | " + line.getValue());
		}
	}
	
	public static void main(String[] args){
		Map<Integer, List<Integer>> plot = createPlot(data);
		printPlot(plot);
	}
}
Task: Stem-and-leaf plot







Python: from collections import namedtuple
from pprint import pprint as pp
from math import floor

Stem = namedtuple('Stem', 'data, leafdigits')

data0 = Stem((12, 127, 28, 42, 39, 113, 42, 18, 44, 118, 44, 37, 113, 124, 37,
              48, 127, 36, 29, 31, 125, 139, 131, 115, 105, 132, 104, 123, 35,
              113, 122, 42, 117, 119, 58, 109, 23, 105, 63, 27, 44, 105, 99,
              41, 128, 121, 116, 125, 32, 61, 37, 127, 29, 113, 121, 58, 114,
              126, 53, 114, 96, 25, 109, 7, 31, 141, 46, 13, 27, 43, 117, 116,
              27, 7, 68, 40, 31, 115, 124, 42, 128, 52, 71, 118, 117, 38, 27,
              106, 33, 117, 116, 111, 40, 119, 47, 105, 57, 122, 109, 124, 115,
              43, 120, 43, 27, 27, 18, 28, 48, 125, 107, 114, 34, 133, 45, 120,
              30, 127, 31, 116, 146),
             1.0)

def stemplot(stem):
    d = []
    interval = int(10**int(stem.leafdigits))
    for data in sorted(stem.data):
        data = int(floor(data))
        stm, lf = divmod(data,interval)
        d.append( (int(stm), int(lf)) )
    stems, leafs = list(zip(*d))
    stemwidth = max(len(str(x)) for x in stems)
    leafwidth = max(len(str(x)) for x in leafs)
    laststem, out = min(stems) - 1, []
    for s,l in d:
        while laststem < s:
            laststem += 1
            out.append('\n%*i |' % ( stemwidth, laststem))
        out.append(' %0*i' % (leafwidth, l))
    out.append('\n\nKey:\n Stem multiplier: %i\n X | Y  =>  %i*X+Y\n'
               % (interval, interval))
    return ''.join(out)

if __name__ == '__main__':
    print( stemplot(data0) )
Task: Stem-and-leaf plot




Go: package main

import (
    "fmt"
    "sort"
    "strconv"
    "strings"
)

var data = `12 127 28 42` 

func main() {
    
    m := make(map[int][]string)
    for _, s := range strings.Fields(data) {
        if len(s) == 1 {
            m[0] = append(m[0], s)
        } else if i, err := strconv.Atoi(s[:len(s)-1]); err == nil {
            m[i] = append(m[i], s[len(s)-1:])
        } else {
            panic("non numeric data")
        }
    }
    
    s := make([]int, len(m))
    var i int
    for k := range m {
        s[i] = k
        i++
    }
    sort.Ints(s)
    
    for k := s[0]; ; k++ {
        v := m[k]
        sort.Strings(v)
        fmt.Printf("%2d | %s\n", k, strings.Join(v, " "))
        if k == s[len(s)-1] {
            break
        }
    }
}
Task: Stem-and-leaf plot

C: #include <stdio.h>
#include <stdlib.h>

int icmp(const void *a, const void *b)
{
	return *(const int*)a < *(const int*)b ? -1 : *(const int*)a > *(const int*)b;
}

void leaf_plot(int *x, int len)
{
	int i, j, d;

	qsort(x, len, sizeof(int), icmp);

	i = x[0] / 10 - 1;
	for (j = 0; j < len; j++) {
		d = x[j] / 10;
		while (d > i) printf("%s%3d |", j ? "\n" : "", ++i);
		printf(" %d", x[j] % 10);
	}
}

int main()
{
	int data[] = {
	  12, 127,  28,  42,  39, 113,  42,  18,  44, 118,  44,  37, 113, 124,
	  37,  48, 127,  36,  29,  31, 125, 139, 131, 115, 105, 132, 104, 123,
	  35, 113, 122,  42, 117, 119,  58, 109,  23, 105,  63,  27,  44, 105,
	  99,  41, 128, 121, 116, 125,  32,  61,  37, 127,  29, 113, 121,  58,
	 114, 126,  53, 114,  96,  25, 109,   7,  31, 141,  46,  13,  27,  43,
	 117, 116,  27,   7,  68,  40,  31, 115, 124,  42, 128,  52,  71, 118,
	 117,  38,  27, 106,  33, 117, 116, 111,  40, 119,  47, 105,  57, 122,
	 109, 124, 115,  43, 120,  43,  27,  27,  18,  28,  48, 125, 107, 114,
	  34, 133,  45, 120,  30, 127,  31, 116, 146 };

	leaf_plot(data, sizeof(data)/sizeof(data[0]));

	return 0;
}










Fortran:       SUBROUTINE COMBSORT(A,N)
       INTEGER A(*)	
       INTEGER N	
       INTEGER H,T	
       LOGICAL CURSE
        H = N - 1		
    1   H = MAX(1,H*10/13)	
        IF (H.EQ.9 .OR. H.EQ.10) H = 11	
        CURSE = .FALSE.		
        DO I = N - H,1,-1	
          IF (A(I) .GT. A(I + H)) THEN	
            T=A(I); A(I)=A(I+H); A(I+H)=T	
            CURSE = .TRUE.			
          END IF			
        END DO			
        IF (CURSE .OR. H.GT.1) GO TO 1	
      END SUBROUTINE COMBSORT	

      SUBROUTINE TOPIARY(A,N)	
       INTEGER A(*)		
       INTEGER N		
       INTEGER CLIP		
       PARAMETER (CLIP = 10)	
       INTEGER I1,I2,STEM	
        CALL COMBSORT(A,N)	
        STEM = A(1)/CLIP	
        I1 = 1			
        I2 = I1			
   10   I2 = I2 + 1			
        IF (I2 .GT. N) GO TO 11		
        IF (A(I2)/CLIP .EQ.STEM) GO TO 10	
Cast forth a STEM line, corresponding to elements I1:I2 - 1.
   11   WRITE (6,12) STEM,ABS(MOD(A(I1:I2 - 1),CLIP))	
   12   FORMAT (I4,"|",(100I1))		
        IF (I2 .GT. N) RETURN		
        I1 = I2				
Chug along to the next STEM value.
   13   STEM = STEM + 1			
        IF (A(I2)/CLIP.GT.STEM) GO TO 11
        GO TO 10			
      END SUBROUTINE TOPIARY	

      PROGRAM TEST
      INTEGER VALUES(121)	
      DATA VALUES/		
     o  12,127, 28, 42, 39,113, 42, 18, 44,118,	
     1  44, 37,113,124, 37, 48,127, 36, 29, 31,	
     2 125,139,131,115,105,132,104,123, 35,113,
     3 122, 42,117,119, 58,109, 23,105, 63, 27,
     4  44,105, 99, 41,128,121,116,125, 32, 61,
     5  37,127, 29,113,121, 58,114,126, 53,114,
     6  96, 25,109,  7, 31,141, 46, 13, 27, 43,
     7 117,116, 27,  7, 68, 40, 31,115,124, 42,
     8 128, 52, 71,118,117, 38, 27,106, 33,117,
     9 116,111, 40,119, 47,105, 57,122,109,124,
     o 115, 43,120, 43, 27, 27, 18, 28, 48,125,
     1 107,114, 34,133, 45,120, 30,127, 31,116,
     2 146/
        CALL TOPIARY(VALUES,121)
      END
Task: Flatten a list



C#: using System;
using System.Collections;
using System.Linq;

namespace RosettaCodeTasks
{
	static class FlattenList
	{
		public static ArrayList Flatten(this ArrayList List)
		{
			ArrayList NewList = new ArrayList ( );

			NewList.AddRange ( List );

			while ( NewList.OfType<ArrayList> ( ).Count ( ) > 0 )
			{
				int index = NewList.IndexOf ( NewList.OfType<ArrayList> ( ).ElementAt ( 0 ) );
				ArrayList Temp = (ArrayList)NewList[index];
				NewList.RemoveAt ( index );
				NewList.InsertRange ( index, Temp );
			}
			
			return NewList;
		}
	}
}









Ada: generic
   type Element_Type is private;
   with function To_String (E : Element_Type) return String is <>;
package Nestable_Lists is

   type Node_Kind is (Data_Node, List_Node);
   
   type Node (Kind : Node_Kind);
   
   type List is access Node;
   
   type Node (Kind : Node_Kind) is record
      Next : List;
      case Kind is
         when Data_Node =>
            Data    : Element_Type;
         when List_Node =>
            Sublist : List;
      end case;
   end record;
   
   procedure Append (L : in out List; E : Element_Type);
   procedure Append (L : in out List; N : List);
   
   function Flatten (L : List) return List;

   function New_List (E : Element_Type) return List;
   function New_List (N : List) return List;
   
   function To_String (L : List) return String;
   
end Nestable_Lists;
Task: Flatten a list

C: #include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct list_t list_t, *list;
struct list_t{
	int is_list, ival; 
	list *lst;
};

list new_list()
{
	list x = malloc(sizeof(list_t));
	x->ival = 0;
	x->is_list = 1;
	x->lst = 0;
	return x;
}

void append(list parent, list child)
{
	parent->lst = realloc(parent->lst, sizeof(list) * (parent->ival + 1));
	parent->lst[parent->ival++] = child;
}

list from_string(char *s, char **e, list parent)
{
	list ret = 0;
	if (!parent) parent = new_list();

	while (*s != '\0') {
		if (*s == ']') {
			if (e) *e = s + 1;
			return parent;
		}
		if (*s == '[') {
			ret = new_list();
			ret->is_list = 1;
			ret->ival = 0;
			append(parent, ret);
			from_string(s + 1, &s, ret);
			continue;
		}
		if (*s >= '0' && *s <= '9') {
			ret = new_list();
			ret->is_list = 0;
			ret->ival = strtol(s, &s, 10);
			append(parent, ret);
			continue;
		}
		s++;
	}

	if (e) *e = s;
	return parent;
}

void show_list(list l)
{
	int i;
	if (!l) return;
	if (!l->is_list) {
		printf("%d", l->ival);
		return;
	}

	printf("[");
	for (i = 0; i < l->ival; i++) {
		show_list(l->lst[i]);
		if (i < l->ival - 1) printf(", ");
	}
	printf("]");
}

list flatten(list from, list to)
{
	int i;
	list t;

	if (!to) to = new_list();
	if (!from->is_list) {
		t = new_list();
		*t = *from;
		append(to, t);
	} else
		for (i = 0; i < from->ival; i++)
			flatten(from->lst[i], to);
	return to;
}

void delete_list(list l)
{
	int i;
	if (!l) return;
	if (l->is_list && l->ival) {
		for (i = 0; i < l->ival; i++)
			delete_list(l->lst[i]);
		free(l->lst);
	}

	free(l);
}

int main()
{
	list l = from_string("[1], 2, [[3,4], 5], [[[]]], [[[6]]], 7, 8, []", 0, 0);

	printf("Nested: ");
	show_list(l);
	printf("\n");

	list flat = flatten(l, 0);
	printf("Flattened: ");
	show_list(flat);

	
	return 0;
}











Ada: generic
   type Element_Type is private;
   with function To_String (E : Element_Type) return String is <>;
package Nestable_Lists is

   type Node_Kind is (Data_Node, List_Node);
   
   type Node (Kind : Node_Kind);
   
   type List is access Node;
   
   type Node (Kind : Node_Kind) is record
      Next : List;
      case Kind is
         when Data_Node =>
            Data    : Element_Type;
         when List_Node =>
            Sublist : List;
      end case;
   end record;
   
   procedure Append (L : in out List; E : Element_Type);
   procedure Append (L : in out List; N : List);
   
   function Flatten (L : List) return List;

   function New_List (E : Element_Type) return List;
   function New_List (N : List) return List;
   
   function To_String (L : List) return String;
   
end Nestable_Lists;
Task: Flatten a list


C++: #include <list>
#include <boost/any.hpp>

typedef std::list<boost::any> anylist;

void flatten(std::list<boost::any>& list)
{
  typedef anylist::iterator iterator;

  iterator current = list.begin();
  while (current != list.end())
  {
    if (current->type() == typeid(anylist))
    {
      iterator next = current;
      ++next;
      list.splice(next, boost::any_cast<anylist&>(*current));
      current = list.erase(current);
    }
    else
      ++current;
  }
}










Ada: generic
   type Element_Type is private;
   with function To_String (E : Element_Type) return String is <>;
package Nestable_Lists is

   type Node_Kind is (Data_Node, List_Node);
   
   type Node (Kind : Node_Kind);
   
   type List is access Node;
   
   type Node (Kind : Node_Kind) is record
      Next : List;
      case Kind is
         when Data_Node =>
            Data    : Element_Type;
         when List_Node =>
            Sublist : List;
      end case;
   end record;
   
   procedure Append (L : in out List; E : Element_Type);
   procedure Append (L : in out List; N : List);
   
   function Flatten (L : List) return List;

   function New_List (E : Element_Type) return List;
   function New_List (N : List) return List;
   
   function To_String (L : List) return String;
   
end Nestable_Lists;
Task: Flatten a list




Go: package main

import "fmt"

func list(s ...interface{}) []interface{} {
    return s
}

func main() {
    s := list(list(1),
        2,
        list(list(3, 4), 5),
        list(list(list())),
        list(list(list(6))),
        7,
        8,
        list(),
    )
    fmt.Println(s)
    fmt.Println(flatten(s))
}

func flatten(s []interface{}) (r []int) {
    for _, e := range s {
        switch i := e.(type) {
        case int:
            r = append(r, i)
        case []interface{}:
            r = append(r, flatten(i)...)
        }
    }
    return
}








Ada: generic
   type Element_Type is private;
   with function To_String (E : Element_Type) return String is <>;
package Nestable_Lists is

   type Node_Kind is (Data_Node, List_Node);
   
   type Node (Kind : Node_Kind);
   
   type List is access Node;
   
   type Node (Kind : Node_Kind) is record
      Next : List;
      case Kind is
         when Data_Node =>
            Data    : Element_Type;
         when List_Node =>
            Sublist : List;
      end case;
   end record;
   
   procedure Append (L : in out List; E : Element_Type);
   procedure Append (L : in out List; N : List);
   
   function Flatten (L : List) return List;

   function New_List (E : Element_Type) return List;
   function New_List (N : List) return List;
   
   function To_String (L : List) return String;
   
end Nestable_Lists;
Task: Flatten a list





Java: import java.util.LinkedList;
import java.util.List;


public final class FlattenUtil {

	public static List<Object> flatten(List<?> list) {
		List<Object> retVal = new LinkedList<Object>();
		flatten(list, retVal);
		return retVal;
	}

	public static void flatten(List<?> fromTreeList, List<Object> toFlatList) {
		for (Object item : fromTreeList) {
			if (item instanceof List<?>) {
				flatten((List<?>) item, toFlatList);
			} else {
				toFlatList.add(item);
			}
		}
	}
}







Ada: generic
   type Element_Type is private;
   with function To_String (E : Element_Type) return String is <>;
package Nestable_Lists is

   type Node_Kind is (Data_Node, List_Node);
   
   type Node (Kind : Node_Kind);
   
   type List is access Node;
   
   type Node (Kind : Node_Kind) is record
      Next : List;
      case Kind is
         when Data_Node =>
            Data    : Element_Type;
         when List_Node =>
            Sublist : List;
      end case;
   end record;
   
   procedure Append (L : in out List; E : Element_Type);
   procedure Append (L : in out List; N : List);
   
   function Flatten (L : List) return List;

   function New_List (E : Element_Type) return List;
   function New_List (N : List) return List;
   
   function To_String (L : List) return String;
   
end Nestable_Lists;
Task: Flatten a list







Python: >>> def flatten(lst):
	return sum( ([x] if not isinstance(x, list) else flatten(x)
		     for x in lst), [] )

>>> lst = [[1], 2, [[3,4], 5], [[[]]], [[[6]]], 7, 8, []]
>>> flatten(lst)
[1, 2, 3, 4, 5, 6, 7, 8]





Ada: generic
   type Element_Type is private;
   with function To_String (E : Element_Type) return String is <>;
package Nestable_Lists is

   type Node_Kind is (Data_Node, List_Node);
   
   type Node (Kind : Node_Kind);
   
   type List is access Node;
   
   type Node (Kind : Node_Kind) is record
      Next : List;
      case Kind is
         when Data_Node =>
            Data    : Element_Type;
         when List_Node =>
            Sublist : List;
      end case;
   end record;
   
   procedure Append (L : in out List; E : Element_Type);
   procedure Append (L : in out List; N : List);
   
   function Flatten (L : List) return List;

   function New_List (E : Element_Type) return List;
   function New_List (N : List) return List;
   
   function To_String (L : List) return String;
   
end Nestable_Lists;
Task: Flatten a list








VB: class flattener
	dim separator 
	
	sub class_initialize
		separator = ","
	end sub
	
	private function makeflat( a )
		dim i
		dim res
		for i = lbound( a ) to ubound( a ) 
			if isarray( a( i ) ) then
				res = res & makeflat( a( i ) )
			else
				res = res & a( i ) & separator
			end if
		next
		makeflat = res
	end function

	public function flatten( a )
		dim res
		res = makeflat( a )
		res = left( res, len( res ) - len(separator))
		res = split( res, separator )
		flatten = res
	end function
	
	public property let itemSeparator( c )
		separator = c
	end property
end class




Ada: generic
   type Element_Type is private;
   with function To_String (E : Element_Type) return String is <>;
package Nestable_Lists is

   type Node_Kind is (Data_Node, List_Node);
   
   type Node (Kind : Node_Kind);
   
   type List is access Node;
   
   type Node (Kind : Node_Kind) is record
      Next : List;
      case Kind is
         when Data_Node =>
            Data    : Element_Type;
         when List_Node =>
            Sublist : List;
      end case;
   end record;
   
   procedure Append (L : in out List; E : Element_Type);
   procedure Append (L : in out List; N : List);
   
   function Flatten (L : List) return List;

   function New_List (E : Element_Type) return List;
   function New_List (N : List) return List;
   
   function To_String (L : List) return String;
   
end Nestable_Lists;
Task: Flatten a list

C: #include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct list_t list_t, *list;
struct list_t{
	int is_list, ival; 
	list *lst;
};

list new_list()
{
	list x = malloc(sizeof(list_t));
	x->ival = 0;
	x->is_list = 1;
	x->lst = 0;
	return x;
}

void append(list parent, list child)
{
	parent->lst = realloc(parent->lst, sizeof(list) * (parent->ival + 1));
	parent->lst[parent->ival++] = child;
}

list from_string(char *s, char **e, list parent)
{
	list ret = 0;
	if (!parent) parent = new_list();

	while (*s != '\0') {
		if (*s == ']') {
			if (e) *e = s + 1;
			return parent;
		}
		if (*s == '[') {
			ret = new_list();
			ret->is_list = 1;
			ret->ival = 0;
			append(parent, ret);
			from_string(s + 1, &s, ret);
			continue;
		}
		if (*s >= '0' && *s <= '9') {
			ret = new_list();
			ret->is_list = 0;
			ret->ival = strtol(s, &s, 10);
			append(parent, ret);
			continue;
		}
		s++;
	}

	if (e) *e = s;
	return parent;
}

void show_list(list l)
{
	int i;
	if (!l) return;
	if (!l->is_list) {
		printf("%d", l->ival);
		return;
	}

	printf("[");
	for (i = 0; i < l->ival; i++) {
		show_list(l->lst[i]);
		if (i < l->ival - 1) printf(", ");
	}
	printf("]");
}

list flatten(list from, list to)
{
	int i;
	list t;

	if (!to) to = new_list();
	if (!from->is_list) {
		t = new_list();
		*t = *from;
		append(to, t);
	} else
		for (i = 0; i < from->ival; i++)
			flatten(from->lst[i], to);
	return to;
}

void delete_list(list l)
{
	int i;
	if (!l) return;
	if (l->is_list && l->ival) {
		for (i = 0; i < l->ival; i++)
			delete_list(l->lst[i]);
		free(l->lst);
	}

	free(l);
}

int main()
{
	list l = from_string("[1], 2, [[3,4], 5], [[[]]], [[[6]]], 7, 8, []", 0, 0);

	printf("Nested: ");
	show_list(l);
	printf("\n");

	list flat = flatten(l, 0);
	printf("Flattened: ");
	show_list(flat);

	
	return 0;
}
Task: Flatten a list



C#: using System;
using System.Collections;
using System.Linq;

namespace RosettaCodeTasks
{
	static class FlattenList
	{
		public static ArrayList Flatten(this ArrayList List)
		{
			ArrayList NewList = new ArrayList ( );

			NewList.AddRange ( List );

			while ( NewList.OfType<ArrayList> ( ).Count ( ) > 0 )
			{
				int index = NewList.IndexOf ( NewList.OfType<ArrayList> ( ).ElementAt ( 0 ) );
				ArrayList Temp = (ArrayList)NewList[index];
				NewList.RemoveAt ( index );
				NewList.InsertRange ( index, Temp );
			}
			
			return NewList;
		}
	}
}
Task: Flatten a list


C++: #include <list>
#include <boost/any.hpp>

typedef std::list<boost::any> anylist;

void flatten(std::list<boost::any>& list)
{
  typedef anylist::iterator iterator;

  iterator current = list.begin();
  while (current != list.end())
  {
    if (current->type() == typeid(anylist))
    {
      iterator next = current;
      ++next;
      list.splice(next, boost::any_cast<anylist&>(*current));
      current = list.erase(current);
    }
    else
      ++current;
  }
}
Task: Flatten a list





Java: import java.util.LinkedList;
import java.util.List;


public final class FlattenUtil {

	public static List<Object> flatten(List<?> list) {
		List<Object> retVal = new LinkedList<Object>();
		flatten(list, retVal);
		return retVal;
	}

	public static void flatten(List<?> fromTreeList, List<Object> toFlatList) {
		for (Object item : fromTreeList) {
			if (item instanceof List<?>) {
				flatten((List<?>) item, toFlatList);
			} else {
				toFlatList.add(item);
			}
		}
	}
}
Task: Flatten a list







Python: >>> def flatten(lst):
	return sum( ([x] if not isinstance(x, list) else flatten(x)
		     for x in lst), [] )

>>> lst = [[1], 2, [[3,4], 5], [[[]]], [[[6]]], 7, 8, []]
>>> flatten(lst)
[1, 2, 3, 4, 5, 6, 7, 8]
Task: Flatten a list








VB: class flattener
	dim separator 
	
	sub class_initialize
		separator = ","
	end sub
	
	private function makeflat( a )
		dim i
		dim res
		for i = lbound( a ) to ubound( a ) 
			if isarray( a( i ) ) then
				res = res & makeflat( a( i ) )
			else
				res = res & a( i ) & separator
			end if
		next
		makeflat = res
	end function

	public function flatten( a )
		dim res
		res = makeflat( a )
		res = left( res, len( res ) - len(separator))
		res = split( res, separator )
		flatten = res
	end function
	
	public property let itemSeparator( c )
		separator = c
	end property
end class
Task: Flatten a list




Go: package main

import "fmt"

func list(s ...interface{}) []interface{} {
    return s
}

func main() {
    s := list(list(1),
        2,
        list(list(3, 4), 5),
        list(list(list())),
        list(list(list(6))),
        7,
        8,
        list(),
    )
    fmt.Println(s)
    fmt.Println(flatten(s))
}

func flatten(s []interface{}) (r []int) {
    for _, e := range s {
        switch i := e.(type) {
        case int:
            r = append(r, i)
        case []interface{}:
            r = append(r, flatten(i)...)
        }
    }
    return
}
Task: Flatten a list






PHP: 
while (array_filter($lst, 'is_array'))
    $lst = call_user_func_array('array_merge', $lst);
Task: Flatten a list






PHP: 
while (array_filter($lst, 'is_array'))
    $lst = call_user_func_array('array_merge', $lst);






Ada: generic
   type Element_Type is private;
   with function To_String (E : Element_Type) return String is <>;
package Nestable_Lists is

   type Node_Kind is (Data_Node, List_Node);
   
   type Node (Kind : Node_Kind);
   
   type List is access Node;
   
   type Node (Kind : Node_Kind) is record
      Next : List;
      case Kind is
         when Data_Node =>
            Data    : Element_Type;
         when List_Node =>
            Sublist : List;
      end case;
   end record;
   
   procedure Append (L : in out List; E : Element_Type);
   procedure Append (L : in out List; N : List);
   
   function Flatten (L : List) return List;

   function New_List (E : Element_Type) return List;
   function New_List (N : List) return List;
   
   function To_String (L : List) return String;
   
end Nestable_Lists;