Instruction
stringlengths
45
106
input_code
stringlengths
1
13.7k
output_code
stringlengths
1
13.7k
Maintain the same structure and functionality when rewriting this code in C++.
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 <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; ...
Change the programming language of this snippet from F# to C++ without modifying what it does.
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 <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; ...
Ensure the translated Java code behaves exactly like the original F# snippet.
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([...
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 (...
Preserve the algorithm and functionality while converting the code from F# to Java.
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([...
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 (...
Write a version of this F# function in Python with identical behavior.
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([...
>>> 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 this program in Python 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([...
>>> 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 VB version of this F# code.
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([...
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 f...
Produce a language-to-language conversion: from F# to VB, 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([...
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 f...
Produce a language-to-language conversion: from F# to Go, 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([...
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...
Can you help me rewrite this code in Go instead of F#, keeping it the same logically?
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([...
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...
Change the following Forth code into C without altering its purpose.
: then drop ) a:each drop ; : flatten [] >r r> ; [[1], 2, [[3,4], 5], [[[]]], [[[6]]], 7, 8, []] dup . cr flatten . cr bye
#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...
Maintain the same structure and functionality when rewriting this code in C.
: then drop ) a:each drop ; : flatten [] >r r> ; [[1], 2, [[3,4], 5], [[[]]], [[[6]]], 7, 8, []] dup . cr flatten . cr bye
#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...
Convert the following code from Forth to C#, ensuring the logic remains intact.
: then drop ) a:each drop ; : flatten [] >r r> ; [[1], 2, [[3,4], 5], [[[]]], [[[6]]], 7, 8, []] dup . cr flatten . cr bye
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...
Change the following Forth code into C# without altering its purpose.
: then drop ) a:each drop ; : flatten [] >r r> ; [[1], 2, [[3,4], 5], [[[]]], [[[6]]], 7, 8, []] dup . cr flatten . cr bye
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...
Can you help me rewrite this code in C++ instead of Forth, keeping it the same logically?
: then drop ) a:each drop ; : flatten [] >r r> ; [[1], 2, [[3,4], 5], [[[]]], [[[6]]], 7, 8, []] dup . cr flatten . cr bye
#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; ...
Maintain the same structure and functionality when rewriting this code in C++.
: then drop ) a:each drop ; : flatten [] >r r> ; [[1], 2, [[3,4], 5], [[[]]], [[[6]]], 7, 8, []] dup . cr flatten . cr bye
#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; ...
Preserve the algorithm and functionality while converting the code from Forth to Java.
: then drop ) a:each drop ; : flatten [] >r r> ; [[1], 2, [[3,4], 5], [[[]]], [[[6]]], 7, 8, []] dup . cr flatten . cr bye
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 (...
Ensure the translated Java code behaves exactly like the original Forth snippet.
: then drop ) a:each drop ; : flatten [] >r r> ; [[1], 2, [[3,4], 5], [[[]]], [[[6]]], 7, 8, []] dup . cr flatten . cr bye
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 (...
Maintain the same structure and functionality when rewriting this code in Python.
: then drop ) a:each drop ; : flatten [] >r r> ; [[1], 2, [[3,4], 5], [[[]]], [[[6]]], 7, 8, []] dup . cr flatten . cr bye
>>> 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 Forth implementation into Python, maintaining the same output and logic.
: then drop ) a:each drop ; : flatten [] >r r> ; [[1], 2, [[3,4], 5], [[[]]], [[[6]]], 7, 8, []] dup . cr flatten . cr bye
>>> 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]
Can you help me rewrite this code in VB instead of Forth, keeping it the same logically?
: then drop ) a:each drop ; : flatten [] >r r> ; [[1], 2, [[3,4], 5], [[[]]], [[[6]]], 7, 8, []] dup . cr flatten . cr bye
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 f...
Write the same code in VB as shown below in Forth.
: then drop ) a:each drop ; : flatten [] >r r> ; [[1], 2, [[3,4], 5], [[[]]], [[[6]]], 7, 8, []] dup . cr flatten . cr bye
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 f...
Convert this Forth block to Go, preserving its control flow and logic.
: then drop ) a:each drop ; : flatten [] >r r> ; [[1], 2, [[3,4], 5], [[[]]], [[[6]]], 7, 8, []] dup . cr flatten . cr bye
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...
Translate the given Forth code snippet into Go without altering its behavior.
: then drop ) a:each drop ; : flatten [] >r r> ; [[1], 2, [[3,4], 5], [[[]]], [[[6]]], 7, 8, []] dup . cr flatten . cr bye
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...
Translate the given Fortran code snippet into C# without altering its behavior.
module flat implicit none type n integer :: a type(n), dimension(:), pointer :: p => null() logical :: empty = .false. end type contains recursive subroutine del(this) type(n), intent(inout) :: this integer :: i...
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...
Maintain the same structure and functionality when rewriting this code in C#.
module flat implicit none type n integer :: a type(n), dimension(:), pointer :: p => null() logical :: empty = .false. end type contains recursive subroutine del(this) type(n), intent(inout) :: this integer :: i...
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...
Ensure the translated C++ code behaves exactly like the original Fortran snippet.
module flat implicit none type n integer :: a type(n), dimension(:), pointer :: p => null() logical :: empty = .false. end type contains recursive subroutine del(this) type(n), intent(inout) :: this integer :: i...
#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; ...
Port the following code from Fortran to C++ with equivalent syntax and logic.
module flat implicit none type n integer :: a type(n), dimension(:), pointer :: p => null() logical :: empty = .false. end type contains recursive subroutine del(this) type(n), intent(inout) :: this integer :: i...
#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; ...
Can you help me rewrite this code in C instead of Fortran, keeping it the same logically?
module flat implicit none type n integer :: a type(n), dimension(:), pointer :: p => null() logical :: empty = .false. end type contains recursive subroutine del(this) type(n), intent(inout) :: this integer :: i...
#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...
Can you help me rewrite this code in C instead of Fortran, keeping it the same logically?
module flat implicit none type n integer :: a type(n), dimension(:), pointer :: p => null() logical :: empty = .false. end type contains recursive subroutine del(this) type(n), intent(inout) :: this integer :: i...
#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...
Rewrite this program in Go while keeping its functionality equivalent to the Fortran version.
module flat implicit none type n integer :: a type(n), dimension(:), pointer :: p => null() logical :: empty = .false. end type contains recursive subroutine del(this) type(n), intent(inout) :: this integer :: i...
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...
Port the provided Fortran code into Java while preserving the original functionality.
module flat implicit none type n integer :: a type(n), dimension(:), pointer :: p => null() logical :: empty = .false. end type contains recursive subroutine del(this) type(n), intent(inout) :: this integer :: i...
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 (...
Change the following Fortran code into Java without altering its purpose.
module flat implicit none type n integer :: a type(n), dimension(:), pointer :: p => null() logical :: empty = .false. end type contains recursive subroutine del(this) type(n), intent(inout) :: this integer :: i...
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 (...
Transform the following Fortran implementation into Python, maintaining the same output and logic.
module flat implicit none type n integer :: a type(n), dimension(:), pointer :: p => null() logical :: empty = .false. end type contains recursive subroutine del(this) type(n), intent(inout) :: this integer :: i...
>>> 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 Fortran to Python with equivalent syntax and logic.
module flat implicit none type n integer :: a type(n), dimension(:), pointer :: p => null() logical :: empty = .false. end type contains recursive subroutine del(this) type(n), intent(inout) :: this integer :: i...
>>> 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]
Translate this program into VB but keep the logic exactly as in Fortran.
module flat implicit none type n integer :: a type(n), dimension(:), pointer :: p => null() logical :: empty = .false. end type contains recursive subroutine del(this) type(n), intent(inout) :: this integer :: i...
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 f...
Produce a functionally identical VB code for the snippet given in Fortran.
module flat implicit none type n integer :: a type(n), dimension(:), pointer :: p => null() logical :: empty = .false. end type contains recursive subroutine del(this) type(n), intent(inout) :: this integer :: i...
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 f...
Generate an equivalent PHP version of this Fortran code.
module flat implicit none type n integer :: a type(n), dimension(:), pointer :: p => null() logical :: empty = .false. end type contains recursive subroutine del(this) type(n), intent(inout) :: this integer :: i...
while (array_filter($lst, 'is_array')) $lst = call_user_func_array('array_merge', $lst);
Can you help me rewrite this code in PHP instead of Fortran, keeping it the same logically?
module flat implicit none type n integer :: a type(n), dimension(:), pointer :: p => null() logical :: empty = .false. end type contains recursive subroutine del(this) type(n), intent(inout) :: this integer :: i...
while (array_filter($lst, 'is_array')) $lst = call_user_func_array('array_merge', $lst);
Change the following Groovy code into C without altering its purpose.
assert [[1], 2, [[3,4], 5], [[[]]], [[[6]]], 7, 8, []].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...
Convert this Groovy block to C, preserving its control flow and logic.
assert [[1], 2, [[3,4], 5], [[[]]], [[[6]]], 7, 8, []].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...
Translate the given Groovy code snippet into C# without altering its behavior.
assert [[1], 2, [[3,4], 5], [[[]]], [[[6]]], 7, 8, []].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...
Preserve the algorithm and functionality while converting the code from Groovy to C#.
assert [[1], 2, [[3,4], 5], [[[]]], [[[6]]], 7, 8, []].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...
Preserve the algorithm and functionality while converting the code from Groovy to C++.
assert [[1], 2, [[3,4], 5], [[[]]], [[[6]]], 7, 8, []].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; ...
Generate a C++ translation of this Groovy snippet without changing its computational steps.
assert [[1], 2, [[3,4], 5], [[[]]], [[[6]]], 7, 8, []].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; ...
Preserve the algorithm and functionality while converting the code from Groovy to Java.
assert [[1], 2, [[3,4], 5], [[[]]], [[[6]]], 7, 8, []].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 (...
Generate a Java translation of this Groovy snippet without changing its computational steps.
assert [[1], 2, [[3,4], 5], [[[]]], [[[6]]], 7, 8, []].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 (...
Preserve the algorithm and functionality while converting the code from Groovy to Python.
assert [[1], 2, [[3,4], 5], [[[]]], [[[6]]], 7, 8, []].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]
Ensure the translated Python code behaves exactly like the original Groovy snippet.
assert [[1], 2, [[3,4], 5], [[[]]], [[[6]]], 7, 8, []].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]
Rewrite the snippet below in VB so it works the same as the original Groovy code.
assert [[1], 2, [[3,4], 5], [[[]]], [[[6]]], 7, 8, []].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 f...
Produce a functionally identical VB code for the snippet given in Groovy.
assert [[1], 2, [[3,4], 5], [[[]]], [[[6]]], 7, 8, []].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 f...
Produce a functionally identical Go code for the snippet given in Groovy.
assert [[1], 2, [[3,4], 5], [[[]]], [[[6]]], 7, 8, []].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...
Change the following Groovy code into Go without altering its purpose.
assert [[1], 2, [[3,4], 5], [[[]]], [[[6]]], 7, 8, []].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...
Translate the given Haskell code snippet into C without altering its behavior.
import Graphics.Element exposing (show) type Tree a = Leaf a | Node (List (Tree a)) flatten : Tree a -> List a flatten tree = case tree of Leaf a -> [a] Node list -> List.concatMap flatten list tree : Tree Int tree = Node [ Node [Leaf 1] , Leaf 2 , Node [Node [Leaf 3, Leaf 4], Leaf 5] , Node [...
#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...
Generate a C translation of this Haskell snippet without changing its computational steps.
import Graphics.Element exposing (show) type Tree a = Leaf a | Node (List (Tree a)) flatten : Tree a -> List a flatten tree = case tree of Leaf a -> [a] Node list -> List.concatMap flatten list tree : Tree Int tree = Node [ Node [Leaf 1] , Leaf 2 , Node [Node [Leaf 3, Leaf 4], Leaf 5] , Node [...
#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...
Translate the given Haskell code snippet into C# without altering its behavior.
import Graphics.Element exposing (show) type Tree a = Leaf a | Node (List (Tree a)) flatten : Tree a -> List a flatten tree = case tree of Leaf a -> [a] Node list -> List.concatMap flatten list tree : Tree Int tree = Node [ Node [Leaf 1] , Leaf 2 , Node [Node [Leaf 3, Leaf 4], Leaf 5] , Node [...
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...
Keep all operations the same but rewrite the snippet in C#.
import Graphics.Element exposing (show) type Tree a = Leaf a | Node (List (Tree a)) flatten : Tree a -> List a flatten tree = case tree of Leaf a -> [a] Node list -> List.concatMap flatten list tree : Tree Int tree = Node [ Node [Leaf 1] , Leaf 2 , Node [Node [Leaf 3, Leaf 4], Leaf 5] , Node [...
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...
Can you help me rewrite this code in C++ instead of Haskell, keeping it the same logically?
import Graphics.Element exposing (show) type Tree a = Leaf a | Node (List (Tree a)) flatten : Tree a -> List a flatten tree = case tree of Leaf a -> [a] Node list -> List.concatMap flatten list tree : Tree Int tree = Node [ Node [Leaf 1] , Leaf 2 , Node [Node [Leaf 3, Leaf 4], Leaf 5] , Node [...
#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; ...
Rewrite the snippet below in C++ so it works the same as the original Haskell code.
import Graphics.Element exposing (show) type Tree a = Leaf a | Node (List (Tree a)) flatten : Tree a -> List a flatten tree = case tree of Leaf a -> [a] Node list -> List.concatMap flatten list tree : Tree Int tree = Node [ Node [Leaf 1] , Leaf 2 , Node [Node [Leaf 3, Leaf 4], Leaf 5] , Node [...
#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; ...
Can you help me rewrite this code in Java instead of Haskell, keeping it the same logically?
import Graphics.Element exposing (show) type Tree a = Leaf a | Node (List (Tree a)) flatten : Tree a -> List a flatten tree = case tree of Leaf a -> [a] Node list -> List.concatMap flatten list tree : Tree Int tree = Node [ Node [Leaf 1] , Leaf 2 , Node [Node [Leaf 3, Leaf 4], Leaf 5] , Node [...
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 (...
Generate an equivalent Java version of this Haskell code.
import Graphics.Element exposing (show) type Tree a = Leaf a | Node (List (Tree a)) flatten : Tree a -> List a flatten tree = case tree of Leaf a -> [a] Node list -> List.concatMap flatten list tree : Tree Int tree = Node [ Node [Leaf 1] , Leaf 2 , Node [Node [Leaf 3, Leaf 4], Leaf 5] , Node [...
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 (...
Convert this Haskell block to Python, preserving its control flow and logic.
import Graphics.Element exposing (show) type Tree a = Leaf a | Node (List (Tree a)) flatten : Tree a -> List a flatten tree = case tree of Leaf a -> [a] Node list -> List.concatMap flatten list tree : Tree Int tree = Node [ Node [Leaf 1] , Leaf 2 , Node [Node [Leaf 3, Leaf 4], Leaf 5] , Node [...
>>> 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 Haskell snippet to Python and keep its semantics consistent.
import Graphics.Element exposing (show) type Tree a = Leaf a | Node (List (Tree a)) flatten : Tree a -> List a flatten tree = case tree of Leaf a -> [a] Node list -> List.concatMap flatten list tree : Tree Int tree = Node [ Node [Leaf 1] , Leaf 2 , Node [Node [Leaf 3, Leaf 4], Leaf 5] , Node [...
>>> 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 Haskell code.
import Graphics.Element exposing (show) type Tree a = Leaf a | Node (List (Tree a)) flatten : Tree a -> List a flatten tree = case tree of Leaf a -> [a] Node list -> List.concatMap flatten list tree : Tree Int tree = Node [ Node [Leaf 1] , Leaf 2 , Node [Node [Leaf 3, Leaf 4], Leaf 5] , Node [...
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 f...
Write the same code in VB as shown below in Haskell.
import Graphics.Element exposing (show) type Tree a = Leaf a | Node (List (Tree a)) flatten : Tree a -> List a flatten tree = case tree of Leaf a -> [a] Node list -> List.concatMap flatten list tree : Tree Int tree = Node [ Node [Leaf 1] , Leaf 2 , Node [Node [Leaf 3, Leaf 4], Leaf 5] , Node [...
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 f...
Keep all operations the same but rewrite the snippet in Go.
import Graphics.Element exposing (show) type Tree a = Leaf a | Node (List (Tree a)) flatten : Tree a -> List a flatten tree = case tree of Leaf a -> [a] Node list -> List.concatMap flatten list tree : Tree Int tree = Node [ Node [Leaf 1] , Leaf 2 , Node [Node [Leaf 3, Leaf 4], Leaf 5] , Node [...
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...
Change the following Haskell code into Go without altering its purpose.
import Graphics.Element exposing (show) type Tree a = Leaf a | Node (List (Tree a)) flatten : Tree a -> List a flatten tree = case tree of Leaf a -> [a] Node list -> List.concatMap flatten list tree : Tree Int tree = Node [ Node [Leaf 1] , Leaf 2 , Node [Node [Leaf 3, Leaf 4], Leaf 5] , Node [...
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...
Generate a C translation of this Icon snippet without changing its computational steps.
link strings procedure sflatten(s) return pretrim(trim(compress(deletec(s,'[ ]'),',') ,','),',') end
#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...
Convert the following code from Icon to C, ensuring the logic remains intact.
link strings procedure sflatten(s) return pretrim(trim(compress(deletec(s,'[ ]'),',') ,','),',') end
#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...
Keep all operations the same but rewrite the snippet in C#.
link strings procedure sflatten(s) return pretrim(trim(compress(deletec(s,'[ ]'),',') ,','),',') end
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...
Translate the given Icon code snippet into C# without altering its behavior.
link strings procedure sflatten(s) return pretrim(trim(compress(deletec(s,'[ ]'),',') ,','),',') end
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...
Convert the following code from Icon to C++, ensuring the logic remains intact.
link strings procedure sflatten(s) return pretrim(trim(compress(deletec(s,'[ ]'),',') ,','),',') end
#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; ...
Generate an equivalent C++ version of this Icon code.
link strings procedure sflatten(s) return pretrim(trim(compress(deletec(s,'[ ]'),',') ,','),',') end
#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; ...
Port the provided Icon code into Java while preserving the original functionality.
link strings procedure sflatten(s) return pretrim(trim(compress(deletec(s,'[ ]'),',') ,','),',') end
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 (...
Produce a functionally identical Java code for the snippet given in Icon.
link strings procedure sflatten(s) return pretrim(trim(compress(deletec(s,'[ ]'),',') ,','),',') end
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 (...
Please provide an equivalent version of this Icon code in Python.
link strings procedure sflatten(s) return pretrim(trim(compress(deletec(s,'[ ]'),',') ,','),',') end
>>> 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 a VB translation of this Icon snippet without changing its computational steps.
link strings procedure sflatten(s) return pretrim(trim(compress(deletec(s,'[ ]'),',') ,','),',') end
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 f...
Preserve the algorithm and functionality while converting the code from Icon to VB.
link strings procedure sflatten(s) return pretrim(trim(compress(deletec(s,'[ ]'),',') ,','),',') end
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 f...
Can you help me rewrite this code in Go instead of Icon, keeping it the same logically?
link strings procedure sflatten(s) return pretrim(trim(compress(deletec(s,'[ ]'),',') ,','),',') end
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...
Rewrite this program in Go while keeping its functionality equivalent to the Icon version.
link strings procedure sflatten(s) return pretrim(trim(compress(deletec(s,'[ ]'),',') ,','),',') end
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...
Change the following J code into C without altering its purpose.
flatten =: [: ; <S:0
#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...
Ensure the translated C code behaves exactly like the original J snippet.
flatten =: [: ; <S:0
#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...
Can you help me rewrite this code in C# instead of J, keeping it the same logically?
flatten =: [: ; <S:0
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...
Convert this J block to C#, preserving its control flow and logic.
flatten =: [: ; <S:0
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...
Change the following J code into C++ without altering its purpose.
flatten =: [: ; <S:0
#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; ...
Translate this program into C++ but keep the logic exactly as in J.
flatten =: [: ; <S:0
#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; ...
Convert this J snippet to Java and keep its semantics consistent.
flatten =: [: ; <S:0
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 (...
Generate an equivalent Java version of this J code.
flatten =: [: ; <S:0
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 (...
Rewrite the snippet below in Python so it works the same as the original J code.
flatten =: [: ; <S:0
>>> 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 algorithm in Python as shown in this J implementation.
flatten =: [: ; <S:0
>>> 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 J implementation into VB, maintaining the same output and logic.
flatten =: [: ; <S:0
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 f...
Rewrite the snippet below in VB so it works the same as the original J code.
flatten =: [: ; <S:0
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 f...
Write the same algorithm in Go as shown in this J implementation.
flatten =: [: ; <S:0
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...
Write the same code in Go as shown below in J.
flatten =: [: ; <S:0
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...
Generate a C translation of this Julia snippet without changing its computational steps.
isflat(x) = isempty(x) || first(x) === x function flat_mapreduce(arr) mapreduce(vcat, arr, init=[]) do x isflat(x) ? x : flat(x) end end
#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...
Write the same code in C as shown below in Julia.
isflat(x) = isempty(x) || first(x) === x function flat_mapreduce(arr) mapreduce(vcat, arr, init=[]) do x isflat(x) ? x : flat(x) end end
#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...
Produce a language-to-language conversion: from Julia to C#, same semantics.
isflat(x) = isempty(x) || first(x) === x function flat_mapreduce(arr) mapreduce(vcat, arr, init=[]) do x isflat(x) ? x : flat(x) end end
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...
Rewrite this program in C# while keeping its functionality equivalent to the Julia version.
isflat(x) = isempty(x) || first(x) === x function flat_mapreduce(arr) mapreduce(vcat, arr, init=[]) do x isflat(x) ? x : flat(x) end end
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...
Write a version of this Julia function in C++ with identical behavior.
isflat(x) = isempty(x) || first(x) === x function flat_mapreduce(arr) mapreduce(vcat, arr, init=[]) do x isflat(x) ? x : flat(x) end end
#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; ...
Ensure the translated C++ code behaves exactly like the original Julia snippet.
isflat(x) = isempty(x) || first(x) === x function flat_mapreduce(arr) mapreduce(vcat, arr, init=[]) do x isflat(x) ? x : flat(x) end end
#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; ...