Instruction
stringlengths
45
106
input_code
stringlengths
1
13.7k
output_code
stringlengths
1
13.7k
Generate a C# translation of this Arturo snippet without changing its computational steps.
print flatten [[1], 2, [[3, 4], 5], [[[]]], [[[6]]], 7, 8, []]
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; } } }
Port the provided Arturo code into C# while preserving the original functionality.
print flatten [[1], 2, [[3, 4], 5], [[[]]], [[[6]]], 7, 8, []]
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; } } }
Maintain the same structure and functionality when rewriting this code in C++.
print flatten [[1], 2, [[3, 4], 5], [[[]]], [[[6]]], 7, 8, []]
#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; } }
Port the following code from Arturo to C++ with equivalent syntax and logic.
print flatten [[1], 2, [[3, 4], 5], [[[]]], [[[6]]], 7, 8, []]
#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; } }
Convert this Arturo block to Java, preserving its control flow and logic.
print flatten [[1], 2, [[3, 4], 5], [[[]]], [[[6]]], 7, 8, []]
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); } } } }
Port the following code from Arturo to Java with equivalent syntax and logic.
print flatten [[1], 2, [[3, 4], 5], [[[]]], [[[6]]], 7, 8, []]
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); } } } }
Generate a Python translation of this Arturo snippet without changing its computational steps.
print flatten [[1], 2, [[3, 4], 5], [[[]]], [[[6]]], 7, 8, []]
>>> 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]
Convert this Arturo snippet to Python and keep its semantics consistent.
print flatten [[1], 2, [[3, 4], 5], [[[]]], [[[6]]], 7, 8, []]
>>> 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]
Transform the following Arturo implementation into VB, maintaining the same output and logic.
print flatten [[1], 2, [[3, 4], 5], [[[]]], [[[6]]], 7, 8, []]
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
Produce a language-to-language conversion: from Arturo to VB, same semantics.
print flatten [[1], 2, [[3, 4], 5], [[[]]], [[[6]]], 7, 8, []]
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
Rewrite the snippet below in Go so it works the same as the original Arturo code.
print flatten [[1], 2, [[3, 4], 5], [[[]]], [[[6]]], 7, 8, []]
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 }
Produce a functionally identical Go code for the snippet given in Arturo.
print flatten [[1], 2, [[3, 4], 5], [[[]]], [[[6]]], 7, 8, []]
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 }
Ensure the translated C code behaves exactly like the original AutoHotKey snippet.
list := object(1, object(1, 1), 2, 2, 3, object(1, object(1, 3, 2, 4) , 2, 5), 4, object(1, object(1, object(1, object()))), 5 , object(1, object(1, 6)), 6, 7, 7, 8, 9, object()) msgbox % objPrint(list)  msgbox % objPrint(objFlatten(list))  return !r::reload !q::exitapp objPrint(ast, reserved=0) { if !isobject(ast) return " " ast " " if !reserved reserved := object("seen" . &ast, 1)   enum := ast._newenum() while enum[key, value] { if reserved["seen" . &value] continue   string .= objPrint(value, reserved) } return "(" string ")" } objFlatten(ast) { if !isobject(ast) return ast flat := object()  enum := ast._newenum() while enum[key, value] { if !isobject(value) flat._Insert(value) else { next := objFlatten(value) loop % next._MaxIndex() flat._Insert(next[A_Index]) } } return flat }
#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; }
Rewrite this program in C while keeping its functionality equivalent to the AutoHotKey version.
list := object(1, object(1, 1), 2, 2, 3, object(1, object(1, 3, 2, 4) , 2, 5), 4, object(1, object(1, object(1, object()))), 5 , object(1, object(1, 6)), 6, 7, 7, 8, 9, object()) msgbox % objPrint(list)  msgbox % objPrint(objFlatten(list))  return !r::reload !q::exitapp objPrint(ast, reserved=0) { if !isobject(ast) return " " ast " " if !reserved reserved := object("seen" . &ast, 1)   enum := ast._newenum() while enum[key, value] { if reserved["seen" . &value] continue   string .= objPrint(value, reserved) } return "(" string ")" } objFlatten(ast) { if !isobject(ast) return ast flat := object()  enum := ast._newenum() while enum[key, value] { if !isobject(value) flat._Insert(value) else { next := objFlatten(value) loop % next._MaxIndex() flat._Insert(next[A_Index]) } } return flat }
#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; }
Ensure the translated C# code behaves exactly like the original AutoHotKey snippet.
list := object(1, object(1, 1), 2, 2, 3, object(1, object(1, 3, 2, 4) , 2, 5), 4, object(1, object(1, object(1, object()))), 5 , object(1, object(1, 6)), 6, 7, 7, 8, 9, object()) msgbox % objPrint(list)  msgbox % objPrint(objFlatten(list))  return !r::reload !q::exitapp objPrint(ast, reserved=0) { if !isobject(ast) return " " ast " " if !reserved reserved := object("seen" . &ast, 1)   enum := ast._newenum() while enum[key, value] { if reserved["seen" . &value] continue   string .= objPrint(value, reserved) } return "(" string ")" } objFlatten(ast) { if !isobject(ast) return ast flat := object()  enum := ast._newenum() while enum[key, value] { if !isobject(value) flat._Insert(value) else { next := objFlatten(value) loop % next._MaxIndex() flat._Insert(next[A_Index]) } } return flat }
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; } } }
Maintain the same structure and functionality when rewriting this code in C#.
list := object(1, object(1, 1), 2, 2, 3, object(1, object(1, 3, 2, 4) , 2, 5), 4, object(1, object(1, object(1, object()))), 5 , object(1, object(1, 6)), 6, 7, 7, 8, 9, object()) msgbox % objPrint(list)  msgbox % objPrint(objFlatten(list))  return !r::reload !q::exitapp objPrint(ast, reserved=0) { if !isobject(ast) return " " ast " " if !reserved reserved := object("seen" . &ast, 1)   enum := ast._newenum() while enum[key, value] { if reserved["seen" . &value] continue   string .= objPrint(value, reserved) } return "(" string ")" } objFlatten(ast) { if !isobject(ast) return ast flat := object()  enum := ast._newenum() while enum[key, value] { if !isobject(value) flat._Insert(value) else { next := objFlatten(value) loop % next._MaxIndex() flat._Insert(next[A_Index]) } } return flat }
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; } } }
Rewrite this program in C++ while keeping its functionality equivalent to the AutoHotKey version.
list := object(1, object(1, 1), 2, 2, 3, object(1, object(1, 3, 2, 4) , 2, 5), 4, object(1, object(1, object(1, object()))), 5 , object(1, object(1, 6)), 6, 7, 7, 8, 9, object()) msgbox % objPrint(list)  msgbox % objPrint(objFlatten(list))  return !r::reload !q::exitapp objPrint(ast, reserved=0) { if !isobject(ast) return " " ast " " if !reserved reserved := object("seen" . &ast, 1)   enum := ast._newenum() while enum[key, value] { if reserved["seen" . &value] continue   string .= objPrint(value, reserved) } return "(" string ")" } objFlatten(ast) { if !isobject(ast) return ast flat := object()  enum := ast._newenum() while enum[key, value] { if !isobject(value) flat._Insert(value) else { next := objFlatten(value) loop % next._MaxIndex() flat._Insert(next[A_Index]) } } return flat }
#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; } }
Produce a functionally identical C++ code for the snippet given in AutoHotKey.
list := object(1, object(1, 1), 2, 2, 3, object(1, object(1, 3, 2, 4) , 2, 5), 4, object(1, object(1, object(1, object()))), 5 , object(1, object(1, 6)), 6, 7, 7, 8, 9, object()) msgbox % objPrint(list)  msgbox % objPrint(objFlatten(list))  return !r::reload !q::exitapp objPrint(ast, reserved=0) { if !isobject(ast) return " " ast " " if !reserved reserved := object("seen" . &ast, 1)   enum := ast._newenum() while enum[key, value] { if reserved["seen" . &value] continue   string .= objPrint(value, reserved) } return "(" string ")" } objFlatten(ast) { if !isobject(ast) return ast flat := object()  enum := ast._newenum() while enum[key, value] { if !isobject(value) flat._Insert(value) else { next := objFlatten(value) loop % next._MaxIndex() flat._Insert(next[A_Index]) } } return flat }
#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; } }
Change the following AutoHotKey code into Java without altering its purpose.
list := object(1, object(1, 1), 2, 2, 3, object(1, object(1, 3, 2, 4) , 2, 5), 4, object(1, object(1, object(1, object()))), 5 , object(1, object(1, 6)), 6, 7, 7, 8, 9, object()) msgbox % objPrint(list)  msgbox % objPrint(objFlatten(list))  return !r::reload !q::exitapp objPrint(ast, reserved=0) { if !isobject(ast) return " " ast " " if !reserved reserved := object("seen" . &ast, 1)   enum := ast._newenum() while enum[key, value] { if reserved["seen" . &value] continue   string .= objPrint(value, reserved) } return "(" string ")" } objFlatten(ast) { if !isobject(ast) return ast flat := object()  enum := ast._newenum() while enum[key, value] { if !isobject(value) flat._Insert(value) else { next := objFlatten(value) loop % next._MaxIndex() flat._Insert(next[A_Index]) } } return flat }
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); } } } }
Transform the following AutoHotKey implementation into Java, maintaining the same output and logic.
list := object(1, object(1, 1), 2, 2, 3, object(1, object(1, 3, 2, 4) , 2, 5), 4, object(1, object(1, object(1, object()))), 5 , object(1, object(1, 6)), 6, 7, 7, 8, 9, object()) msgbox % objPrint(list)  msgbox % objPrint(objFlatten(list))  return !r::reload !q::exitapp objPrint(ast, reserved=0) { if !isobject(ast) return " " ast " " if !reserved reserved := object("seen" . &ast, 1)   enum := ast._newenum() while enum[key, value] { if reserved["seen" . &value] continue   string .= objPrint(value, reserved) } return "(" string ")" } objFlatten(ast) { if !isobject(ast) return ast flat := object()  enum := ast._newenum() while enum[key, value] { if !isobject(value) flat._Insert(value) else { next := objFlatten(value) loop % next._MaxIndex() flat._Insert(next[A_Index]) } } return flat }
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); } } } }
Write the same code in Python as shown below in AutoHotKey.
list := object(1, object(1, 1), 2, 2, 3, object(1, object(1, 3, 2, 4) , 2, 5), 4, object(1, object(1, object(1, object()))), 5 , object(1, object(1, 6)), 6, 7, 7, 8, 9, object()) msgbox % objPrint(list)  msgbox % objPrint(objFlatten(list))  return !r::reload !q::exitapp objPrint(ast, reserved=0) { if !isobject(ast) return " " ast " " if !reserved reserved := object("seen" . &ast, 1)   enum := ast._newenum() while enum[key, value] { if reserved["seen" . &value] continue   string .= objPrint(value, reserved) } return "(" string ")" } objFlatten(ast) { if !isobject(ast) return ast flat := object()  enum := ast._newenum() while enum[key, value] { if !isobject(value) flat._Insert(value) else { next := objFlatten(value) loop % next._MaxIndex() flat._Insert(next[A_Index]) } } return flat }
>>> 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]
Generate an equivalent Python version of this AutoHotKey code.
list := object(1, object(1, 1), 2, 2, 3, object(1, object(1, 3, 2, 4) , 2, 5), 4, object(1, object(1, object(1, object()))), 5 , object(1, object(1, 6)), 6, 7, 7, 8, 9, object()) msgbox % objPrint(list)  msgbox % objPrint(objFlatten(list))  return !r::reload !q::exitapp objPrint(ast, reserved=0) { if !isobject(ast) return " " ast " " if !reserved reserved := object("seen" . &ast, 1)   enum := ast._newenum() while enum[key, value] { if reserved["seen" . &value] continue   string .= objPrint(value, reserved) } return "(" string ")" } objFlatten(ast) { if !isobject(ast) return ast flat := object()  enum := ast._newenum() while enum[key, value] { if !isobject(value) flat._Insert(value) else { next := objFlatten(value) loop % next._MaxIndex() flat._Insert(next[A_Index]) } } return flat }
>>> 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]
Write the same code in VB as shown below in AutoHotKey.
list := object(1, object(1, 1), 2, 2, 3, object(1, object(1, 3, 2, 4) , 2, 5), 4, object(1, object(1, object(1, object()))), 5 , object(1, object(1, 6)), 6, 7, 7, 8, 9, object()) msgbox % objPrint(list)  msgbox % objPrint(objFlatten(list))  return !r::reload !q::exitapp objPrint(ast, reserved=0) { if !isobject(ast) return " " ast " " if !reserved reserved := object("seen" . &ast, 1)   enum := ast._newenum() while enum[key, value] { if reserved["seen" . &value] continue   string .= objPrint(value, reserved) } return "(" string ")" } objFlatten(ast) { if !isobject(ast) return ast flat := object()  enum := ast._newenum() while enum[key, value] { if !isobject(value) flat._Insert(value) else { next := objFlatten(value) loop % next._MaxIndex() flat._Insert(next[A_Index]) } } return flat }
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
Keep all operations the same but rewrite the snippet in VB.
list := object(1, object(1, 1), 2, 2, 3, object(1, object(1, 3, 2, 4) , 2, 5), 4, object(1, object(1, object(1, object()))), 5 , object(1, object(1, 6)), 6, 7, 7, 8, 9, object()) msgbox % objPrint(list)  msgbox % objPrint(objFlatten(list))  return !r::reload !q::exitapp objPrint(ast, reserved=0) { if !isobject(ast) return " " ast " " if !reserved reserved := object("seen" . &ast, 1)   enum := ast._newenum() while enum[key, value] { if reserved["seen" . &value] continue   string .= objPrint(value, reserved) } return "(" string ")" } objFlatten(ast) { if !isobject(ast) return ast flat := object()  enum := ast._newenum() while enum[key, value] { if !isobject(value) flat._Insert(value) else { next := objFlatten(value) loop % next._MaxIndex() flat._Insert(next[A_Index]) } } return flat }
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
Translate the given AutoHotKey code snippet into Go without altering its behavior.
list := object(1, object(1, 1), 2, 2, 3, object(1, object(1, 3, 2, 4) , 2, 5), 4, object(1, object(1, object(1, object()))), 5 , object(1, object(1, 6)), 6, 7, 7, 8, 9, object()) msgbox % objPrint(list)  msgbox % objPrint(objFlatten(list))  return !r::reload !q::exitapp objPrint(ast, reserved=0) { if !isobject(ast) return " " ast " " if !reserved reserved := object("seen" . &ast, 1)   enum := ast._newenum() while enum[key, value] { if reserved["seen" . &value] continue   string .= objPrint(value, reserved) } return "(" string ")" } objFlatten(ast) { if !isobject(ast) return ast flat := object()  enum := ast._newenum() while enum[key, value] { if !isobject(value) flat._Insert(value) else { next := objFlatten(value) loop % next._MaxIndex() flat._Insert(next[A_Index]) } } return flat }
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 }
Generate an equivalent Go version of this AutoHotKey code.
list := object(1, object(1, 1), 2, 2, 3, object(1, object(1, 3, 2, 4) , 2, 5), 4, object(1, object(1, object(1, object()))), 5 , object(1, object(1, 6)), 6, 7, 7, 8, 9, object()) msgbox % objPrint(list)  msgbox % objPrint(objFlatten(list))  return !r::reload !q::exitapp objPrint(ast, reserved=0) { if !isobject(ast) return " " ast " " if !reserved reserved := object("seen" . &ast, 1)   enum := ast._newenum() while enum[key, value] { if reserved["seen" . &value] continue   string .= objPrint(value, reserved) } return "(" string ")" } objFlatten(ast) { if !isobject(ast) return ast flat := object()  enum := ast._newenum() while enum[key, value] { if !isobject(value) flat._Insert(value) else { next := objFlatten(value) loop % next._MaxIndex() flat._Insert(next[A_Index]) } } return flat }
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 }
Ensure the translated C code behaves exactly like the original Clojure snippet.
(defn flatten [lst] (sum (genexpr (if (isinstance x list) (flatten x) [x]) [x lst]) [])) (print (flatten [[1] 2 [[3 4] 5] [[[]]] [[[6]]] 7 8 []]))
#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; }
Please provide an equivalent version of this Clojure code in C.
(defn flatten [lst] (sum (genexpr (if (isinstance x list) (flatten x) [x]) [x lst]) [])) (print (flatten [[1] 2 [[3 4] 5] [[[]]] [[[6]]] 7 8 []]))
#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; }
Produce a language-to-language conversion: from Clojure to C#, same semantics.
(defn flatten [lst] (sum (genexpr (if (isinstance x list) (flatten x) [x]) [x lst]) [])) (print (flatten [[1] 2 [[3 4] 5] [[[]]] [[[6]]] 7 8 []]))
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; } } }
Translate this program into C# but keep the logic exactly as in Clojure.
(defn flatten [lst] (sum (genexpr (if (isinstance x list) (flatten x) [x]) [x lst]) [])) (print (flatten [[1] 2 [[3 4] 5] [[[]]] [[[6]]] 7 8 []]))
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; } } }
Translate the given Clojure code snippet into C++ without altering its behavior.
(defn flatten [lst] (sum (genexpr (if (isinstance x list) (flatten x) [x]) [x lst]) [])) (print (flatten [[1] 2 [[3 4] 5] [[[]]] [[[6]]] 7 8 []]))
#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; } }
Convert this Clojure snippet to C++ and keep its semantics consistent.
(defn flatten [lst] (sum (genexpr (if (isinstance x list) (flatten x) [x]) [x lst]) [])) (print (flatten [[1] 2 [[3 4] 5] [[[]]] [[[6]]] 7 8 []]))
#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; } }
Translate this program into Java but keep the logic exactly as in Clojure.
(defn flatten [lst] (sum (genexpr (if (isinstance x list) (flatten x) [x]) [x lst]) [])) (print (flatten [[1] 2 [[3 4] 5] [[[]]] [[[6]]] 7 8 []]))
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); } } } }
Change the programming language of this snippet from Clojure to Java without modifying what it does.
(defn flatten [lst] (sum (genexpr (if (isinstance x list) (flatten x) [x]) [x lst]) [])) (print (flatten [[1] 2 [[3 4] 5] [[[]]] [[[6]]] 7 8 []]))
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); } } } }
Keep all operations the same but rewrite the snippet in Python.
(defn flatten [lst] (sum (genexpr (if (isinstance x list) (flatten x) [x]) [x lst]) [])) (print (flatten [[1] 2 [[3 4] 5] [[[]]] [[[6]]] 7 8 []]))
>>> 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]
Port the following code from Clojure to Python with equivalent syntax and logic.
(defn flatten [lst] (sum (genexpr (if (isinstance x list) (flatten x) [x]) [x lst]) [])) (print (flatten [[1] 2 [[3 4] 5] [[[]]] [[[6]]] 7 8 []]))
>>> 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]
Produce a language-to-language conversion: from Clojure to VB, same semantics.
(defn flatten [lst] (sum (genexpr (if (isinstance x list) (flatten x) [x]) [x lst]) [])) (print (flatten [[1] 2 [[3 4] 5] [[[]]] [[[6]]] 7 8 []]))
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
Please provide an equivalent version of this Clojure code in VB.
(defn flatten [lst] (sum (genexpr (if (isinstance x list) (flatten x) [x]) [x lst]) [])) (print (flatten [[1] 2 [[3 4] 5] [[[]]] [[[6]]] 7 8 []]))
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
Write a version of this Clojure function in Go with identical behavior.
(defn flatten [lst] (sum (genexpr (if (isinstance x list) (flatten x) [x]) [x lst]) [])) (print (flatten [[1] 2 [[3 4] 5] [[[]]] [[[6]]] 7 8 []]))
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 }
Maintain the same structure and functionality when rewriting this code in Go.
(defn flatten [lst] (sum (genexpr (if (isinstance x list) (flatten x) [x]) [x lst]) [])) (print (flatten [[1] 2 [[3 4] 5] [[[]]] [[[6]]] 7 8 []]))
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 }
Generate a C translation of this Common_Lisp snippet without changing its computational steps.
(defun flatten (tr) (cond ((null tr) nil) ((atom tr) (list tr)) (t (append (flatten (first tr)) (flatten (rest tr))))))
#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; }
Port the following code from Common_Lisp to C with equivalent syntax and logic.
(defun flatten (tr) (cond ((null tr) nil) ((atom tr) (list tr)) (t (append (flatten (first tr)) (flatten (rest tr))))))
#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; }
Write the same code in C# as shown below in Common_Lisp.
(defun flatten (tr) (cond ((null tr) nil) ((atom tr) (list tr)) (t (append (flatten (first tr)) (flatten (rest tr))))))
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; } } }
Produce a functionally identical C# code for the snippet given in Common_Lisp.
(defun flatten (tr) (cond ((null tr) nil) ((atom tr) (list tr)) (t (append (flatten (first tr)) (flatten (rest tr))))))
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; } } }
Convert the following code from Common_Lisp to C++, ensuring the logic remains intact.
(defun flatten (tr) (cond ((null tr) nil) ((atom tr) (list tr)) (t (append (flatten (first tr)) (flatten (rest tr))))))
#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; } }
Write the same algorithm in C++ as shown in this Common_Lisp implementation.
(defun flatten (tr) (cond ((null tr) nil) ((atom tr) (list tr)) (t (append (flatten (first tr)) (flatten (rest tr))))))
#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; } }
Convert the following code from Common_Lisp to Java, ensuring the logic remains intact.
(defun flatten (tr) (cond ((null tr) nil) ((atom tr) (list tr)) (t (append (flatten (first tr)) (flatten (rest tr))))))
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); } } } }
Change the programming language of this snippet from Common_Lisp to Java without modifying what it does.
(defun flatten (tr) (cond ((null tr) nil) ((atom tr) (list tr)) (t (append (flatten (first tr)) (flatten (rest tr))))))
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); } } } }
Port the following code from Common_Lisp to Python with equivalent syntax and logic.
(defun flatten (tr) (cond ((null tr) nil) ((atom tr) (list tr)) (t (append (flatten (first tr)) (flatten (rest tr))))))
>>> 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]
Transform the following Common_Lisp implementation into Python, maintaining the same output and logic.
(defun flatten (tr) (cond ((null tr) nil) ((atom tr) (list tr)) (t (append (flatten (first tr)) (flatten (rest tr))))))
>>> 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]
Rewrite the snippet below in VB so it works the same as the original Common_Lisp code.
(defun flatten (tr) (cond ((null tr) nil) ((atom tr) (list tr)) (t (append (flatten (first tr)) (flatten (rest tr))))))
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
Rewrite this program in VB while keeping its functionality equivalent to the Common_Lisp version.
(defun flatten (tr) (cond ((null tr) nil) ((atom tr) (list tr)) (t (append (flatten (first tr)) (flatten (rest tr))))))
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
Rewrite the snippet below in Go so it works the same as the original Common_Lisp code.
(defun flatten (tr) (cond ((null tr) nil) ((atom tr) (list tr)) (t (append (flatten (first tr)) (flatten (rest tr))))))
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 }
Change the programming language of this snippet from Common_Lisp to Go without modifying what it does.
(defun flatten (tr) (cond ((null tr) nil) ((atom tr) (list tr)) (t (append (flatten (first tr)) (flatten (rest tr))))))
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 }
Preserve the algorithm and functionality while converting the code from D to C.
import std.stdio, std.algorithm, std.conv, std.range; struct TreeList(T) { union { TreeList[] arr; T data; } bool isArray = true; static TreeList opCall(A...)(A items) pure nothrow { TreeList result; foreach (i, el; items) static if (is(A[i] == T)) { TreeList item; item.isArray = false; item.data = el; result.arr ~= item; } else result.arr ~= el; return result; } string toString() const pure { return isArray ? arr.text : data.text; } } T[] flatten(T)(in TreeList!T t) pure nothrow { if (t.isArray) return t.arr.map!flatten.join; else return [t.data]; } void main() { alias TreeList!int L; static assert(L.sizeof == 12); auto l = L(L(1), 2, L(L(3,4), 5), L(L(L())), L(L(L(6))),7,8,L()); l.writeln; l.flatten.writeln; }
#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; }
Convert the following code from D to C, ensuring the logic remains intact.
import std.stdio, std.algorithm, std.conv, std.range; struct TreeList(T) { union { TreeList[] arr; T data; } bool isArray = true; static TreeList opCall(A...)(A items) pure nothrow { TreeList result; foreach (i, el; items) static if (is(A[i] == T)) { TreeList item; item.isArray = false; item.data = el; result.arr ~= item; } else result.arr ~= el; return result; } string toString() const pure { return isArray ? arr.text : data.text; } } T[] flatten(T)(in TreeList!T t) pure nothrow { if (t.isArray) return t.arr.map!flatten.join; else return [t.data]; } void main() { alias TreeList!int L; static assert(L.sizeof == 12); auto l = L(L(1), 2, L(L(3,4), 5), L(L(L())), L(L(L(6))),7,8,L()); l.writeln; l.flatten.writeln; }
#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; }
Preserve the algorithm and functionality while converting the code from D to C#.
import std.stdio, std.algorithm, std.conv, std.range; struct TreeList(T) { union { TreeList[] arr; T data; } bool isArray = true; static TreeList opCall(A...)(A items) pure nothrow { TreeList result; foreach (i, el; items) static if (is(A[i] == T)) { TreeList item; item.isArray = false; item.data = el; result.arr ~= item; } else result.arr ~= el; return result; } string toString() const pure { return isArray ? arr.text : data.text; } } T[] flatten(T)(in TreeList!T t) pure nothrow { if (t.isArray) return t.arr.map!flatten.join; else return [t.data]; } void main() { alias TreeList!int L; static assert(L.sizeof == 12); auto l = L(L(1), 2, L(L(3,4), 5), L(L(L())), L(L(L(6))),7,8,L()); l.writeln; l.flatten.writeln; }
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; } } }
Write the same algorithm in C# as shown in this D implementation.
import std.stdio, std.algorithm, std.conv, std.range; struct TreeList(T) { union { TreeList[] arr; T data; } bool isArray = true; static TreeList opCall(A...)(A items) pure nothrow { TreeList result; foreach (i, el; items) static if (is(A[i] == T)) { TreeList item; item.isArray = false; item.data = el; result.arr ~= item; } else result.arr ~= el; return result; } string toString() const pure { return isArray ? arr.text : data.text; } } T[] flatten(T)(in TreeList!T t) pure nothrow { if (t.isArray) return t.arr.map!flatten.join; else return [t.data]; } void main() { alias TreeList!int L; static assert(L.sizeof == 12); auto l = L(L(1), 2, L(L(3,4), 5), L(L(L())), L(L(L(6))),7,8,L()); l.writeln; l.flatten.writeln; }
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; } } }
Convert this D snippet to C++ and keep its semantics consistent.
import std.stdio, std.algorithm, std.conv, std.range; struct TreeList(T) { union { TreeList[] arr; T data; } bool isArray = true; static TreeList opCall(A...)(A items) pure nothrow { TreeList result; foreach (i, el; items) static if (is(A[i] == T)) { TreeList item; item.isArray = false; item.data = el; result.arr ~= item; } else result.arr ~= el; return result; } string toString() const pure { return isArray ? arr.text : data.text; } } T[] flatten(T)(in TreeList!T t) pure nothrow { if (t.isArray) return t.arr.map!flatten.join; else return [t.data]; } void main() { alias TreeList!int L; static assert(L.sizeof == 12); auto l = L(L(1), 2, L(L(3,4), 5), L(L(L())), L(L(L(6))),7,8,L()); l.writeln; l.flatten.writeln; }
#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; } }
Keep all operations the same but rewrite the snippet in C++.
import std.stdio, std.algorithm, std.conv, std.range; struct TreeList(T) { union { TreeList[] arr; T data; } bool isArray = true; static TreeList opCall(A...)(A items) pure nothrow { TreeList result; foreach (i, el; items) static if (is(A[i] == T)) { TreeList item; item.isArray = false; item.data = el; result.arr ~= item; } else result.arr ~= el; return result; } string toString() const pure { return isArray ? arr.text : data.text; } } T[] flatten(T)(in TreeList!T t) pure nothrow { if (t.isArray) return t.arr.map!flatten.join; else return [t.data]; } void main() { alias TreeList!int L; static assert(L.sizeof == 12); auto l = L(L(1), 2, L(L(3,4), 5), L(L(L())), L(L(L(6))),7,8,L()); l.writeln; l.flatten.writeln; }
#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; } }
Write the same algorithm in Java as shown in this D implementation.
import std.stdio, std.algorithm, std.conv, std.range; struct TreeList(T) { union { TreeList[] arr; T data; } bool isArray = true; static TreeList opCall(A...)(A items) pure nothrow { TreeList result; foreach (i, el; items) static if (is(A[i] == T)) { TreeList item; item.isArray = false; item.data = el; result.arr ~= item; } else result.arr ~= el; return result; } string toString() const pure { return isArray ? arr.text : data.text; } } T[] flatten(T)(in TreeList!T t) pure nothrow { if (t.isArray) return t.arr.map!flatten.join; else return [t.data]; } void main() { alias TreeList!int L; static assert(L.sizeof == 12); auto l = L(L(1), 2, L(L(3,4), 5), L(L(L())), L(L(L(6))),7,8,L()); l.writeln; l.flatten.writeln; }
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); } } } }
Maintain the same structure and functionality when rewriting this code in Java.
import std.stdio, std.algorithm, std.conv, std.range; struct TreeList(T) { union { TreeList[] arr; T data; } bool isArray = true; static TreeList opCall(A...)(A items) pure nothrow { TreeList result; foreach (i, el; items) static if (is(A[i] == T)) { TreeList item; item.isArray = false; item.data = el; result.arr ~= item; } else result.arr ~= el; return result; } string toString() const pure { return isArray ? arr.text : data.text; } } T[] flatten(T)(in TreeList!T t) pure nothrow { if (t.isArray) return t.arr.map!flatten.join; else return [t.data]; } void main() { alias TreeList!int L; static assert(L.sizeof == 12); auto l = L(L(1), 2, L(L(3,4), 5), L(L(L())), L(L(L(6))),7,8,L()); l.writeln; l.flatten.writeln; }
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); } } } }
Convert this D block to Python, preserving its control flow and logic.
import std.stdio, std.algorithm, std.conv, std.range; struct TreeList(T) { union { TreeList[] arr; T data; } bool isArray = true; static TreeList opCall(A...)(A items) pure nothrow { TreeList result; foreach (i, el; items) static if (is(A[i] == T)) { TreeList item; item.isArray = false; item.data = el; result.arr ~= item; } else result.arr ~= el; return result; } string toString() const pure { return isArray ? arr.text : data.text; } } T[] flatten(T)(in TreeList!T t) pure nothrow { if (t.isArray) return t.arr.map!flatten.join; else return [t.data]; } void main() { alias TreeList!int L; static assert(L.sizeof == 12); auto l = L(L(1), 2, L(L(3,4), 5), L(L(L())), L(L(L(6))),7,8,L()); l.writeln; l.flatten.writeln; }
>>> 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]
Write the same code in Python as shown below in D.
import std.stdio, std.algorithm, std.conv, std.range; struct TreeList(T) { union { TreeList[] arr; T data; } bool isArray = true; static TreeList opCall(A...)(A items) pure nothrow { TreeList result; foreach (i, el; items) static if (is(A[i] == T)) { TreeList item; item.isArray = false; item.data = el; result.arr ~= item; } else result.arr ~= el; return result; } string toString() const pure { return isArray ? arr.text : data.text; } } T[] flatten(T)(in TreeList!T t) pure nothrow { if (t.isArray) return t.arr.map!flatten.join; else return [t.data]; } void main() { alias TreeList!int L; static assert(L.sizeof == 12); auto l = L(L(1), 2, L(L(3,4), 5), L(L(L())), L(L(L(6))),7,8,L()); l.writeln; l.flatten.writeln; }
>>> 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]
Maintain the same structure and functionality when rewriting this code in VB.
import std.stdio, std.algorithm, std.conv, std.range; struct TreeList(T) { union { TreeList[] arr; T data; } bool isArray = true; static TreeList opCall(A...)(A items) pure nothrow { TreeList result; foreach (i, el; items) static if (is(A[i] == T)) { TreeList item; item.isArray = false; item.data = el; result.arr ~= item; } else result.arr ~= el; return result; } string toString() const pure { return isArray ? arr.text : data.text; } } T[] flatten(T)(in TreeList!T t) pure nothrow { if (t.isArray) return t.arr.map!flatten.join; else return [t.data]; } void main() { alias TreeList!int L; static assert(L.sizeof == 12); auto l = L(L(1), 2, L(L(3,4), 5), L(L(L())), L(L(L(6))),7,8,L()); l.writeln; l.flatten.writeln; }
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
Can you help me rewrite this code in VB instead of D, keeping it the same logically?
import std.stdio, std.algorithm, std.conv, std.range; struct TreeList(T) { union { TreeList[] arr; T data; } bool isArray = true; static TreeList opCall(A...)(A items) pure nothrow { TreeList result; foreach (i, el; items) static if (is(A[i] == T)) { TreeList item; item.isArray = false; item.data = el; result.arr ~= item; } else result.arr ~= el; return result; } string toString() const pure { return isArray ? arr.text : data.text; } } T[] flatten(T)(in TreeList!T t) pure nothrow { if (t.isArray) return t.arr.map!flatten.join; else return [t.data]; } void main() { alias TreeList!int L; static assert(L.sizeof == 12); auto l = L(L(1), 2, L(L(3,4), 5), L(L(L())), L(L(L(6))),7,8,L()); l.writeln; l.flatten.writeln; }
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
Port the provided D code into Go while preserving the original functionality.
import std.stdio, std.algorithm, std.conv, std.range; struct TreeList(T) { union { TreeList[] arr; T data; } bool isArray = true; static TreeList opCall(A...)(A items) pure nothrow { TreeList result; foreach (i, el; items) static if (is(A[i] == T)) { TreeList item; item.isArray = false; item.data = el; result.arr ~= item; } else result.arr ~= el; return result; } string toString() const pure { return isArray ? arr.text : data.text; } } T[] flatten(T)(in TreeList!T t) pure nothrow { if (t.isArray) return t.arr.map!flatten.join; else return [t.data]; } void main() { alias TreeList!int L; static assert(L.sizeof == 12); auto l = L(L(1), 2, L(L(3,4), 5), L(L(L())), L(L(L(6))),7,8,L()); l.writeln; l.flatten.writeln; }
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 }
Change the following D code into Go without altering its purpose.
import std.stdio, std.algorithm, std.conv, std.range; struct TreeList(T) { union { TreeList[] arr; T data; } bool isArray = true; static TreeList opCall(A...)(A items) pure nothrow { TreeList result; foreach (i, el; items) static if (is(A[i] == T)) { TreeList item; item.isArray = false; item.data = el; result.arr ~= item; } else result.arr ~= el; return result; } string toString() const pure { return isArray ? arr.text : data.text; } } T[] flatten(T)(in TreeList!T t) pure nothrow { if (t.isArray) return t.arr.map!flatten.join; else return [t.data]; } void main() { alias TreeList!int L; static assert(L.sizeof == 12); auto l = L(L(1), 2, L(L(3,4), 5), L(L(L())), L(L(L(6))),7,8,L()); l.writeln; l.flatten.writeln; }
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 }
Translate this program into C but keep the logic exactly as in Elixir.
defmodule RC do def flatten([]), do: [] def flatten([h|t]), do: flatten(h) ++ flatten(t) def flatten(h), do: [h] end list = [[1], 2, [[3,4], 5], [[[]]], [[[6]]], 7, 8, []] IO.inspect RC.flatten(list) IO.inspect List.flatten(list)
#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; }
Write a version of this Elixir function in C with identical behavior.
defmodule RC do def flatten([]), do: [] def flatten([h|t]), do: flatten(h) ++ flatten(t) def flatten(h), do: [h] end list = [[1], 2, [[3,4], 5], [[[]]], [[[6]]], 7, 8, []] IO.inspect RC.flatten(list) IO.inspect List.flatten(list)
#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; }
Write the same algorithm in C# as shown in this Elixir implementation.
defmodule RC do def flatten([]), do: [] def flatten([h|t]), do: flatten(h) ++ flatten(t) def flatten(h), do: [h] end list = [[1], 2, [[3,4], 5], [[[]]], [[[6]]], 7, 8, []] IO.inspect RC.flatten(list) IO.inspect List.flatten(list)
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; } } }
Please provide an equivalent version of this Elixir code in C#.
defmodule RC do def flatten([]), do: [] def flatten([h|t]), do: flatten(h) ++ flatten(t) def flatten(h), do: [h] end list = [[1], 2, [[3,4], 5], [[[]]], [[[6]]], 7, 8, []] IO.inspect RC.flatten(list) IO.inspect List.flatten(list)
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; } } }
Write a version of this Elixir function in C++ with identical behavior.
defmodule RC do def flatten([]), do: [] def flatten([h|t]), do: flatten(h) ++ flatten(t) def flatten(h), do: [h] end list = [[1], 2, [[3,4], 5], [[[]]], [[[6]]], 7, 8, []] IO.inspect RC.flatten(list) IO.inspect List.flatten(list)
#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; } }
Port the provided Elixir code into C++ while preserving the original functionality.
defmodule RC do def flatten([]), do: [] def flatten([h|t]), do: flatten(h) ++ flatten(t) def flatten(h), do: [h] end list = [[1], 2, [[3,4], 5], [[[]]], [[[6]]], 7, 8, []] IO.inspect RC.flatten(list) IO.inspect List.flatten(list)
#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; } }
Generate a Java translation of this Elixir snippet without changing its computational steps.
defmodule RC do def flatten([]), do: [] def flatten([h|t]), do: flatten(h) ++ flatten(t) def flatten(h), do: [h] end list = [[1], 2, [[3,4], 5], [[[]]], [[[6]]], 7, 8, []] IO.inspect RC.flatten(list) IO.inspect List.flatten(list)
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); } } } }
Rewrite the snippet below in Java so it works the same as the original Elixir code.
defmodule RC do def flatten([]), do: [] def flatten([h|t]), do: flatten(h) ++ flatten(t) def flatten(h), do: [h] end list = [[1], 2, [[3,4], 5], [[[]]], [[[6]]], 7, 8, []] IO.inspect RC.flatten(list) IO.inspect List.flatten(list)
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); } } } }
Translate the given Elixir code snippet into Python without altering its behavior.
defmodule RC do def flatten([]), do: [] def flatten([h|t]), do: flatten(h) ++ flatten(t) def flatten(h), do: [h] end list = [[1], 2, [[3,4], 5], [[[]]], [[[6]]], 7, 8, []] IO.inspect RC.flatten(list) IO.inspect List.flatten(list)
>>> 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]
Generate an equivalent Python version of this Elixir code.
defmodule RC do def flatten([]), do: [] def flatten([h|t]), do: flatten(h) ++ flatten(t) def flatten(h), do: [h] end list = [[1], 2, [[3,4], 5], [[[]]], [[[6]]], 7, 8, []] IO.inspect RC.flatten(list) IO.inspect List.flatten(list)
>>> 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]
Please provide an equivalent version of this Elixir code in VB.
defmodule RC do def flatten([]), do: [] def flatten([h|t]), do: flatten(h) ++ flatten(t) def flatten(h), do: [h] end list = [[1], 2, [[3,4], 5], [[[]]], [[[6]]], 7, 8, []] IO.inspect RC.flatten(list) IO.inspect List.flatten(list)
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
Generate a VB translation of this Elixir snippet without changing its computational steps.
defmodule RC do def flatten([]), do: [] def flatten([h|t]), do: flatten(h) ++ flatten(t) def flatten(h), do: [h] end list = [[1], 2, [[3,4], 5], [[[]]], [[[6]]], 7, 8, []] IO.inspect RC.flatten(list) IO.inspect List.flatten(list)
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
Can you help me rewrite this code in Go instead of Elixir, keeping it the same logically?
defmodule RC do def flatten([]), do: [] def flatten([h|t]), do: flatten(h) ++ flatten(t) def flatten(h), do: [h] end list = [[1], 2, [[3,4], 5], [[[]]], [[[6]]], 7, 8, []] IO.inspect RC.flatten(list) IO.inspect List.flatten(list)
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 }
Translate this program into Go but keep the logic exactly as in Elixir.
defmodule RC do def flatten([]), do: [] def flatten([h|t]), do: flatten(h) ++ flatten(t) def flatten(h), do: [h] end list = [[1], 2, [[3,4], 5], [[[]]], [[[6]]], 7, 8, []] IO.inspect RC.flatten(list) IO.inspect List.flatten(list)
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 }
Generate an equivalent C version of this Erlang code.
flatten([]) -> []; flatten([H|T]) -> flatten(H) ++ flatten(T); flatten(H) -> [H].
#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; }
Transform the following Erlang implementation into C, maintaining the same output and logic.
flatten([]) -> []; flatten([H|T]) -> flatten(H) ++ flatten(T); flatten(H) -> [H].
#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; }
Write a version of this Erlang function in C# with identical behavior.
flatten([]) -> []; flatten([H|T]) -> flatten(H) ++ flatten(T); flatten(H) -> [H].
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; } } }
Preserve the algorithm and functionality while converting the code from Erlang to C#.
flatten([]) -> []; flatten([H|T]) -> flatten(H) ++ flatten(T); flatten(H) -> [H].
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; } } }
Port the provided Erlang code into C++ while preserving the original functionality.
flatten([]) -> []; flatten([H|T]) -> flatten(H) ++ flatten(T); flatten(H) -> [H].
#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; } }
Translate this program into C++ but keep the logic exactly as in Erlang.
flatten([]) -> []; flatten([H|T]) -> flatten(H) ++ flatten(T); flatten(H) -> [H].
#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; } }
Convert the following code from Erlang to Java, ensuring the logic remains intact.
flatten([]) -> []; flatten([H|T]) -> flatten(H) ++ flatten(T); flatten(H) -> [H].
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); } } } }
Port the following code from Erlang to Java with equivalent syntax and logic.
flatten([]) -> []; flatten([H|T]) -> flatten(H) ++ flatten(T); flatten(H) -> [H].
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); } } } }
Generate a Python translation of this Erlang snippet without changing its computational steps.
flatten([]) -> []; flatten([H|T]) -> flatten(H) ++ flatten(T); flatten(H) -> [H].
>>> 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]
Generate an equivalent Python version of this Erlang code.
flatten([]) -> []; flatten([H|T]) -> flatten(H) ++ flatten(T); flatten(H) -> [H].
>>> 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]
Rewrite the snippet below in VB so it works the same as the original Erlang code.
flatten([]) -> []; flatten([H|T]) -> flatten(H) ++ flatten(T); flatten(H) -> [H].
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
Maintain the same structure and functionality when rewriting this code in VB.
flatten([]) -> []; flatten([H|T]) -> flatten(H) ++ flatten(T); flatten(H) -> [H].
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
Produce a functionally identical Go code for the snippet given in Erlang.
flatten([]) -> []; flatten([H|T]) -> flatten(H) ++ flatten(T); flatten(H) -> [H].
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 }
Keep all operations the same but rewrite the snippet in Go.
flatten([]) -> []; flatten([H|T]) -> flatten(H) ++ flatten(T); flatten(H) -> [H].
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 }
Produce a language-to-language conversion: from F# to C, same semantics.
type 'a ll = | I of 'a | L of 'a ll list let rec flatten = function | [] -> [] | (I x)::y -> x :: (flatten y) | (L x)::y -> List.append (flatten x) (flatten y) printfn "%A" (flatten [L([I(1)]); I(2); L([L([I(3);I(4)]); I(5)]); L([L([L([])])]); L([L([L([I(6)])])]); I(7); I(8); L([])])
#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; }
Rewrite this program in C while keeping its functionality equivalent to the F# version.
type 'a ll = | I of 'a | L of 'a ll list let rec flatten = function | [] -> [] | (I x)::y -> x :: (flatten y) | (L x)::y -> List.append (flatten x) (flatten y) printfn "%A" (flatten [L([I(1)]); I(2); L([L([I(3);I(4)]); I(5)]); L([L([L([])])]); L([L([L([I(6)])])]); I(7); I(8); L([])])
#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; }
Transform the following F# implementation into C#, maintaining the same output and logic.
type 'a ll = | I of 'a | L of 'a ll list let rec flatten = function | [] -> [] | (I x)::y -> x :: (flatten y) | (L x)::y -> List.append (flatten x) (flatten y) printfn "%A" (flatten [L([I(1)]); I(2); L([L([I(3);I(4)]); I(5)]); L([L([L([])])]); L([L([L([I(6)])])]); I(7); I(8); L([])])
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; } } }
Produce a language-to-language conversion: from F# to C#, same semantics.
type 'a ll = | I of 'a | L of 'a ll list let rec flatten = function | [] -> [] | (I x)::y -> x :: (flatten y) | (L x)::y -> List.append (flatten x) (flatten y) printfn "%A" (flatten [L([I(1)]); I(2); L([L([I(3);I(4)]); I(5)]); L([L([L([])])]); L([L([L([I(6)])])]); I(7); I(8); L([])])
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; } } }