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;
++next;
list.splice(next, boost::any_cast<anylist&>(*current));
current = list.erase(current);
}
else
++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;
++next;
list.splice(next, boost::any_cast<anylist&>(*current));
current = list.erase(current);
}
else
++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 (Object item : fromTreeList) {
if (item instanceof List<?>) {
flatten((List<?>) item, toFlatList);
} else {
toFlatList.add(item);
}
}
}
}
|
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 (Object item : fromTreeList) {
if (item instanceof List<?>) {
flatten((List<?>) item, toFlatList);
} else {
toFlatList.add(item);
}
}
}
}
|
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 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 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 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 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 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
}
|
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 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 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(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;
}
|
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(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 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 index = NewList.IndexOf ( NewList.OfType<ArrayList> ( ).ElementAt ( 0 ) );
ArrayList Temp = (ArrayList)NewList[index];
NewList.RemoveAt ( index );
NewList.InsertRange ( index, Temp );
}
return NewList;
}
}
}
|
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 index = NewList.IndexOf ( NewList.OfType<ArrayList> ( ).ElementAt ( 0 ) );
ArrayList Temp = (ArrayList)NewList[index];
NewList.RemoveAt ( index );
NewList.InsertRange ( index, Temp );
}
return NewList;
}
}
}
|
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;
++next;
list.splice(next, boost::any_cast<anylist&>(*current));
current = list.erase(current);
}
else
++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;
++next;
list.splice(next, boost::any_cast<anylist&>(*current));
current = list.erase(current);
}
else
++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 (Object item : fromTreeList) {
if (item instanceof List<?>) {
flatten((List<?>) item, toFlatList);
} else {
toFlatList.add(item);
}
}
}
}
|
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 (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 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 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 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 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
|
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 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 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 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 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
if (associated(this%p)) then
do i = 1, size(this%p)
call del(this%p(i))
end do
end if
end subroutine
function join(xs) result (r)
type(n), dimension(:), target :: xs
type(n) :: r
integer :: i
if (size(xs)>0) then
allocate(r%p(size(xs)), source=xs)
do i = 1, size(xs)
r%p(i) = xs(i)
end do
else
r%empty = .true.
end if
end function
recursive subroutine flatten1(x,r)
integer, dimension (:), allocatable, intent(inout) :: r
type(n), intent(in) :: x
integer, dimension (:), allocatable :: tmp
integer :: i
if (associated(x%p)) then
do i = 1, size(x%p)
call flatten1(x%p(i), r)
end do
elseif (.not. x%empty) then
allocate(tmp(size(r)+1))
tmp(1:size(r)) = r
tmp(size(r)+1) = x%a
call move_alloc(tmp, r)
end if
end subroutine
function flatten(x) result (r)
type(n), intent(in) :: x
integer, dimension(:), allocatable :: r
allocate(r(0))
call flatten1(x,r)
end function
recursive subroutine show(x)
type(n) :: x
integer :: i
if (x%empty) then
write (*, "(a)", advance="no") "[]"
elseif (associated(x%p)) then
write (*, "(a)", advance="no") "["
do i = 1, size(x%p)
call show(x%p(i))
if (i<size(x%p)) then
write (*, "(a)", advance="no") ", "
end if
end do
write (*, "(a)", advance="no") "]"
else
write (*, "(g0)", advance="no") x%a
end if
end subroutine
function fromString(line) result (r)
character(len=*) :: line
type (n) :: r
type (n), dimension(:), allocatable :: buffer, buffer1
integer, dimension(:), allocatable :: stack, stack1
integer :: sp,i0,i,j, a, cur, start
character :: c
if (.not. allocated(buffer)) then
allocate (buffer(5))
end if
if (.not. allocated(stack)) then
allocate (stack(5))
end if
sp = 1; cur = 1; i = 1
do
if ( i > len_trim(line) ) exit
c = line(i:i)
if (c=="[") then
if (sp>size(stack)) then
allocate(stack1(2*size(stack)))
stack1(1:size(stack)) = stack
call move_alloc(stack1, stack)
end if
stack(sp) = cur; sp = sp + 1; i = i+1
elseif (c=="]") then
sp = sp - 1; start = stack(sp)
r = join(buffer(start:cur-1))
do j = start, cur-1
call del(buffer(j))
end do
buffer(start) = r; cur = start+1; i = i+1
elseif (index(" ,",c)>0) then
i = i + 1; continue
elseif (index("-123456789",c)>0) then
i0 = i
do
if ((i>len_trim(line)).or. &
index("1234567890",line(i:i))==0) then
read(line(i0:i-1),*) a
if (cur>size(buffer)) then
allocate(buffer1(2*size(buffer)))
buffer1(1:size(buffer)) = buffer
call move_alloc(buffer1, buffer)
end if
buffer(cur) = n(a); cur = cur + 1; exit
else
i = i+1
end if
end do
else
stop "input corrupted"
end if
end do
end function
end module
program main
use flat
type (n) :: x
x = fromString("[[1], 2, [[3,4], 5], [[[]]], [[[6]]], 7, 8, []]")
write(*, "(a)", advance="no") "input : "
call show(x)
print *
write (*,"(a)", advance="no") "flatten : ["
write (*, "(*(i0,:,:', '))", advance="no") flatten(x)
print *, "]"
end program
| 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#. |
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
if (associated(this%p)) then
do i = 1, size(this%p)
call del(this%p(i))
end do
end if
end subroutine
function join(xs) result (r)
type(n), dimension(:), target :: xs
type(n) :: r
integer :: i
if (size(xs)>0) then
allocate(r%p(size(xs)), source=xs)
do i = 1, size(xs)
r%p(i) = xs(i)
end do
else
r%empty = .true.
end if
end function
recursive subroutine flatten1(x,r)
integer, dimension (:), allocatable, intent(inout) :: r
type(n), intent(in) :: x
integer, dimension (:), allocatable :: tmp
integer :: i
if (associated(x%p)) then
do i = 1, size(x%p)
call flatten1(x%p(i), r)
end do
elseif (.not. x%empty) then
allocate(tmp(size(r)+1))
tmp(1:size(r)) = r
tmp(size(r)+1) = x%a
call move_alloc(tmp, r)
end if
end subroutine
function flatten(x) result (r)
type(n), intent(in) :: x
integer, dimension(:), allocatable :: r
allocate(r(0))
call flatten1(x,r)
end function
recursive subroutine show(x)
type(n) :: x
integer :: i
if (x%empty) then
write (*, "(a)", advance="no") "[]"
elseif (associated(x%p)) then
write (*, "(a)", advance="no") "["
do i = 1, size(x%p)
call show(x%p(i))
if (i<size(x%p)) then
write (*, "(a)", advance="no") ", "
end if
end do
write (*, "(a)", advance="no") "]"
else
write (*, "(g0)", advance="no") x%a
end if
end subroutine
function fromString(line) result (r)
character(len=*) :: line
type (n) :: r
type (n), dimension(:), allocatable :: buffer, buffer1
integer, dimension(:), allocatable :: stack, stack1
integer :: sp,i0,i,j, a, cur, start
character :: c
if (.not. allocated(buffer)) then
allocate (buffer(5))
end if
if (.not. allocated(stack)) then
allocate (stack(5))
end if
sp = 1; cur = 1; i = 1
do
if ( i > len_trim(line) ) exit
c = line(i:i)
if (c=="[") then
if (sp>size(stack)) then
allocate(stack1(2*size(stack)))
stack1(1:size(stack)) = stack
call move_alloc(stack1, stack)
end if
stack(sp) = cur; sp = sp + 1; i = i+1
elseif (c=="]") then
sp = sp - 1; start = stack(sp)
r = join(buffer(start:cur-1))
do j = start, cur-1
call del(buffer(j))
end do
buffer(start) = r; cur = start+1; i = i+1
elseif (index(" ,",c)>0) then
i = i + 1; continue
elseif (index("-123456789",c)>0) then
i0 = i
do
if ((i>len_trim(line)).or. &
index("1234567890",line(i:i))==0) then
read(line(i0:i-1),*) a
if (cur>size(buffer)) then
allocate(buffer1(2*size(buffer)))
buffer1(1:size(buffer)) = buffer
call move_alloc(buffer1, buffer)
end if
buffer(cur) = n(a); cur = cur + 1; exit
else
i = i+1
end if
end do
else
stop "input corrupted"
end if
end do
end function
end module
program main
use flat
type (n) :: x
x = fromString("[[1], 2, [[3,4], 5], [[[]]], [[[6]]], 7, 8, []]")
write(*, "(a)", advance="no") "input : "
call show(x)
print *
write (*,"(a)", advance="no") "flatten : ["
write (*, "(*(i0,:,:', '))", advance="no") flatten(x)
print *, "]"
end program
| 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;
}
}
}
|
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
if (associated(this%p)) then
do i = 1, size(this%p)
call del(this%p(i))
end do
end if
end subroutine
function join(xs) result (r)
type(n), dimension(:), target :: xs
type(n) :: r
integer :: i
if (size(xs)>0) then
allocate(r%p(size(xs)), source=xs)
do i = 1, size(xs)
r%p(i) = xs(i)
end do
else
r%empty = .true.
end if
end function
recursive subroutine flatten1(x,r)
integer, dimension (:), allocatable, intent(inout) :: r
type(n), intent(in) :: x
integer, dimension (:), allocatable :: tmp
integer :: i
if (associated(x%p)) then
do i = 1, size(x%p)
call flatten1(x%p(i), r)
end do
elseif (.not. x%empty) then
allocate(tmp(size(r)+1))
tmp(1:size(r)) = r
tmp(size(r)+1) = x%a
call move_alloc(tmp, r)
end if
end subroutine
function flatten(x) result (r)
type(n), intent(in) :: x
integer, dimension(:), allocatable :: r
allocate(r(0))
call flatten1(x,r)
end function
recursive subroutine show(x)
type(n) :: x
integer :: i
if (x%empty) then
write (*, "(a)", advance="no") "[]"
elseif (associated(x%p)) then
write (*, "(a)", advance="no") "["
do i = 1, size(x%p)
call show(x%p(i))
if (i<size(x%p)) then
write (*, "(a)", advance="no") ", "
end if
end do
write (*, "(a)", advance="no") "]"
else
write (*, "(g0)", advance="no") x%a
end if
end subroutine
function fromString(line) result (r)
character(len=*) :: line
type (n) :: r
type (n), dimension(:), allocatable :: buffer, buffer1
integer, dimension(:), allocatable :: stack, stack1
integer :: sp,i0,i,j, a, cur, start
character :: c
if (.not. allocated(buffer)) then
allocate (buffer(5))
end if
if (.not. allocated(stack)) then
allocate (stack(5))
end if
sp = 1; cur = 1; i = 1
do
if ( i > len_trim(line) ) exit
c = line(i:i)
if (c=="[") then
if (sp>size(stack)) then
allocate(stack1(2*size(stack)))
stack1(1:size(stack)) = stack
call move_alloc(stack1, stack)
end if
stack(sp) = cur; sp = sp + 1; i = i+1
elseif (c=="]") then
sp = sp - 1; start = stack(sp)
r = join(buffer(start:cur-1))
do j = start, cur-1
call del(buffer(j))
end do
buffer(start) = r; cur = start+1; i = i+1
elseif (index(" ,",c)>0) then
i = i + 1; continue
elseif (index("-123456789",c)>0) then
i0 = i
do
if ((i>len_trim(line)).or. &
index("1234567890",line(i:i))==0) then
read(line(i0:i-1),*) a
if (cur>size(buffer)) then
allocate(buffer1(2*size(buffer)))
buffer1(1:size(buffer)) = buffer
call move_alloc(buffer1, buffer)
end if
buffer(cur) = n(a); cur = cur + 1; exit
else
i = i+1
end if
end do
else
stop "input corrupted"
end if
end do
end function
end module
program main
use flat
type (n) :: x
x = fromString("[[1], 2, [[3,4], 5], [[[]]], [[[6]]], 7, 8, []]")
write(*, "(a)", advance="no") "input : "
call show(x)
print *
write (*,"(a)", advance="no") "flatten : ["
write (*, "(*(i0,:,:', '))", advance="no") flatten(x)
print *, "]"
end program
| #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 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
if (associated(this%p)) then
do i = 1, size(this%p)
call del(this%p(i))
end do
end if
end subroutine
function join(xs) result (r)
type(n), dimension(:), target :: xs
type(n) :: r
integer :: i
if (size(xs)>0) then
allocate(r%p(size(xs)), source=xs)
do i = 1, size(xs)
r%p(i) = xs(i)
end do
else
r%empty = .true.
end if
end function
recursive subroutine flatten1(x,r)
integer, dimension (:), allocatable, intent(inout) :: r
type(n), intent(in) :: x
integer, dimension (:), allocatable :: tmp
integer :: i
if (associated(x%p)) then
do i = 1, size(x%p)
call flatten1(x%p(i), r)
end do
elseif (.not. x%empty) then
allocate(tmp(size(r)+1))
tmp(1:size(r)) = r
tmp(size(r)+1) = x%a
call move_alloc(tmp, r)
end if
end subroutine
function flatten(x) result (r)
type(n), intent(in) :: x
integer, dimension(:), allocatable :: r
allocate(r(0))
call flatten1(x,r)
end function
recursive subroutine show(x)
type(n) :: x
integer :: i
if (x%empty) then
write (*, "(a)", advance="no") "[]"
elseif (associated(x%p)) then
write (*, "(a)", advance="no") "["
do i = 1, size(x%p)
call show(x%p(i))
if (i<size(x%p)) then
write (*, "(a)", advance="no") ", "
end if
end do
write (*, "(a)", advance="no") "]"
else
write (*, "(g0)", advance="no") x%a
end if
end subroutine
function fromString(line) result (r)
character(len=*) :: line
type (n) :: r
type (n), dimension(:), allocatable :: buffer, buffer1
integer, dimension(:), allocatable :: stack, stack1
integer :: sp,i0,i,j, a, cur, start
character :: c
if (.not. allocated(buffer)) then
allocate (buffer(5))
end if
if (.not. allocated(stack)) then
allocate (stack(5))
end if
sp = 1; cur = 1; i = 1
do
if ( i > len_trim(line) ) exit
c = line(i:i)
if (c=="[") then
if (sp>size(stack)) then
allocate(stack1(2*size(stack)))
stack1(1:size(stack)) = stack
call move_alloc(stack1, stack)
end if
stack(sp) = cur; sp = sp + 1; i = i+1
elseif (c=="]") then
sp = sp - 1; start = stack(sp)
r = join(buffer(start:cur-1))
do j = start, cur-1
call del(buffer(j))
end do
buffer(start) = r; cur = start+1; i = i+1
elseif (index(" ,",c)>0) then
i = i + 1; continue
elseif (index("-123456789",c)>0) then
i0 = i
do
if ((i>len_trim(line)).or. &
index("1234567890",line(i:i))==0) then
read(line(i0:i-1),*) a
if (cur>size(buffer)) then
allocate(buffer1(2*size(buffer)))
buffer1(1:size(buffer)) = buffer
call move_alloc(buffer1, buffer)
end if
buffer(cur) = n(a); cur = cur + 1; exit
else
i = i+1
end if
end do
else
stop "input corrupted"
end if
end do
end function
end module
program main
use flat
type (n) :: x
x = fromString("[[1], 2, [[3,4], 5], [[[]]], [[[6]]], 7, 8, []]")
write(*, "(a)", advance="no") "input : "
call show(x)
print *
write (*,"(a)", advance="no") "flatten : ["
write (*, "(*(i0,:,:', '))", advance="no") flatten(x)
print *, "]"
end program
| #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;
}
}
|
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
if (associated(this%p)) then
do i = 1, size(this%p)
call del(this%p(i))
end do
end if
end subroutine
function join(xs) result (r)
type(n), dimension(:), target :: xs
type(n) :: r
integer :: i
if (size(xs)>0) then
allocate(r%p(size(xs)), source=xs)
do i = 1, size(xs)
r%p(i) = xs(i)
end do
else
r%empty = .true.
end if
end function
recursive subroutine flatten1(x,r)
integer, dimension (:), allocatable, intent(inout) :: r
type(n), intent(in) :: x
integer, dimension (:), allocatable :: tmp
integer :: i
if (associated(x%p)) then
do i = 1, size(x%p)
call flatten1(x%p(i), r)
end do
elseif (.not. x%empty) then
allocate(tmp(size(r)+1))
tmp(1:size(r)) = r
tmp(size(r)+1) = x%a
call move_alloc(tmp, r)
end if
end subroutine
function flatten(x) result (r)
type(n), intent(in) :: x
integer, dimension(:), allocatable :: r
allocate(r(0))
call flatten1(x,r)
end function
recursive subroutine show(x)
type(n) :: x
integer :: i
if (x%empty) then
write (*, "(a)", advance="no") "[]"
elseif (associated(x%p)) then
write (*, "(a)", advance="no") "["
do i = 1, size(x%p)
call show(x%p(i))
if (i<size(x%p)) then
write (*, "(a)", advance="no") ", "
end if
end do
write (*, "(a)", advance="no") "]"
else
write (*, "(g0)", advance="no") x%a
end if
end subroutine
function fromString(line) result (r)
character(len=*) :: line
type (n) :: r
type (n), dimension(:), allocatable :: buffer, buffer1
integer, dimension(:), allocatable :: stack, stack1
integer :: sp,i0,i,j, a, cur, start
character :: c
if (.not. allocated(buffer)) then
allocate (buffer(5))
end if
if (.not. allocated(stack)) then
allocate (stack(5))
end if
sp = 1; cur = 1; i = 1
do
if ( i > len_trim(line) ) exit
c = line(i:i)
if (c=="[") then
if (sp>size(stack)) then
allocate(stack1(2*size(stack)))
stack1(1:size(stack)) = stack
call move_alloc(stack1, stack)
end if
stack(sp) = cur; sp = sp + 1; i = i+1
elseif (c=="]") then
sp = sp - 1; start = stack(sp)
r = join(buffer(start:cur-1))
do j = start, cur-1
call del(buffer(j))
end do
buffer(start) = r; cur = start+1; i = i+1
elseif (index(" ,",c)>0) then
i = i + 1; continue
elseif (index("-123456789",c)>0) then
i0 = i
do
if ((i>len_trim(line)).or. &
index("1234567890",line(i:i))==0) then
read(line(i0:i-1),*) a
if (cur>size(buffer)) then
allocate(buffer1(2*size(buffer)))
buffer1(1:size(buffer)) = buffer
call move_alloc(buffer1, buffer)
end if
buffer(cur) = n(a); cur = cur + 1; exit
else
i = i+1
end if
end do
else
stop "input corrupted"
end if
end do
end function
end module
program main
use flat
type (n) :: x
x = fromString("[[1], 2, [[3,4], 5], [[[]]], [[[6]]], 7, 8, []]")
write(*, "(a)", advance="no") "input : "
call show(x)
print *
write (*,"(a)", advance="no") "flatten : ["
write (*, "(*(i0,:,:', '))", advance="no") flatten(x)
print *, "]"
end program
| #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;
}
|
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
if (associated(this%p)) then
do i = 1, size(this%p)
call del(this%p(i))
end do
end if
end subroutine
function join(xs) result (r)
type(n), dimension(:), target :: xs
type(n) :: r
integer :: i
if (size(xs)>0) then
allocate(r%p(size(xs)), source=xs)
do i = 1, size(xs)
r%p(i) = xs(i)
end do
else
r%empty = .true.
end if
end function
recursive subroutine flatten1(x,r)
integer, dimension (:), allocatable, intent(inout) :: r
type(n), intent(in) :: x
integer, dimension (:), allocatable :: tmp
integer :: i
if (associated(x%p)) then
do i = 1, size(x%p)
call flatten1(x%p(i), r)
end do
elseif (.not. x%empty) then
allocate(tmp(size(r)+1))
tmp(1:size(r)) = r
tmp(size(r)+1) = x%a
call move_alloc(tmp, r)
end if
end subroutine
function flatten(x) result (r)
type(n), intent(in) :: x
integer, dimension(:), allocatable :: r
allocate(r(0))
call flatten1(x,r)
end function
recursive subroutine show(x)
type(n) :: x
integer :: i
if (x%empty) then
write (*, "(a)", advance="no") "[]"
elseif (associated(x%p)) then
write (*, "(a)", advance="no") "["
do i = 1, size(x%p)
call show(x%p(i))
if (i<size(x%p)) then
write (*, "(a)", advance="no") ", "
end if
end do
write (*, "(a)", advance="no") "]"
else
write (*, "(g0)", advance="no") x%a
end if
end subroutine
function fromString(line) result (r)
character(len=*) :: line
type (n) :: r
type (n), dimension(:), allocatable :: buffer, buffer1
integer, dimension(:), allocatable :: stack, stack1
integer :: sp,i0,i,j, a, cur, start
character :: c
if (.not. allocated(buffer)) then
allocate (buffer(5))
end if
if (.not. allocated(stack)) then
allocate (stack(5))
end if
sp = 1; cur = 1; i = 1
do
if ( i > len_trim(line) ) exit
c = line(i:i)
if (c=="[") then
if (sp>size(stack)) then
allocate(stack1(2*size(stack)))
stack1(1:size(stack)) = stack
call move_alloc(stack1, stack)
end if
stack(sp) = cur; sp = sp + 1; i = i+1
elseif (c=="]") then
sp = sp - 1; start = stack(sp)
r = join(buffer(start:cur-1))
do j = start, cur-1
call del(buffer(j))
end do
buffer(start) = r; cur = start+1; i = i+1
elseif (index(" ,",c)>0) then
i = i + 1; continue
elseif (index("-123456789",c)>0) then
i0 = i
do
if ((i>len_trim(line)).or. &
index("1234567890",line(i:i))==0) then
read(line(i0:i-1),*) a
if (cur>size(buffer)) then
allocate(buffer1(2*size(buffer)))
buffer1(1:size(buffer)) = buffer
call move_alloc(buffer1, buffer)
end if
buffer(cur) = n(a); cur = cur + 1; exit
else
i = i+1
end if
end do
else
stop "input corrupted"
end if
end do
end function
end module
program main
use flat
type (n) :: x
x = fromString("[[1], 2, [[3,4], 5], [[[]]], [[[6]]], 7, 8, []]")
write(*, "(a)", advance="no") "input : "
call show(x)
print *
write (*,"(a)", advance="no") "flatten : ["
write (*, "(*(i0,:,:', '))", advance="no") flatten(x)
print *, "]"
end program
| #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 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
if (associated(this%p)) then
do i = 1, size(this%p)
call del(this%p(i))
end do
end if
end subroutine
function join(xs) result (r)
type(n), dimension(:), target :: xs
type(n) :: r
integer :: i
if (size(xs)>0) then
allocate(r%p(size(xs)), source=xs)
do i = 1, size(xs)
r%p(i) = xs(i)
end do
else
r%empty = .true.
end if
end function
recursive subroutine flatten1(x,r)
integer, dimension (:), allocatable, intent(inout) :: r
type(n), intent(in) :: x
integer, dimension (:), allocatable :: tmp
integer :: i
if (associated(x%p)) then
do i = 1, size(x%p)
call flatten1(x%p(i), r)
end do
elseif (.not. x%empty) then
allocate(tmp(size(r)+1))
tmp(1:size(r)) = r
tmp(size(r)+1) = x%a
call move_alloc(tmp, r)
end if
end subroutine
function flatten(x) result (r)
type(n), intent(in) :: x
integer, dimension(:), allocatable :: r
allocate(r(0))
call flatten1(x,r)
end function
recursive subroutine show(x)
type(n) :: x
integer :: i
if (x%empty) then
write (*, "(a)", advance="no") "[]"
elseif (associated(x%p)) then
write (*, "(a)", advance="no") "["
do i = 1, size(x%p)
call show(x%p(i))
if (i<size(x%p)) then
write (*, "(a)", advance="no") ", "
end if
end do
write (*, "(a)", advance="no") "]"
else
write (*, "(g0)", advance="no") x%a
end if
end subroutine
function fromString(line) result (r)
character(len=*) :: line
type (n) :: r
type (n), dimension(:), allocatable :: buffer, buffer1
integer, dimension(:), allocatable :: stack, stack1
integer :: sp,i0,i,j, a, cur, start
character :: c
if (.not. allocated(buffer)) then
allocate (buffer(5))
end if
if (.not. allocated(stack)) then
allocate (stack(5))
end if
sp = 1; cur = 1; i = 1
do
if ( i > len_trim(line) ) exit
c = line(i:i)
if (c=="[") then
if (sp>size(stack)) then
allocate(stack1(2*size(stack)))
stack1(1:size(stack)) = stack
call move_alloc(stack1, stack)
end if
stack(sp) = cur; sp = sp + 1; i = i+1
elseif (c=="]") then
sp = sp - 1; start = stack(sp)
r = join(buffer(start:cur-1))
do j = start, cur-1
call del(buffer(j))
end do
buffer(start) = r; cur = start+1; i = i+1
elseif (index(" ,",c)>0) then
i = i + 1; continue
elseif (index("-123456789",c)>0) then
i0 = i
do
if ((i>len_trim(line)).or. &
index("1234567890",line(i:i))==0) then
read(line(i0:i-1),*) a
if (cur>size(buffer)) then
allocate(buffer1(2*size(buffer)))
buffer1(1:size(buffer)) = buffer
call move_alloc(buffer1, buffer)
end if
buffer(cur) = n(a); cur = cur + 1; exit
else
i = i+1
end if
end do
else
stop "input corrupted"
end if
end do
end function
end module
program main
use flat
type (n) :: x
x = fromString("[[1], 2, [[3,4], 5], [[[]]], [[[6]]], 7, 8, []]")
write(*, "(a)", advance="no") "input : "
call show(x)
print *
write (*,"(a)", advance="no") "flatten : ["
write (*, "(*(i0,:,:', '))", advance="no") flatten(x)
print *, "]"
end program
| 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
}
|
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
if (associated(this%p)) then
do i = 1, size(this%p)
call del(this%p(i))
end do
end if
end subroutine
function join(xs) result (r)
type(n), dimension(:), target :: xs
type(n) :: r
integer :: i
if (size(xs)>0) then
allocate(r%p(size(xs)), source=xs)
do i = 1, size(xs)
r%p(i) = xs(i)
end do
else
r%empty = .true.
end if
end function
recursive subroutine flatten1(x,r)
integer, dimension (:), allocatable, intent(inout) :: r
type(n), intent(in) :: x
integer, dimension (:), allocatable :: tmp
integer :: i
if (associated(x%p)) then
do i = 1, size(x%p)
call flatten1(x%p(i), r)
end do
elseif (.not. x%empty) then
allocate(tmp(size(r)+1))
tmp(1:size(r)) = r
tmp(size(r)+1) = x%a
call move_alloc(tmp, r)
end if
end subroutine
function flatten(x) result (r)
type(n), intent(in) :: x
integer, dimension(:), allocatable :: r
allocate(r(0))
call flatten1(x,r)
end function
recursive subroutine show(x)
type(n) :: x
integer :: i
if (x%empty) then
write (*, "(a)", advance="no") "[]"
elseif (associated(x%p)) then
write (*, "(a)", advance="no") "["
do i = 1, size(x%p)
call show(x%p(i))
if (i<size(x%p)) then
write (*, "(a)", advance="no") ", "
end if
end do
write (*, "(a)", advance="no") "]"
else
write (*, "(g0)", advance="no") x%a
end if
end subroutine
function fromString(line) result (r)
character(len=*) :: line
type (n) :: r
type (n), dimension(:), allocatable :: buffer, buffer1
integer, dimension(:), allocatable :: stack, stack1
integer :: sp,i0,i,j, a, cur, start
character :: c
if (.not. allocated(buffer)) then
allocate (buffer(5))
end if
if (.not. allocated(stack)) then
allocate (stack(5))
end if
sp = 1; cur = 1; i = 1
do
if ( i > len_trim(line) ) exit
c = line(i:i)
if (c=="[") then
if (sp>size(stack)) then
allocate(stack1(2*size(stack)))
stack1(1:size(stack)) = stack
call move_alloc(stack1, stack)
end if
stack(sp) = cur; sp = sp + 1; i = i+1
elseif (c=="]") then
sp = sp - 1; start = stack(sp)
r = join(buffer(start:cur-1))
do j = start, cur-1
call del(buffer(j))
end do
buffer(start) = r; cur = start+1; i = i+1
elseif (index(" ,",c)>0) then
i = i + 1; continue
elseif (index("-123456789",c)>0) then
i0 = i
do
if ((i>len_trim(line)).or. &
index("1234567890",line(i:i))==0) then
read(line(i0:i-1),*) a
if (cur>size(buffer)) then
allocate(buffer1(2*size(buffer)))
buffer1(1:size(buffer)) = buffer
call move_alloc(buffer1, buffer)
end if
buffer(cur) = n(a); cur = cur + 1; exit
else
i = i+1
end if
end do
else
stop "input corrupted"
end if
end do
end function
end module
program main
use flat
type (n) :: x
x = fromString("[[1], 2, [[3,4], 5], [[[]]], [[[6]]], 7, 8, []]")
write(*, "(a)", advance="no") "input : "
call show(x)
print *
write (*,"(a)", advance="no") "flatten : ["
write (*, "(*(i0,:,:', '))", advance="no") flatten(x)
print *, "]"
end program
| 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 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
if (associated(this%p)) then
do i = 1, size(this%p)
call del(this%p(i))
end do
end if
end subroutine
function join(xs) result (r)
type(n), dimension(:), target :: xs
type(n) :: r
integer :: i
if (size(xs)>0) then
allocate(r%p(size(xs)), source=xs)
do i = 1, size(xs)
r%p(i) = xs(i)
end do
else
r%empty = .true.
end if
end function
recursive subroutine flatten1(x,r)
integer, dimension (:), allocatable, intent(inout) :: r
type(n), intent(in) :: x
integer, dimension (:), allocatable :: tmp
integer :: i
if (associated(x%p)) then
do i = 1, size(x%p)
call flatten1(x%p(i), r)
end do
elseif (.not. x%empty) then
allocate(tmp(size(r)+1))
tmp(1:size(r)) = r
tmp(size(r)+1) = x%a
call move_alloc(tmp, r)
end if
end subroutine
function flatten(x) result (r)
type(n), intent(in) :: x
integer, dimension(:), allocatable :: r
allocate(r(0))
call flatten1(x,r)
end function
recursive subroutine show(x)
type(n) :: x
integer :: i
if (x%empty) then
write (*, "(a)", advance="no") "[]"
elseif (associated(x%p)) then
write (*, "(a)", advance="no") "["
do i = 1, size(x%p)
call show(x%p(i))
if (i<size(x%p)) then
write (*, "(a)", advance="no") ", "
end if
end do
write (*, "(a)", advance="no") "]"
else
write (*, "(g0)", advance="no") x%a
end if
end subroutine
function fromString(line) result (r)
character(len=*) :: line
type (n) :: r
type (n), dimension(:), allocatable :: buffer, buffer1
integer, dimension(:), allocatable :: stack, stack1
integer :: sp,i0,i,j, a, cur, start
character :: c
if (.not. allocated(buffer)) then
allocate (buffer(5))
end if
if (.not. allocated(stack)) then
allocate (stack(5))
end if
sp = 1; cur = 1; i = 1
do
if ( i > len_trim(line) ) exit
c = line(i:i)
if (c=="[") then
if (sp>size(stack)) then
allocate(stack1(2*size(stack)))
stack1(1:size(stack)) = stack
call move_alloc(stack1, stack)
end if
stack(sp) = cur; sp = sp + 1; i = i+1
elseif (c=="]") then
sp = sp - 1; start = stack(sp)
r = join(buffer(start:cur-1))
do j = start, cur-1
call del(buffer(j))
end do
buffer(start) = r; cur = start+1; i = i+1
elseif (index(" ,",c)>0) then
i = i + 1; continue
elseif (index("-123456789",c)>0) then
i0 = i
do
if ((i>len_trim(line)).or. &
index("1234567890",line(i:i))==0) then
read(line(i0:i-1),*) a
if (cur>size(buffer)) then
allocate(buffer1(2*size(buffer)))
buffer1(1:size(buffer)) = buffer
call move_alloc(buffer1, buffer)
end if
buffer(cur) = n(a); cur = cur + 1; exit
else
i = i+1
end if
end do
else
stop "input corrupted"
end if
end do
end function
end module
program main
use flat
type (n) :: x
x = fromString("[[1], 2, [[3,4], 5], [[[]]], [[[6]]], 7, 8, []]")
write(*, "(a)", advance="no") "input : "
call show(x)
print *
write (*,"(a)", advance="no") "flatten : ["
write (*, "(*(i0,:,:', '))", advance="no") flatten(x)
print *, "]"
end program
| 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 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
if (associated(this%p)) then
do i = 1, size(this%p)
call del(this%p(i))
end do
end if
end subroutine
function join(xs) result (r)
type(n), dimension(:), target :: xs
type(n) :: r
integer :: i
if (size(xs)>0) then
allocate(r%p(size(xs)), source=xs)
do i = 1, size(xs)
r%p(i) = xs(i)
end do
else
r%empty = .true.
end if
end function
recursive subroutine flatten1(x,r)
integer, dimension (:), allocatable, intent(inout) :: r
type(n), intent(in) :: x
integer, dimension (:), allocatable :: tmp
integer :: i
if (associated(x%p)) then
do i = 1, size(x%p)
call flatten1(x%p(i), r)
end do
elseif (.not. x%empty) then
allocate(tmp(size(r)+1))
tmp(1:size(r)) = r
tmp(size(r)+1) = x%a
call move_alloc(tmp, r)
end if
end subroutine
function flatten(x) result (r)
type(n), intent(in) :: x
integer, dimension(:), allocatable :: r
allocate(r(0))
call flatten1(x,r)
end function
recursive subroutine show(x)
type(n) :: x
integer :: i
if (x%empty) then
write (*, "(a)", advance="no") "[]"
elseif (associated(x%p)) then
write (*, "(a)", advance="no") "["
do i = 1, size(x%p)
call show(x%p(i))
if (i<size(x%p)) then
write (*, "(a)", advance="no") ", "
end if
end do
write (*, "(a)", advance="no") "]"
else
write (*, "(g0)", advance="no") x%a
end if
end subroutine
function fromString(line) result (r)
character(len=*) :: line
type (n) :: r
type (n), dimension(:), allocatable :: buffer, buffer1
integer, dimension(:), allocatable :: stack, stack1
integer :: sp,i0,i,j, a, cur, start
character :: c
if (.not. allocated(buffer)) then
allocate (buffer(5))
end if
if (.not. allocated(stack)) then
allocate (stack(5))
end if
sp = 1; cur = 1; i = 1
do
if ( i > len_trim(line) ) exit
c = line(i:i)
if (c=="[") then
if (sp>size(stack)) then
allocate(stack1(2*size(stack)))
stack1(1:size(stack)) = stack
call move_alloc(stack1, stack)
end if
stack(sp) = cur; sp = sp + 1; i = i+1
elseif (c=="]") then
sp = sp - 1; start = stack(sp)
r = join(buffer(start:cur-1))
do j = start, cur-1
call del(buffer(j))
end do
buffer(start) = r; cur = start+1; i = i+1
elseif (index(" ,",c)>0) then
i = i + 1; continue
elseif (index("-123456789",c)>0) then
i0 = i
do
if ((i>len_trim(line)).or. &
index("1234567890",line(i:i))==0) then
read(line(i0:i-1),*) a
if (cur>size(buffer)) then
allocate(buffer1(2*size(buffer)))
buffer1(1:size(buffer)) = buffer
call move_alloc(buffer1, buffer)
end if
buffer(cur) = n(a); cur = cur + 1; exit
else
i = i+1
end if
end do
else
stop "input corrupted"
end if
end do
end function
end module
program main
use flat
type (n) :: x
x = fromString("[[1], 2, [[3,4], 5], [[[]]], [[[6]]], 7, 8, []]")
write(*, "(a)", advance="no") "input : "
call show(x)
print *
write (*,"(a)", advance="no") "flatten : ["
write (*, "(*(i0,:,:', '))", advance="no") flatten(x)
print *, "]"
end program
| >>> 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
if (associated(this%p)) then
do i = 1, size(this%p)
call del(this%p(i))
end do
end if
end subroutine
function join(xs) result (r)
type(n), dimension(:), target :: xs
type(n) :: r
integer :: i
if (size(xs)>0) then
allocate(r%p(size(xs)), source=xs)
do i = 1, size(xs)
r%p(i) = xs(i)
end do
else
r%empty = .true.
end if
end function
recursive subroutine flatten1(x,r)
integer, dimension (:), allocatable, intent(inout) :: r
type(n), intent(in) :: x
integer, dimension (:), allocatable :: tmp
integer :: i
if (associated(x%p)) then
do i = 1, size(x%p)
call flatten1(x%p(i), r)
end do
elseif (.not. x%empty) then
allocate(tmp(size(r)+1))
tmp(1:size(r)) = r
tmp(size(r)+1) = x%a
call move_alloc(tmp, r)
end if
end subroutine
function flatten(x) result (r)
type(n), intent(in) :: x
integer, dimension(:), allocatable :: r
allocate(r(0))
call flatten1(x,r)
end function
recursive subroutine show(x)
type(n) :: x
integer :: i
if (x%empty) then
write (*, "(a)", advance="no") "[]"
elseif (associated(x%p)) then
write (*, "(a)", advance="no") "["
do i = 1, size(x%p)
call show(x%p(i))
if (i<size(x%p)) then
write (*, "(a)", advance="no") ", "
end if
end do
write (*, "(a)", advance="no") "]"
else
write (*, "(g0)", advance="no") x%a
end if
end subroutine
function fromString(line) result (r)
character(len=*) :: line
type (n) :: r
type (n), dimension(:), allocatable :: buffer, buffer1
integer, dimension(:), allocatable :: stack, stack1
integer :: sp,i0,i,j, a, cur, start
character :: c
if (.not. allocated(buffer)) then
allocate (buffer(5))
end if
if (.not. allocated(stack)) then
allocate (stack(5))
end if
sp = 1; cur = 1; i = 1
do
if ( i > len_trim(line) ) exit
c = line(i:i)
if (c=="[") then
if (sp>size(stack)) then
allocate(stack1(2*size(stack)))
stack1(1:size(stack)) = stack
call move_alloc(stack1, stack)
end if
stack(sp) = cur; sp = sp + 1; i = i+1
elseif (c=="]") then
sp = sp - 1; start = stack(sp)
r = join(buffer(start:cur-1))
do j = start, cur-1
call del(buffer(j))
end do
buffer(start) = r; cur = start+1; i = i+1
elseif (index(" ,",c)>0) then
i = i + 1; continue
elseif (index("-123456789",c)>0) then
i0 = i
do
if ((i>len_trim(line)).or. &
index("1234567890",line(i:i))==0) then
read(line(i0:i-1),*) a
if (cur>size(buffer)) then
allocate(buffer1(2*size(buffer)))
buffer1(1:size(buffer)) = buffer
call move_alloc(buffer1, buffer)
end if
buffer(cur) = n(a); cur = cur + 1; exit
else
i = i+1
end if
end do
else
stop "input corrupted"
end if
end do
end function
end module
program main
use flat
type (n) :: x
x = fromString("[[1], 2, [[3,4], 5], [[[]]], [[[6]]], 7, 8, []]")
write(*, "(a)", advance="no") "input : "
call show(x)
print *
write (*,"(a)", advance="no") "flatten : ["
write (*, "(*(i0,:,:', '))", advance="no") flatten(x)
print *, "]"
end program
| >>> 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
if (associated(this%p)) then
do i = 1, size(this%p)
call del(this%p(i))
end do
end if
end subroutine
function join(xs) result (r)
type(n), dimension(:), target :: xs
type(n) :: r
integer :: i
if (size(xs)>0) then
allocate(r%p(size(xs)), source=xs)
do i = 1, size(xs)
r%p(i) = xs(i)
end do
else
r%empty = .true.
end if
end function
recursive subroutine flatten1(x,r)
integer, dimension (:), allocatable, intent(inout) :: r
type(n), intent(in) :: x
integer, dimension (:), allocatable :: tmp
integer :: i
if (associated(x%p)) then
do i = 1, size(x%p)
call flatten1(x%p(i), r)
end do
elseif (.not. x%empty) then
allocate(tmp(size(r)+1))
tmp(1:size(r)) = r
tmp(size(r)+1) = x%a
call move_alloc(tmp, r)
end if
end subroutine
function flatten(x) result (r)
type(n), intent(in) :: x
integer, dimension(:), allocatable :: r
allocate(r(0))
call flatten1(x,r)
end function
recursive subroutine show(x)
type(n) :: x
integer :: i
if (x%empty) then
write (*, "(a)", advance="no") "[]"
elseif (associated(x%p)) then
write (*, "(a)", advance="no") "["
do i = 1, size(x%p)
call show(x%p(i))
if (i<size(x%p)) then
write (*, "(a)", advance="no") ", "
end if
end do
write (*, "(a)", advance="no") "]"
else
write (*, "(g0)", advance="no") x%a
end if
end subroutine
function fromString(line) result (r)
character(len=*) :: line
type (n) :: r
type (n), dimension(:), allocatable :: buffer, buffer1
integer, dimension(:), allocatable :: stack, stack1
integer :: sp,i0,i,j, a, cur, start
character :: c
if (.not. allocated(buffer)) then
allocate (buffer(5))
end if
if (.not. allocated(stack)) then
allocate (stack(5))
end if
sp = 1; cur = 1; i = 1
do
if ( i > len_trim(line) ) exit
c = line(i:i)
if (c=="[") then
if (sp>size(stack)) then
allocate(stack1(2*size(stack)))
stack1(1:size(stack)) = stack
call move_alloc(stack1, stack)
end if
stack(sp) = cur; sp = sp + 1; i = i+1
elseif (c=="]") then
sp = sp - 1; start = stack(sp)
r = join(buffer(start:cur-1))
do j = start, cur-1
call del(buffer(j))
end do
buffer(start) = r; cur = start+1; i = i+1
elseif (index(" ,",c)>0) then
i = i + 1; continue
elseif (index("-123456789",c)>0) then
i0 = i
do
if ((i>len_trim(line)).or. &
index("1234567890",line(i:i))==0) then
read(line(i0:i-1),*) a
if (cur>size(buffer)) then
allocate(buffer1(2*size(buffer)))
buffer1(1:size(buffer)) = buffer
call move_alloc(buffer1, buffer)
end if
buffer(cur) = n(a); cur = cur + 1; exit
else
i = i+1
end if
end do
else
stop "input corrupted"
end if
end do
end function
end module
program main
use flat
type (n) :: x
x = fromString("[[1], 2, [[3,4], 5], [[[]]], [[[6]]], 7, 8, []]")
write(*, "(a)", advance="no") "input : "
call show(x)
print *
write (*,"(a)", advance="no") "flatten : ["
write (*, "(*(i0,:,:', '))", advance="no") flatten(x)
print *, "]"
end program
| 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 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
if (associated(this%p)) then
do i = 1, size(this%p)
call del(this%p(i))
end do
end if
end subroutine
function join(xs) result (r)
type(n), dimension(:), target :: xs
type(n) :: r
integer :: i
if (size(xs)>0) then
allocate(r%p(size(xs)), source=xs)
do i = 1, size(xs)
r%p(i) = xs(i)
end do
else
r%empty = .true.
end if
end function
recursive subroutine flatten1(x,r)
integer, dimension (:), allocatable, intent(inout) :: r
type(n), intent(in) :: x
integer, dimension (:), allocatable :: tmp
integer :: i
if (associated(x%p)) then
do i = 1, size(x%p)
call flatten1(x%p(i), r)
end do
elseif (.not. x%empty) then
allocate(tmp(size(r)+1))
tmp(1:size(r)) = r
tmp(size(r)+1) = x%a
call move_alloc(tmp, r)
end if
end subroutine
function flatten(x) result (r)
type(n), intent(in) :: x
integer, dimension(:), allocatable :: r
allocate(r(0))
call flatten1(x,r)
end function
recursive subroutine show(x)
type(n) :: x
integer :: i
if (x%empty) then
write (*, "(a)", advance="no") "[]"
elseif (associated(x%p)) then
write (*, "(a)", advance="no") "["
do i = 1, size(x%p)
call show(x%p(i))
if (i<size(x%p)) then
write (*, "(a)", advance="no") ", "
end if
end do
write (*, "(a)", advance="no") "]"
else
write (*, "(g0)", advance="no") x%a
end if
end subroutine
function fromString(line) result (r)
character(len=*) :: line
type (n) :: r
type (n), dimension(:), allocatable :: buffer, buffer1
integer, dimension(:), allocatable :: stack, stack1
integer :: sp,i0,i,j, a, cur, start
character :: c
if (.not. allocated(buffer)) then
allocate (buffer(5))
end if
if (.not. allocated(stack)) then
allocate (stack(5))
end if
sp = 1; cur = 1; i = 1
do
if ( i > len_trim(line) ) exit
c = line(i:i)
if (c=="[") then
if (sp>size(stack)) then
allocate(stack1(2*size(stack)))
stack1(1:size(stack)) = stack
call move_alloc(stack1, stack)
end if
stack(sp) = cur; sp = sp + 1; i = i+1
elseif (c=="]") then
sp = sp - 1; start = stack(sp)
r = join(buffer(start:cur-1))
do j = start, cur-1
call del(buffer(j))
end do
buffer(start) = r; cur = start+1; i = i+1
elseif (index(" ,",c)>0) then
i = i + 1; continue
elseif (index("-123456789",c)>0) then
i0 = i
do
if ((i>len_trim(line)).or. &
index("1234567890",line(i:i))==0) then
read(line(i0:i-1),*) a
if (cur>size(buffer)) then
allocate(buffer1(2*size(buffer)))
buffer1(1:size(buffer)) = buffer
call move_alloc(buffer1, buffer)
end if
buffer(cur) = n(a); cur = cur + 1; exit
else
i = i+1
end if
end do
else
stop "input corrupted"
end if
end do
end function
end module
program main
use flat
type (n) :: x
x = fromString("[[1], 2, [[3,4], 5], [[[]]], [[[6]]], 7, 8, []]")
write(*, "(a)", advance="no") "input : "
call show(x)
print *
write (*,"(a)", advance="no") "flatten : ["
write (*, "(*(i0,:,:', '))", advance="no") flatten(x)
print *, "]"
end program
| 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 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
if (associated(this%p)) then
do i = 1, size(this%p)
call del(this%p(i))
end do
end if
end subroutine
function join(xs) result (r)
type(n), dimension(:), target :: xs
type(n) :: r
integer :: i
if (size(xs)>0) then
allocate(r%p(size(xs)), source=xs)
do i = 1, size(xs)
r%p(i) = xs(i)
end do
else
r%empty = .true.
end if
end function
recursive subroutine flatten1(x,r)
integer, dimension (:), allocatable, intent(inout) :: r
type(n), intent(in) :: x
integer, dimension (:), allocatable :: tmp
integer :: i
if (associated(x%p)) then
do i = 1, size(x%p)
call flatten1(x%p(i), r)
end do
elseif (.not. x%empty) then
allocate(tmp(size(r)+1))
tmp(1:size(r)) = r
tmp(size(r)+1) = x%a
call move_alloc(tmp, r)
end if
end subroutine
function flatten(x) result (r)
type(n), intent(in) :: x
integer, dimension(:), allocatable :: r
allocate(r(0))
call flatten1(x,r)
end function
recursive subroutine show(x)
type(n) :: x
integer :: i
if (x%empty) then
write (*, "(a)", advance="no") "[]"
elseif (associated(x%p)) then
write (*, "(a)", advance="no") "["
do i = 1, size(x%p)
call show(x%p(i))
if (i<size(x%p)) then
write (*, "(a)", advance="no") ", "
end if
end do
write (*, "(a)", advance="no") "]"
else
write (*, "(g0)", advance="no") x%a
end if
end subroutine
function fromString(line) result (r)
character(len=*) :: line
type (n) :: r
type (n), dimension(:), allocatable :: buffer, buffer1
integer, dimension(:), allocatable :: stack, stack1
integer :: sp,i0,i,j, a, cur, start
character :: c
if (.not. allocated(buffer)) then
allocate (buffer(5))
end if
if (.not. allocated(stack)) then
allocate (stack(5))
end if
sp = 1; cur = 1; i = 1
do
if ( i > len_trim(line) ) exit
c = line(i:i)
if (c=="[") then
if (sp>size(stack)) then
allocate(stack1(2*size(stack)))
stack1(1:size(stack)) = stack
call move_alloc(stack1, stack)
end if
stack(sp) = cur; sp = sp + 1; i = i+1
elseif (c=="]") then
sp = sp - 1; start = stack(sp)
r = join(buffer(start:cur-1))
do j = start, cur-1
call del(buffer(j))
end do
buffer(start) = r; cur = start+1; i = i+1
elseif (index(" ,",c)>0) then
i = i + 1; continue
elseif (index("-123456789",c)>0) then
i0 = i
do
if ((i>len_trim(line)).or. &
index("1234567890",line(i:i))==0) then
read(line(i0:i-1),*) a
if (cur>size(buffer)) then
allocate(buffer1(2*size(buffer)))
buffer1(1:size(buffer)) = buffer
call move_alloc(buffer1, buffer)
end if
buffer(cur) = n(a); cur = cur + 1; exit
else
i = i+1
end if
end do
else
stop "input corrupted"
end if
end do
end function
end module
program main
use flat
type (n) :: x
x = fromString("[[1], 2, [[3,4], 5], [[[]]], [[[6]]], 7, 8, []]")
write(*, "(a)", advance="no") "input : "
call show(x)
print *
write (*,"(a)", advance="no") "flatten : ["
write (*, "(*(i0,:,:', '))", advance="no") flatten(x)
print *, "]"
end program
|
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
if (associated(this%p)) then
do i = 1, size(this%p)
call del(this%p(i))
end do
end if
end subroutine
function join(xs) result (r)
type(n), dimension(:), target :: xs
type(n) :: r
integer :: i
if (size(xs)>0) then
allocate(r%p(size(xs)), source=xs)
do i = 1, size(xs)
r%p(i) = xs(i)
end do
else
r%empty = .true.
end if
end function
recursive subroutine flatten1(x,r)
integer, dimension (:), allocatable, intent(inout) :: r
type(n), intent(in) :: x
integer, dimension (:), allocatable :: tmp
integer :: i
if (associated(x%p)) then
do i = 1, size(x%p)
call flatten1(x%p(i), r)
end do
elseif (.not. x%empty) then
allocate(tmp(size(r)+1))
tmp(1:size(r)) = r
tmp(size(r)+1) = x%a
call move_alloc(tmp, r)
end if
end subroutine
function flatten(x) result (r)
type(n), intent(in) :: x
integer, dimension(:), allocatable :: r
allocate(r(0))
call flatten1(x,r)
end function
recursive subroutine show(x)
type(n) :: x
integer :: i
if (x%empty) then
write (*, "(a)", advance="no") "[]"
elseif (associated(x%p)) then
write (*, "(a)", advance="no") "["
do i = 1, size(x%p)
call show(x%p(i))
if (i<size(x%p)) then
write (*, "(a)", advance="no") ", "
end if
end do
write (*, "(a)", advance="no") "]"
else
write (*, "(g0)", advance="no") x%a
end if
end subroutine
function fromString(line) result (r)
character(len=*) :: line
type (n) :: r
type (n), dimension(:), allocatable :: buffer, buffer1
integer, dimension(:), allocatable :: stack, stack1
integer :: sp,i0,i,j, a, cur, start
character :: c
if (.not. allocated(buffer)) then
allocate (buffer(5))
end if
if (.not. allocated(stack)) then
allocate (stack(5))
end if
sp = 1; cur = 1; i = 1
do
if ( i > len_trim(line) ) exit
c = line(i:i)
if (c=="[") then
if (sp>size(stack)) then
allocate(stack1(2*size(stack)))
stack1(1:size(stack)) = stack
call move_alloc(stack1, stack)
end if
stack(sp) = cur; sp = sp + 1; i = i+1
elseif (c=="]") then
sp = sp - 1; start = stack(sp)
r = join(buffer(start:cur-1))
do j = start, cur-1
call del(buffer(j))
end do
buffer(start) = r; cur = start+1; i = i+1
elseif (index(" ,",c)>0) then
i = i + 1; continue
elseif (index("-123456789",c)>0) then
i0 = i
do
if ((i>len_trim(line)).or. &
index("1234567890",line(i:i))==0) then
read(line(i0:i-1),*) a
if (cur>size(buffer)) then
allocate(buffer1(2*size(buffer)))
buffer1(1:size(buffer)) = buffer
call move_alloc(buffer1, buffer)
end if
buffer(cur) = n(a); cur = cur + 1; exit
else
i = i+1
end if
end do
else
stop "input corrupted"
end if
end do
end function
end module
program main
use flat
type (n) :: x
x = fromString("[[1], 2, [[3,4], 5], [[[]]], [[[6]]], 7, 8, []]")
write(*, "(a)", advance="no") "input : "
call show(x)
print *
write (*,"(a)", advance="no") "flatten : ["
write (*, "(*(i0,:,:', '))", advance="no") flatten(x)
print *, "]"
end program
|
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(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 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(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;
}
|
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 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 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 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 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;
++next;
list.splice(next, boost::any_cast<anylist&>(*current));
current = list.erase(current);
}
else
++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;
++next;
list.splice(next, boost::any_cast<anylist&>(*current));
current = list.erase(current);
}
else
++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 (Object item : fromTreeList) {
if (item instanceof List<?>) {
flatten((List<?>) item, toFlatList);
} else {
toFlatList.add(item);
}
}
}
}
|
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 (Object item : fromTreeList) {
if (item instanceof List<?>) {
flatten((List<?>) item, toFlatList);
} else {
toFlatList.add(item);
}
}
}
}
|
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 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 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 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 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 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 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 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 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 [Node [Node []]]
, Node [Node [Node [Leaf 6]]]
, Leaf 7
, Leaf 8
, Node []
]
main =
show (flatten tree)
| #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;
}
|
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 [Node [Node []]]
, Node [Node [Node [Leaf 6]]]
, Leaf 7
, Leaf 8
, Node []
]
main =
show (flatten tree)
| #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;
}
|
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 [Node [Node []]]
, Node [Node [Node [Leaf 6]]]
, Leaf 7
, Leaf 8
, Node []
]
main =
show (flatten tree)
| 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;
}
}
}
|
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 [Node [Node []]]
, Node [Node [Node [Leaf 6]]]
, Leaf 7
, Leaf 8
, Node []
]
main =
show (flatten tree)
| 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;
}
}
}
|
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 [Node [Node []]]
, Node [Node [Node [Leaf 6]]]
, Leaf 7
, Leaf 8
, Node []
]
main =
show (flatten tree)
| #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;
}
}
|
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 [Node [Node []]]
, Node [Node [Node [Leaf 6]]]
, Leaf 7
, Leaf 8
, Node []
]
main =
show (flatten tree)
| #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;
}
}
|
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 [Node [Node []]]
, Node [Node [Node [Leaf 6]]]
, Leaf 7
, Leaf 8
, Node []
]
main =
show (flatten tree)
| 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 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 [Node [Node []]]
, Node [Node [Node [Leaf 6]]]
, Leaf 7
, Leaf 8
, Node []
]
main =
show (flatten tree)
| 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 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 [Node [Node []]]
, Node [Node [Node [Leaf 6]]]
, Leaf 7
, Leaf 8
, Node []
]
main =
show (flatten tree)
| >>> 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 [Node [Node []]]
, Node [Node [Node [Leaf 6]]]
, Leaf 7
, Leaf 8
, Node []
]
main =
show (flatten tree)
| >>> 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 [Node [Node []]]
, Node [Node [Node [Leaf 6]]]
, Leaf 7
, Leaf 8
, Node []
]
main =
show (flatten tree)
| 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 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 [Node [Node []]]
, Node [Node [Node [Leaf 6]]]
, Leaf 7
, Leaf 8
, Node []
]
main =
show (flatten tree)
| 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 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 [Node [Node []]]
, Node [Node [Node [Leaf 6]]]
, Leaf 7
, Leaf 8
, Node []
]
main =
show (flatten tree)
| 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 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 [Node [Node []]]
, Node [Node [Node [Leaf 6]]]
, Leaf 7
, Leaf 8
, Node []
]
main =
show (flatten tree)
| 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 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(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 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(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;
}
|
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 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 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 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 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;
++next;
list.splice(next, boost::any_cast<anylist&>(*current));
current = list.erase(current);
}
else
++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;
++next;
list.splice(next, boost::any_cast<anylist&>(*current));
current = list.erase(current);
}
else
++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 (Object item : fromTreeList) {
if (item instanceof List<?>) {
flatten((List<?>) item, toFlatList);
} else {
toFlatList.add(item);
}
}
}
}
|
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 (Object item : fromTreeList) {
if (item instanceof List<?>) {
flatten((List<?>) item, toFlatList);
} else {
toFlatList.add(item);
}
}
}
}
|
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 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
|
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 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 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 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
}
|
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 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 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(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 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(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;
}
|
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 index = NewList.IndexOf ( NewList.OfType<ArrayList> ( ).ElementAt ( 0 ) );
ArrayList Temp = (ArrayList)NewList[index];
NewList.RemoveAt ( index );
NewList.InsertRange ( index, Temp );
}
return NewList;
}
}
}
|
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 index = NewList.IndexOf ( NewList.OfType<ArrayList> ( ).ElementAt ( 0 ) );
ArrayList Temp = (ArrayList)NewList[index];
NewList.RemoveAt ( index );
NewList.InsertRange ( index, Temp );
}
return NewList;
}
}
}
|
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;
++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 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;
++next;
list.splice(next, boost::any_cast<anylist&>(*current));
current = list.erase(current);
}
else
++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 (Object item : fromTreeList) {
if (item instanceof List<?>) {
flatten((List<?>) item, toFlatList);
} else {
toFlatList.add(item);
}
}
}
}
|
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 (Object item : fromTreeList) {
if (item instanceof List<?>) {
flatten((List<?>) item, toFlatList);
} else {
toFlatList.add(item);
}
}
}
}
|
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 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 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 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 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 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
}
|
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 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 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(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 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(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 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 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 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 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 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;
++next;
list.splice(next, boost::any_cast<anylist&>(*current));
current = list.erase(current);
}
else
++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;
++next;
list.splice(next, boost::any_cast<anylist&>(*current));
current = list.erase(current);
}
else
++current;
}
}
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.