Instruction
stringlengths
45
106
input_code
stringlengths
1
13.7k
output_code
stringlengths
1
13.7k
Translate the given R code snippet into Go without altering its behavior.
s <- sapply (1:10, function (x) { x function (i=x) i*i }) s[[5]]() [1] 25
package main import "fmt" func main() { fs := make([]func() int, 10) for i := range fs { i := i fs[i] = func() int { return i * i } } fmt.Println("func #0:", fs[0]()) fmt.Println("func #3:", fs[3]()) }
Preserve the algorithm and functionality while converting the code from Racket to C.
#lang racket (define functions (for/list ([i 10]) (λ() (* i i)))) (map (λ(f) (f)) functions)
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <sys/mman.h> typedef int (*f_int)(); #define TAG 0xdeadbeef int _tmpl() { volatile int x = TAG; return x * x; } #define PROT (PROT_EXEC | PROT_WRITE) #define FLAGS (MAP_PRIVATE | MAP_ANONYMOUS) f_int dupf(int v) { size_t len = (void*)dupf - (void*)_tmpl; f_int ret = mmap(NULL, len, PROT, FLAGS, 0, 0); char *p; if(ret == MAP_FAILED) { perror("mmap"); exit(-1); } memcpy(ret, _tmpl, len); for (p = (char*)ret; p < (char*)ret + len - sizeof(int); p++) if (*(int *)p == TAG) *(int *)p = v; return ret; } int main() { f_int funcs[10]; int i; for (i = 0; i < 10; i++) funcs[i] = dupf(i); for (i = 0; i < 9; i++) printf("func[%d]: %d\n", i, funcs[i]()); return 0; }
Port the provided Racket code into C while preserving the original functionality.
#lang racket (define functions (for/list ([i 10]) (λ() (* i i)))) (map (λ(f) (f)) functions)
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <sys/mman.h> typedef int (*f_int)(); #define TAG 0xdeadbeef int _tmpl() { volatile int x = TAG; return x * x; } #define PROT (PROT_EXEC | PROT_WRITE) #define FLAGS (MAP_PRIVATE | MAP_ANONYMOUS) f_int dupf(int v) { size_t len = (void*)dupf - (void*)_tmpl; f_int ret = mmap(NULL, len, PROT, FLAGS, 0, 0); char *p; if(ret == MAP_FAILED) { perror("mmap"); exit(-1); } memcpy(ret, _tmpl, len); for (p = (char*)ret; p < (char*)ret + len - sizeof(int); p++) if (*(int *)p == TAG) *(int *)p = v; return ret; } int main() { f_int funcs[10]; int i; for (i = 0; i < 10; i++) funcs[i] = dupf(i); for (i = 0; i < 9; i++) printf("func[%d]: %d\n", i, funcs[i]()); return 0; }
Port the provided Racket code into C# while preserving the original functionality.
#lang racket (define functions (for/list ([i 10]) (λ() (* i i)))) (map (λ(f) (f)) functions)
using System; using System.Linq; class Program { static void Main() { var captor = (Func<int, Func<int>>)(number => () => number * number); var functions = Enumerable.Range(0, 10).Select(captor); foreach (var function in functions.Take(9)) { Console.WriteLine(function()); } } }
Port the following code from Racket to C# with equivalent syntax and logic.
#lang racket (define functions (for/list ([i 10]) (λ() (* i i)))) (map (λ(f) (f)) functions)
using System; using System.Linq; class Program { static void Main() { var captor = (Func<int, Func<int>>)(number => () => number * number); var functions = Enumerable.Range(0, 10).Select(captor); foreach (var function in functions.Take(9)) { Console.WriteLine(function()); } } }
Translate the given Racket code snippet into C++ without altering its behavior.
#lang racket (define functions (for/list ([i 10]) (λ() (* i i)))) (map (λ(f) (f)) functions)
#include <iostream> #include <functional> #include <vector> int main() { std::vector<std::function<int()> > funcs; for (int i = 0; i < 10; i++) funcs.push_back([=]() { return i * i; }); for ( std::function<int( )> f : funcs ) std::cout << f( ) << std::endl ; return 0; }
Rewrite this program in C++ while keeping its functionality equivalent to the Racket version.
#lang racket (define functions (for/list ([i 10]) (λ() (* i i)))) (map (λ(f) (f)) functions)
#include <iostream> #include <functional> #include <vector> int main() { std::vector<std::function<int()> > funcs; for (int i = 0; i < 10; i++) funcs.push_back([=]() { return i * i; }); for ( std::function<int( )> f : funcs ) std::cout << f( ) << std::endl ; return 0; }
Convert this Racket block to Java, preserving its control flow and logic.
#lang racket (define functions (for/list ([i 10]) (λ() (* i i)))) (map (λ(f) (f)) functions)
import java.util.function.Supplier; import java.util.ArrayList; public class ValueCapture { public static void main(String[] args) { ArrayList<Supplier<Integer>> funcs = new ArrayList<>(); for (int i = 0; i < 10; i++) { int j = i; funcs.add(() -> j * j); } Supplier<Integer> foo = funcs.get(3); System.out.println(foo.get()); } }
Ensure the translated Java code behaves exactly like the original Racket snippet.
#lang racket (define functions (for/list ([i 10]) (λ() (* i i)))) (map (λ(f) (f)) functions)
import java.util.function.Supplier; import java.util.ArrayList; public class ValueCapture { public static void main(String[] args) { ArrayList<Supplier<Integer>> funcs = new ArrayList<>(); for (int i = 0; i < 10; i++) { int j = i; funcs.add(() -> j * j); } Supplier<Integer> foo = funcs.get(3); System.out.println(foo.get()); } }
Ensure the translated Python code behaves exactly like the original Racket snippet.
#lang racket (define functions (for/list ([i 10]) (λ() (* i i)))) (map (λ(f) (f)) functions)
funcs = [] for i in range(10): funcs.append(lambda: i * i) print funcs[3]()
Translate the given Racket code snippet into Python without altering its behavior.
#lang racket (define functions (for/list ([i 10]) (λ() (* i i)))) (map (λ(f) (f)) functions)
funcs = [] for i in range(10): funcs.append(lambda: i * i) print funcs[3]()
Keep all operations the same but rewrite the snippet in Go.
#lang racket (define functions (for/list ([i 10]) (λ() (* i i)))) (map (λ(f) (f)) functions)
package main import "fmt" func main() { fs := make([]func() int, 10) for i := range fs { i := i fs[i] = func() int { return i * i } } fmt.Println("func #0:", fs[0]()) fmt.Println("func #3:", fs[3]()) }
Produce a functionally identical Go code for the snippet given in Racket.
#lang racket (define functions (for/list ([i 10]) (λ() (* i i)))) (map (λ(f) (f)) functions)
package main import "fmt" func main() { fs := make([]func() int, 10) for i := range fs { i := i fs[i] = func() int { return i * i } } fmt.Println("func #0:", fs[0]()) fmt.Println("func #3:", fs[3]()) }
Write the same algorithm in C as shown in this REXX implementation.
parse arg seed base $ if datatype(seed, 'W') then call random ,,seed if base=='' | base="," then base=0 if $='' then $= 8 5 4 9 1 3 2 7 6 0 say 'the' word("zero one", base+1)'─based list is: ' $ ?='.'random(0, 9) interpret 'CALL' ? say 'function ' ? " returned " result exit .0: return .(0) .1: return .(1) .2: return .(2) .3: return .(3) .4: return .(4) .5: return .(5) .6: return .(6) .7: return .(7) .8: return .(8) .9: return .(9) .: arg #; _=wordpos(#,$); if _==0 then return 'not in the list.'; return (_-(\base))**2
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <sys/mman.h> typedef int (*f_int)(); #define TAG 0xdeadbeef int _tmpl() { volatile int x = TAG; return x * x; } #define PROT (PROT_EXEC | PROT_WRITE) #define FLAGS (MAP_PRIVATE | MAP_ANONYMOUS) f_int dupf(int v) { size_t len = (void*)dupf - (void*)_tmpl; f_int ret = mmap(NULL, len, PROT, FLAGS, 0, 0); char *p; if(ret == MAP_FAILED) { perror("mmap"); exit(-1); } memcpy(ret, _tmpl, len); for (p = (char*)ret; p < (char*)ret + len - sizeof(int); p++) if (*(int *)p == TAG) *(int *)p = v; return ret; } int main() { f_int funcs[10]; int i; for (i = 0; i < 10; i++) funcs[i] = dupf(i); for (i = 0; i < 9; i++) printf("func[%d]: %d\n", i, funcs[i]()); return 0; }
Convert this REXX snippet to C and keep its semantics consistent.
parse arg seed base $ if datatype(seed, 'W') then call random ,,seed if base=='' | base="," then base=0 if $='' then $= 8 5 4 9 1 3 2 7 6 0 say 'the' word("zero one", base+1)'─based list is: ' $ ?='.'random(0, 9) interpret 'CALL' ? say 'function ' ? " returned " result exit .0: return .(0) .1: return .(1) .2: return .(2) .3: return .(3) .4: return .(4) .5: return .(5) .6: return .(6) .7: return .(7) .8: return .(8) .9: return .(9) .: arg #; _=wordpos(#,$); if _==0 then return 'not in the list.'; return (_-(\base))**2
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <sys/mman.h> typedef int (*f_int)(); #define TAG 0xdeadbeef int _tmpl() { volatile int x = TAG; return x * x; } #define PROT (PROT_EXEC | PROT_WRITE) #define FLAGS (MAP_PRIVATE | MAP_ANONYMOUS) f_int dupf(int v) { size_t len = (void*)dupf - (void*)_tmpl; f_int ret = mmap(NULL, len, PROT, FLAGS, 0, 0); char *p; if(ret == MAP_FAILED) { perror("mmap"); exit(-1); } memcpy(ret, _tmpl, len); for (p = (char*)ret; p < (char*)ret + len - sizeof(int); p++) if (*(int *)p == TAG) *(int *)p = v; return ret; } int main() { f_int funcs[10]; int i; for (i = 0; i < 10; i++) funcs[i] = dupf(i); for (i = 0; i < 9; i++) printf("func[%d]: %d\n", i, funcs[i]()); return 0; }
Convert this REXX block to C#, preserving its control flow and logic.
parse arg seed base $ if datatype(seed, 'W') then call random ,,seed if base=='' | base="," then base=0 if $='' then $= 8 5 4 9 1 3 2 7 6 0 say 'the' word("zero one", base+1)'─based list is: ' $ ?='.'random(0, 9) interpret 'CALL' ? say 'function ' ? " returned " result exit .0: return .(0) .1: return .(1) .2: return .(2) .3: return .(3) .4: return .(4) .5: return .(5) .6: return .(6) .7: return .(7) .8: return .(8) .9: return .(9) .: arg #; _=wordpos(#,$); if _==0 then return 'not in the list.'; return (_-(\base))**2
using System; using System.Linq; class Program { static void Main() { var captor = (Func<int, Func<int>>)(number => () => number * number); var functions = Enumerable.Range(0, 10).Select(captor); foreach (var function in functions.Take(9)) { Console.WriteLine(function()); } } }
Transform the following REXX implementation into C#, maintaining the same output and logic.
parse arg seed base $ if datatype(seed, 'W') then call random ,,seed if base=='' | base="," then base=0 if $='' then $= 8 5 4 9 1 3 2 7 6 0 say 'the' word("zero one", base+1)'─based list is: ' $ ?='.'random(0, 9) interpret 'CALL' ? say 'function ' ? " returned " result exit .0: return .(0) .1: return .(1) .2: return .(2) .3: return .(3) .4: return .(4) .5: return .(5) .6: return .(6) .7: return .(7) .8: return .(8) .9: return .(9) .: arg #; _=wordpos(#,$); if _==0 then return 'not in the list.'; return (_-(\base))**2
using System; using System.Linq; class Program { static void Main() { var captor = (Func<int, Func<int>>)(number => () => number * number); var functions = Enumerable.Range(0, 10).Select(captor); foreach (var function in functions.Take(9)) { Console.WriteLine(function()); } } }
Can you help me rewrite this code in C++ instead of REXX, keeping it the same logically?
parse arg seed base $ if datatype(seed, 'W') then call random ,,seed if base=='' | base="," then base=0 if $='' then $= 8 5 4 9 1 3 2 7 6 0 say 'the' word("zero one", base+1)'─based list is: ' $ ?='.'random(0, 9) interpret 'CALL' ? say 'function ' ? " returned " result exit .0: return .(0) .1: return .(1) .2: return .(2) .3: return .(3) .4: return .(4) .5: return .(5) .6: return .(6) .7: return .(7) .8: return .(8) .9: return .(9) .: arg #; _=wordpos(#,$); if _==0 then return 'not in the list.'; return (_-(\base))**2
#include <iostream> #include <functional> #include <vector> int main() { std::vector<std::function<int()> > funcs; for (int i = 0; i < 10; i++) funcs.push_back([=]() { return i * i; }); for ( std::function<int( )> f : funcs ) std::cout << f( ) << std::endl ; return 0; }
Produce a functionally identical C++ code for the snippet given in REXX.
parse arg seed base $ if datatype(seed, 'W') then call random ,,seed if base=='' | base="," then base=0 if $='' then $= 8 5 4 9 1 3 2 7 6 0 say 'the' word("zero one", base+1)'─based list is: ' $ ?='.'random(0, 9) interpret 'CALL' ? say 'function ' ? " returned " result exit .0: return .(0) .1: return .(1) .2: return .(2) .3: return .(3) .4: return .(4) .5: return .(5) .6: return .(6) .7: return .(7) .8: return .(8) .9: return .(9) .: arg #; _=wordpos(#,$); if _==0 then return 'not in the list.'; return (_-(\base))**2
#include <iostream> #include <functional> #include <vector> int main() { std::vector<std::function<int()> > funcs; for (int i = 0; i < 10; i++) funcs.push_back([=]() { return i * i; }); for ( std::function<int( )> f : funcs ) std::cout << f( ) << std::endl ; return 0; }
Write a version of this REXX function in Java with identical behavior.
parse arg seed base $ if datatype(seed, 'W') then call random ,,seed if base=='' | base="," then base=0 if $='' then $= 8 5 4 9 1 3 2 7 6 0 say 'the' word("zero one", base+1)'─based list is: ' $ ?='.'random(0, 9) interpret 'CALL' ? say 'function ' ? " returned " result exit .0: return .(0) .1: return .(1) .2: return .(2) .3: return .(3) .4: return .(4) .5: return .(5) .6: return .(6) .7: return .(7) .8: return .(8) .9: return .(9) .: arg #; _=wordpos(#,$); if _==0 then return 'not in the list.'; return (_-(\base))**2
import java.util.function.Supplier; import java.util.ArrayList; public class ValueCapture { public static void main(String[] args) { ArrayList<Supplier<Integer>> funcs = new ArrayList<>(); for (int i = 0; i < 10; i++) { int j = i; funcs.add(() -> j * j); } Supplier<Integer> foo = funcs.get(3); System.out.println(foo.get()); } }
Rewrite this program in Java while keeping its functionality equivalent to the REXX version.
parse arg seed base $ if datatype(seed, 'W') then call random ,,seed if base=='' | base="," then base=0 if $='' then $= 8 5 4 9 1 3 2 7 6 0 say 'the' word("zero one", base+1)'─based list is: ' $ ?='.'random(0, 9) interpret 'CALL' ? say 'function ' ? " returned " result exit .0: return .(0) .1: return .(1) .2: return .(2) .3: return .(3) .4: return .(4) .5: return .(5) .6: return .(6) .7: return .(7) .8: return .(8) .9: return .(9) .: arg #; _=wordpos(#,$); if _==0 then return 'not in the list.'; return (_-(\base))**2
import java.util.function.Supplier; import java.util.ArrayList; public class ValueCapture { public static void main(String[] args) { ArrayList<Supplier<Integer>> funcs = new ArrayList<>(); for (int i = 0; i < 10; i++) { int j = i; funcs.add(() -> j * j); } Supplier<Integer> foo = funcs.get(3); System.out.println(foo.get()); } }
Write the same algorithm in Python as shown in this REXX implementation.
parse arg seed base $ if datatype(seed, 'W') then call random ,,seed if base=='' | base="," then base=0 if $='' then $= 8 5 4 9 1 3 2 7 6 0 say 'the' word("zero one", base+1)'─based list is: ' $ ?='.'random(0, 9) interpret 'CALL' ? say 'function ' ? " returned " result exit .0: return .(0) .1: return .(1) .2: return .(2) .3: return .(3) .4: return .(4) .5: return .(5) .6: return .(6) .7: return .(7) .8: return .(8) .9: return .(9) .: arg #; _=wordpos(#,$); if _==0 then return 'not in the list.'; return (_-(\base))**2
funcs = [] for i in range(10): funcs.append(lambda: i * i) print funcs[3]()
Translate this program into Python but keep the logic exactly as in REXX.
parse arg seed base $ if datatype(seed, 'W') then call random ,,seed if base=='' | base="," then base=0 if $='' then $= 8 5 4 9 1 3 2 7 6 0 say 'the' word("zero one", base+1)'─based list is: ' $ ?='.'random(0, 9) interpret 'CALL' ? say 'function ' ? " returned " result exit .0: return .(0) .1: return .(1) .2: return .(2) .3: return .(3) .4: return .(4) .5: return .(5) .6: return .(6) .7: return .(7) .8: return .(8) .9: return .(9) .: arg #; _=wordpos(#,$); if _==0 then return 'not in the list.'; return (_-(\base))**2
funcs = [] for i in range(10): funcs.append(lambda: i * i) print funcs[3]()
Preserve the algorithm and functionality while converting the code from REXX to Go.
parse arg seed base $ if datatype(seed, 'W') then call random ,,seed if base=='' | base="," then base=0 if $='' then $= 8 5 4 9 1 3 2 7 6 0 say 'the' word("zero one", base+1)'─based list is: ' $ ?='.'random(0, 9) interpret 'CALL' ? say 'function ' ? " returned " result exit .0: return .(0) .1: return .(1) .2: return .(2) .3: return .(3) .4: return .(4) .5: return .(5) .6: return .(6) .7: return .(7) .8: return .(8) .9: return .(9) .: arg #; _=wordpos(#,$); if _==0 then return 'not in the list.'; return (_-(\base))**2
package main import "fmt" func main() { fs := make([]func() int, 10) for i := range fs { i := i fs[i] = func() int { return i * i } } fmt.Println("func #0:", fs[0]()) fmt.Println("func #3:", fs[3]()) }
Translate the given REXX code snippet into Go without altering its behavior.
parse arg seed base $ if datatype(seed, 'W') then call random ,,seed if base=='' | base="," then base=0 if $='' then $= 8 5 4 9 1 3 2 7 6 0 say 'the' word("zero one", base+1)'─based list is: ' $ ?='.'random(0, 9) interpret 'CALL' ? say 'function ' ? " returned " result exit .0: return .(0) .1: return .(1) .2: return .(2) .3: return .(3) .4: return .(4) .5: return .(5) .6: return .(6) .7: return .(7) .8: return .(8) .9: return .(9) .: arg #; _=wordpos(#,$); if _==0 then return 'not in the list.'; return (_-(\base))**2
package main import "fmt" func main() { fs := make([]func() int, 10) for i := range fs { i := i fs[i] = func() int { return i * i } } fmt.Println("func #0:", fs[0]()) fmt.Println("func #3:", fs[3]()) }
Change the following Ruby code into C without altering its purpose.
procs = Array.new(10){|i| ->{i*i} } p procs[7].call
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <sys/mman.h> typedef int (*f_int)(); #define TAG 0xdeadbeef int _tmpl() { volatile int x = TAG; return x * x; } #define PROT (PROT_EXEC | PROT_WRITE) #define FLAGS (MAP_PRIVATE | MAP_ANONYMOUS) f_int dupf(int v) { size_t len = (void*)dupf - (void*)_tmpl; f_int ret = mmap(NULL, len, PROT, FLAGS, 0, 0); char *p; if(ret == MAP_FAILED) { perror("mmap"); exit(-1); } memcpy(ret, _tmpl, len); for (p = (char*)ret; p < (char*)ret + len - sizeof(int); p++) if (*(int *)p == TAG) *(int *)p = v; return ret; } int main() { f_int funcs[10]; int i; for (i = 0; i < 10; i++) funcs[i] = dupf(i); for (i = 0; i < 9; i++) printf("func[%d]: %d\n", i, funcs[i]()); return 0; }
Preserve the algorithm and functionality while converting the code from Ruby to C.
procs = Array.new(10){|i| ->{i*i} } p procs[7].call
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <sys/mman.h> typedef int (*f_int)(); #define TAG 0xdeadbeef int _tmpl() { volatile int x = TAG; return x * x; } #define PROT (PROT_EXEC | PROT_WRITE) #define FLAGS (MAP_PRIVATE | MAP_ANONYMOUS) f_int dupf(int v) { size_t len = (void*)dupf - (void*)_tmpl; f_int ret = mmap(NULL, len, PROT, FLAGS, 0, 0); char *p; if(ret == MAP_FAILED) { perror("mmap"); exit(-1); } memcpy(ret, _tmpl, len); for (p = (char*)ret; p < (char*)ret + len - sizeof(int); p++) if (*(int *)p == TAG) *(int *)p = v; return ret; } int main() { f_int funcs[10]; int i; for (i = 0; i < 10; i++) funcs[i] = dupf(i); for (i = 0; i < 9; i++) printf("func[%d]: %d\n", i, funcs[i]()); return 0; }
Translate this program into C# but keep the logic exactly as in Ruby.
procs = Array.new(10){|i| ->{i*i} } p procs[7].call
using System; using System.Linq; class Program { static void Main() { var captor = (Func<int, Func<int>>)(number => () => number * number); var functions = Enumerable.Range(0, 10).Select(captor); foreach (var function in functions.Take(9)) { Console.WriteLine(function()); } } }
Generate a C# translation of this Ruby snippet without changing its computational steps.
procs = Array.new(10){|i| ->{i*i} } p procs[7].call
using System; using System.Linq; class Program { static void Main() { var captor = (Func<int, Func<int>>)(number => () => number * number); var functions = Enumerable.Range(0, 10).Select(captor); foreach (var function in functions.Take(9)) { Console.WriteLine(function()); } } }
Produce a language-to-language conversion: from Ruby to C++, same semantics.
procs = Array.new(10){|i| ->{i*i} } p procs[7].call
#include <iostream> #include <functional> #include <vector> int main() { std::vector<std::function<int()> > funcs; for (int i = 0; i < 10; i++) funcs.push_back([=]() { return i * i; }); for ( std::function<int( )> f : funcs ) std::cout << f( ) << std::endl ; return 0; }
Maintain the same structure and functionality when rewriting this code in C++.
procs = Array.new(10){|i| ->{i*i} } p procs[7].call
#include <iostream> #include <functional> #include <vector> int main() { std::vector<std::function<int()> > funcs; for (int i = 0; i < 10; i++) funcs.push_back([=]() { return i * i; }); for ( std::function<int( )> f : funcs ) std::cout << f( ) << std::endl ; return 0; }
Rewrite this program in Java while keeping its functionality equivalent to the Ruby version.
procs = Array.new(10){|i| ->{i*i} } p procs[7].call
import java.util.function.Supplier; import java.util.ArrayList; public class ValueCapture { public static void main(String[] args) { ArrayList<Supplier<Integer>> funcs = new ArrayList<>(); for (int i = 0; i < 10; i++) { int j = i; funcs.add(() -> j * j); } Supplier<Integer> foo = funcs.get(3); System.out.println(foo.get()); } }
Rewrite the snippet below in Java so it works the same as the original Ruby code.
procs = Array.new(10){|i| ->{i*i} } p procs[7].call
import java.util.function.Supplier; import java.util.ArrayList; public class ValueCapture { public static void main(String[] args) { ArrayList<Supplier<Integer>> funcs = new ArrayList<>(); for (int i = 0; i < 10; i++) { int j = i; funcs.add(() -> j * j); } Supplier<Integer> foo = funcs.get(3); System.out.println(foo.get()); } }
Rewrite this program in Python while keeping its functionality equivalent to the Ruby version.
procs = Array.new(10){|i| ->{i*i} } p procs[7].call
funcs = [] for i in range(10): funcs.append(lambda: i * i) print funcs[3]()
Write the same code in Python as shown below in Ruby.
procs = Array.new(10){|i| ->{i*i} } p procs[7].call
funcs = [] for i in range(10): funcs.append(lambda: i * i) print funcs[3]()
Convert this Ruby snippet to Go and keep its semantics consistent.
procs = Array.new(10){|i| ->{i*i} } p procs[7].call
package main import "fmt" func main() { fs := make([]func() int, 10) for i := range fs { i := i fs[i] = func() int { return i * i } } fmt.Println("func #0:", fs[0]()) fmt.Println("func #3:", fs[3]()) }
Generate an equivalent Go version of this Ruby code.
procs = Array.new(10){|i| ->{i*i} } p procs[7].call
package main import "fmt" func main() { fs := make([]func() int, 10) for i := range fs { i := i fs[i] = func() int { return i * i } } fmt.Println("func #0:", fs[0]()) fmt.Println("func #3:", fs[3]()) }
Generate a C translation of this Scala snippet without changing its computational steps.
fun main(args: Array<String>) { val funcs = Array(10){ fun(): Int = it * it } (0 .. 8).forEach { println(funcs[it]()) } }
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <sys/mman.h> typedef int (*f_int)(); #define TAG 0xdeadbeef int _tmpl() { volatile int x = TAG; return x * x; } #define PROT (PROT_EXEC | PROT_WRITE) #define FLAGS (MAP_PRIVATE | MAP_ANONYMOUS) f_int dupf(int v) { size_t len = (void*)dupf - (void*)_tmpl; f_int ret = mmap(NULL, len, PROT, FLAGS, 0, 0); char *p; if(ret == MAP_FAILED) { perror("mmap"); exit(-1); } memcpy(ret, _tmpl, len); for (p = (char*)ret; p < (char*)ret + len - sizeof(int); p++) if (*(int *)p == TAG) *(int *)p = v; return ret; } int main() { f_int funcs[10]; int i; for (i = 0; i < 10; i++) funcs[i] = dupf(i); for (i = 0; i < 9; i++) printf("func[%d]: %d\n", i, funcs[i]()); return 0; }
Transform the following Scala implementation into C, maintaining the same output and logic.
fun main(args: Array<String>) { val funcs = Array(10){ fun(): Int = it * it } (0 .. 8).forEach { println(funcs[it]()) } }
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <sys/mman.h> typedef int (*f_int)(); #define TAG 0xdeadbeef int _tmpl() { volatile int x = TAG; return x * x; } #define PROT (PROT_EXEC | PROT_WRITE) #define FLAGS (MAP_PRIVATE | MAP_ANONYMOUS) f_int dupf(int v) { size_t len = (void*)dupf - (void*)_tmpl; f_int ret = mmap(NULL, len, PROT, FLAGS, 0, 0); char *p; if(ret == MAP_FAILED) { perror("mmap"); exit(-1); } memcpy(ret, _tmpl, len); for (p = (char*)ret; p < (char*)ret + len - sizeof(int); p++) if (*(int *)p == TAG) *(int *)p = v; return ret; } int main() { f_int funcs[10]; int i; for (i = 0; i < 10; i++) funcs[i] = dupf(i); for (i = 0; i < 9; i++) printf("func[%d]: %d\n", i, funcs[i]()); return 0; }
Translate this program into C# but keep the logic exactly as in Scala.
fun main(args: Array<String>) { val funcs = Array(10){ fun(): Int = it * it } (0 .. 8).forEach { println(funcs[it]()) } }
using System; using System.Linq; class Program { static void Main() { var captor = (Func<int, Func<int>>)(number => () => number * number); var functions = Enumerable.Range(0, 10).Select(captor); foreach (var function in functions.Take(9)) { Console.WriteLine(function()); } } }
Generate a C# translation of this Scala snippet without changing its computational steps.
fun main(args: Array<String>) { val funcs = Array(10){ fun(): Int = it * it } (0 .. 8).forEach { println(funcs[it]()) } }
using System; using System.Linq; class Program { static void Main() { var captor = (Func<int, Func<int>>)(number => () => number * number); var functions = Enumerable.Range(0, 10).Select(captor); foreach (var function in functions.Take(9)) { Console.WriteLine(function()); } } }
Rewrite this program in C++ while keeping its functionality equivalent to the Scala version.
fun main(args: Array<String>) { val funcs = Array(10){ fun(): Int = it * it } (0 .. 8).forEach { println(funcs[it]()) } }
#include <iostream> #include <functional> #include <vector> int main() { std::vector<std::function<int()> > funcs; for (int i = 0; i < 10; i++) funcs.push_back([=]() { return i * i; }); for ( std::function<int( )> f : funcs ) std::cout << f( ) << std::endl ; return 0; }
Translate the given Scala code snippet into C++ without altering its behavior.
fun main(args: Array<String>) { val funcs = Array(10){ fun(): Int = it * it } (0 .. 8).forEach { println(funcs[it]()) } }
#include <iostream> #include <functional> #include <vector> int main() { std::vector<std::function<int()> > funcs; for (int i = 0; i < 10; i++) funcs.push_back([=]() { return i * i; }); for ( std::function<int( )> f : funcs ) std::cout << f( ) << std::endl ; return 0; }
Produce a language-to-language conversion: from Scala to Java, same semantics.
fun main(args: Array<String>) { val funcs = Array(10){ fun(): Int = it * it } (0 .. 8).forEach { println(funcs[it]()) } }
import java.util.function.Supplier; import java.util.ArrayList; public class ValueCapture { public static void main(String[] args) { ArrayList<Supplier<Integer>> funcs = new ArrayList<>(); for (int i = 0; i < 10; i++) { int j = i; funcs.add(() -> j * j); } Supplier<Integer> foo = funcs.get(3); System.out.println(foo.get()); } }
Generate a Java translation of this Scala snippet without changing its computational steps.
fun main(args: Array<String>) { val funcs = Array(10){ fun(): Int = it * it } (0 .. 8).forEach { println(funcs[it]()) } }
import java.util.function.Supplier; import java.util.ArrayList; public class ValueCapture { public static void main(String[] args) { ArrayList<Supplier<Integer>> funcs = new ArrayList<>(); for (int i = 0; i < 10; i++) { int j = i; funcs.add(() -> j * j); } Supplier<Integer> foo = funcs.get(3); System.out.println(foo.get()); } }
Convert this Scala block to Python, preserving its control flow and logic.
fun main(args: Array<String>) { val funcs = Array(10){ fun(): Int = it * it } (0 .. 8).forEach { println(funcs[it]()) } }
funcs = [] for i in range(10): funcs.append(lambda: i * i) print funcs[3]()
Convert this Scala block to Python, preserving its control flow and logic.
fun main(args: Array<String>) { val funcs = Array(10){ fun(): Int = it * it } (0 .. 8).forEach { println(funcs[it]()) } }
funcs = [] for i in range(10): funcs.append(lambda: i * i) print funcs[3]()
Write the same algorithm in Go as shown in this Scala implementation.
fun main(args: Array<String>) { val funcs = Array(10){ fun(): Int = it * it } (0 .. 8).forEach { println(funcs[it]()) } }
package main import "fmt" func main() { fs := make([]func() int, 10) for i := range fs { i := i fs[i] = func() int { return i * i } } fmt.Println("func #0:", fs[0]()) fmt.Println("func #3:", fs[3]()) }
Ensure the translated Go code behaves exactly like the original Scala snippet.
fun main(args: Array<String>) { val funcs = Array(10){ fun(): Int = it * it } (0 .. 8).forEach { println(funcs[it]()) } }
package main import "fmt" func main() { fs := make([]func() int, 10) for i := range fs { i := i fs[i] = func() int { return i * i } } fmt.Println("func #0:", fs[0]()) fmt.Println("func #3:", fs[3]()) }
Maintain the same structure and functionality when rewriting this code in C.
var funcs: [() -> Int] = [] for var i = 0; i < 10; i++ { funcs.append({ i * i }) } println(funcs[3]())
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <sys/mman.h> typedef int (*f_int)(); #define TAG 0xdeadbeef int _tmpl() { volatile int x = TAG; return x * x; } #define PROT (PROT_EXEC | PROT_WRITE) #define FLAGS (MAP_PRIVATE | MAP_ANONYMOUS) f_int dupf(int v) { size_t len = (void*)dupf - (void*)_tmpl; f_int ret = mmap(NULL, len, PROT, FLAGS, 0, 0); char *p; if(ret == MAP_FAILED) { perror("mmap"); exit(-1); } memcpy(ret, _tmpl, len); for (p = (char*)ret; p < (char*)ret + len - sizeof(int); p++) if (*(int *)p == TAG) *(int *)p = v; return ret; } int main() { f_int funcs[10]; int i; for (i = 0; i < 10; i++) funcs[i] = dupf(i); for (i = 0; i < 9; i++) printf("func[%d]: %d\n", i, funcs[i]()); return 0; }
Translate this program into C but keep the logic exactly as in Swift.
var funcs: [() -> Int] = [] for var i = 0; i < 10; i++ { funcs.append({ i * i }) } println(funcs[3]())
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <sys/mman.h> typedef int (*f_int)(); #define TAG 0xdeadbeef int _tmpl() { volatile int x = TAG; return x * x; } #define PROT (PROT_EXEC | PROT_WRITE) #define FLAGS (MAP_PRIVATE | MAP_ANONYMOUS) f_int dupf(int v) { size_t len = (void*)dupf - (void*)_tmpl; f_int ret = mmap(NULL, len, PROT, FLAGS, 0, 0); char *p; if(ret == MAP_FAILED) { perror("mmap"); exit(-1); } memcpy(ret, _tmpl, len); for (p = (char*)ret; p < (char*)ret + len - sizeof(int); p++) if (*(int *)p == TAG) *(int *)p = v; return ret; } int main() { f_int funcs[10]; int i; for (i = 0; i < 10; i++) funcs[i] = dupf(i); for (i = 0; i < 9; i++) printf("func[%d]: %d\n", i, funcs[i]()); return 0; }
Change the following Swift code into C# without altering its purpose.
var funcs: [() -> Int] = [] for var i = 0; i < 10; i++ { funcs.append({ i * i }) } println(funcs[3]())
using System; using System.Linq; class Program { static void Main() { var captor = (Func<int, Func<int>>)(number => () => number * number); var functions = Enumerable.Range(0, 10).Select(captor); foreach (var function in functions.Take(9)) { Console.WriteLine(function()); } } }
Keep all operations the same but rewrite the snippet in C#.
var funcs: [() -> Int] = [] for var i = 0; i < 10; i++ { funcs.append({ i * i }) } println(funcs[3]())
using System; using System.Linq; class Program { static void Main() { var captor = (Func<int, Func<int>>)(number => () => number * number); var functions = Enumerable.Range(0, 10).Select(captor); foreach (var function in functions.Take(9)) { Console.WriteLine(function()); } } }
Transform the following Swift implementation into C++, maintaining the same output and logic.
var funcs: [() -> Int] = [] for var i = 0; i < 10; i++ { funcs.append({ i * i }) } println(funcs[3]())
#include <iostream> #include <functional> #include <vector> int main() { std::vector<std::function<int()> > funcs; for (int i = 0; i < 10; i++) funcs.push_back([=]() { return i * i; }); for ( std::function<int( )> f : funcs ) std::cout << f( ) << std::endl ; return 0; }
Preserve the algorithm and functionality while converting the code from Swift to C++.
var funcs: [() -> Int] = [] for var i = 0; i < 10; i++ { funcs.append({ i * i }) } println(funcs[3]())
#include <iostream> #include <functional> #include <vector> int main() { std::vector<std::function<int()> > funcs; for (int i = 0; i < 10; i++) funcs.push_back([=]() { return i * i; }); for ( std::function<int( )> f : funcs ) std::cout << f( ) << std::endl ; return 0; }
Translate the given Swift code snippet into Java without altering its behavior.
var funcs: [() -> Int] = [] for var i = 0; i < 10; i++ { funcs.append({ i * i }) } println(funcs[3]())
import java.util.function.Supplier; import java.util.ArrayList; public class ValueCapture { public static void main(String[] args) { ArrayList<Supplier<Integer>> funcs = new ArrayList<>(); for (int i = 0; i < 10; i++) { int j = i; funcs.add(() -> j * j); } Supplier<Integer> foo = funcs.get(3); System.out.println(foo.get()); } }
Rewrite this program in Java while keeping its functionality equivalent to the Swift version.
var funcs: [() -> Int] = [] for var i = 0; i < 10; i++ { funcs.append({ i * i }) } println(funcs[3]())
import java.util.function.Supplier; import java.util.ArrayList; public class ValueCapture { public static void main(String[] args) { ArrayList<Supplier<Integer>> funcs = new ArrayList<>(); for (int i = 0; i < 10; i++) { int j = i; funcs.add(() -> j * j); } Supplier<Integer> foo = funcs.get(3); System.out.println(foo.get()); } }
Can you help me rewrite this code in Python instead of Swift, keeping it the same logically?
var funcs: [() -> Int] = [] for var i = 0; i < 10; i++ { funcs.append({ i * i }) } println(funcs[3]())
funcs = [] for i in range(10): funcs.append(lambda: i * i) print funcs[3]()
Generate a Python translation of this Swift snippet without changing its computational steps.
var funcs: [() -> Int] = [] for var i = 0; i < 10; i++ { funcs.append({ i * i }) } println(funcs[3]())
funcs = [] for i in range(10): funcs.append(lambda: i * i) print funcs[3]()
Change the programming language of this snippet from Swift to Go without modifying what it does.
var funcs: [() -> Int] = [] for var i = 0; i < 10; i++ { funcs.append({ i * i }) } println(funcs[3]())
package main import "fmt" func main() { fs := make([]func() int, 10) for i := range fs { i := i fs[i] = func() int { return i * i } } fmt.Println("func #0:", fs[0]()) fmt.Println("func #3:", fs[3]()) }
Write the same algorithm in Go as shown in this Swift implementation.
var funcs: [() -> Int] = [] for var i = 0; i < 10; i++ { funcs.append({ i * i }) } println(funcs[3]())
package main import "fmt" func main() { fs := make([]func() int, 10) for i := range fs { i := i fs[i] = func() int { return i * i } } fmt.Println("func #0:", fs[0]()) fmt.Println("func #3:", fs[3]()) }
Write a version of this Tcl function in C with identical behavior.
package require Tcl 8.6; proc closure {script} { set valuemap {} foreach v [uplevel 1 {info vars}] { lappend valuemap [list $v [uplevel 1 [list set $v]]] } set body [list $valuemap $script [uplevel 1 {namespace current}]] return [list apply [list {} [list tailcall apply $body]]] } proc collectFor {var from to body} { upvar 1 $var v set result {} for {set v $from} {$v < $to} {incr v} {lappend result [uplevel 1 $body]} return $result } proc buildList {} { collectFor i 0 10 { closure { return [expr $i*$i] } } } set theClosures [buildList] foreach i {a b c d e} { set idx [expr {int(rand()*9)}]; puts $idx=>[{*}[lindex $theClosures $idx]] }
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <sys/mman.h> typedef int (*f_int)(); #define TAG 0xdeadbeef int _tmpl() { volatile int x = TAG; return x * x; } #define PROT (PROT_EXEC | PROT_WRITE) #define FLAGS (MAP_PRIVATE | MAP_ANONYMOUS) f_int dupf(int v) { size_t len = (void*)dupf - (void*)_tmpl; f_int ret = mmap(NULL, len, PROT, FLAGS, 0, 0); char *p; if(ret == MAP_FAILED) { perror("mmap"); exit(-1); } memcpy(ret, _tmpl, len); for (p = (char*)ret; p < (char*)ret + len - sizeof(int); p++) if (*(int *)p == TAG) *(int *)p = v; return ret; } int main() { f_int funcs[10]; int i; for (i = 0; i < 10; i++) funcs[i] = dupf(i); for (i = 0; i < 9; i++) printf("func[%d]: %d\n", i, funcs[i]()); return 0; }
Produce a language-to-language conversion: from Tcl to C, same semantics.
package require Tcl 8.6; proc closure {script} { set valuemap {} foreach v [uplevel 1 {info vars}] { lappend valuemap [list $v [uplevel 1 [list set $v]]] } set body [list $valuemap $script [uplevel 1 {namespace current}]] return [list apply [list {} [list tailcall apply $body]]] } proc collectFor {var from to body} { upvar 1 $var v set result {} for {set v $from} {$v < $to} {incr v} {lappend result [uplevel 1 $body]} return $result } proc buildList {} { collectFor i 0 10 { closure { return [expr $i*$i] } } } set theClosures [buildList] foreach i {a b c d e} { set idx [expr {int(rand()*9)}]; puts $idx=>[{*}[lindex $theClosures $idx]] }
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <sys/mman.h> typedef int (*f_int)(); #define TAG 0xdeadbeef int _tmpl() { volatile int x = TAG; return x * x; } #define PROT (PROT_EXEC | PROT_WRITE) #define FLAGS (MAP_PRIVATE | MAP_ANONYMOUS) f_int dupf(int v) { size_t len = (void*)dupf - (void*)_tmpl; f_int ret = mmap(NULL, len, PROT, FLAGS, 0, 0); char *p; if(ret == MAP_FAILED) { perror("mmap"); exit(-1); } memcpy(ret, _tmpl, len); for (p = (char*)ret; p < (char*)ret + len - sizeof(int); p++) if (*(int *)p == TAG) *(int *)p = v; return ret; } int main() { f_int funcs[10]; int i; for (i = 0; i < 10; i++) funcs[i] = dupf(i); for (i = 0; i < 9; i++) printf("func[%d]: %d\n", i, funcs[i]()); return 0; }
Generate an equivalent C# version of this Tcl code.
package require Tcl 8.6; proc closure {script} { set valuemap {} foreach v [uplevel 1 {info vars}] { lappend valuemap [list $v [uplevel 1 [list set $v]]] } set body [list $valuemap $script [uplevel 1 {namespace current}]] return [list apply [list {} [list tailcall apply $body]]] } proc collectFor {var from to body} { upvar 1 $var v set result {} for {set v $from} {$v < $to} {incr v} {lappend result [uplevel 1 $body]} return $result } proc buildList {} { collectFor i 0 10 { closure { return [expr $i*$i] } } } set theClosures [buildList] foreach i {a b c d e} { set idx [expr {int(rand()*9)}]; puts $idx=>[{*}[lindex $theClosures $idx]] }
using System; using System.Linq; class Program { static void Main() { var captor = (Func<int, Func<int>>)(number => () => number * number); var functions = Enumerable.Range(0, 10).Select(captor); foreach (var function in functions.Take(9)) { Console.WriteLine(function()); } } }
Generate an equivalent C# version of this Tcl code.
package require Tcl 8.6; proc closure {script} { set valuemap {} foreach v [uplevel 1 {info vars}] { lappend valuemap [list $v [uplevel 1 [list set $v]]] } set body [list $valuemap $script [uplevel 1 {namespace current}]] return [list apply [list {} [list tailcall apply $body]]] } proc collectFor {var from to body} { upvar 1 $var v set result {} for {set v $from} {$v < $to} {incr v} {lappend result [uplevel 1 $body]} return $result } proc buildList {} { collectFor i 0 10 { closure { return [expr $i*$i] } } } set theClosures [buildList] foreach i {a b c d e} { set idx [expr {int(rand()*9)}]; puts $idx=>[{*}[lindex $theClosures $idx]] }
using System; using System.Linq; class Program { static void Main() { var captor = (Func<int, Func<int>>)(number => () => number * number); var functions = Enumerable.Range(0, 10).Select(captor); foreach (var function in functions.Take(9)) { Console.WriteLine(function()); } } }
Generate a C++ translation of this Tcl snippet without changing its computational steps.
package require Tcl 8.6; proc closure {script} { set valuemap {} foreach v [uplevel 1 {info vars}] { lappend valuemap [list $v [uplevel 1 [list set $v]]] } set body [list $valuemap $script [uplevel 1 {namespace current}]] return [list apply [list {} [list tailcall apply $body]]] } proc collectFor {var from to body} { upvar 1 $var v set result {} for {set v $from} {$v < $to} {incr v} {lappend result [uplevel 1 $body]} return $result } proc buildList {} { collectFor i 0 10 { closure { return [expr $i*$i] } } } set theClosures [buildList] foreach i {a b c d e} { set idx [expr {int(rand()*9)}]; puts $idx=>[{*}[lindex $theClosures $idx]] }
#include <iostream> #include <functional> #include <vector> int main() { std::vector<std::function<int()> > funcs; for (int i = 0; i < 10; i++) funcs.push_back([=]() { return i * i; }); for ( std::function<int( )> f : funcs ) std::cout << f( ) << std::endl ; return 0; }
Convert the following code from Tcl to C++, ensuring the logic remains intact.
package require Tcl 8.6; proc closure {script} { set valuemap {} foreach v [uplevel 1 {info vars}] { lappend valuemap [list $v [uplevel 1 [list set $v]]] } set body [list $valuemap $script [uplevel 1 {namespace current}]] return [list apply [list {} [list tailcall apply $body]]] } proc collectFor {var from to body} { upvar 1 $var v set result {} for {set v $from} {$v < $to} {incr v} {lappend result [uplevel 1 $body]} return $result } proc buildList {} { collectFor i 0 10 { closure { return [expr $i*$i] } } } set theClosures [buildList] foreach i {a b c d e} { set idx [expr {int(rand()*9)}]; puts $idx=>[{*}[lindex $theClosures $idx]] }
#include <iostream> #include <functional> #include <vector> int main() { std::vector<std::function<int()> > funcs; for (int i = 0; i < 10; i++) funcs.push_back([=]() { return i * i; }); for ( std::function<int( )> f : funcs ) std::cout << f( ) << std::endl ; return 0; }
Can you help me rewrite this code in Java instead of Tcl, keeping it the same logically?
package require Tcl 8.6; proc closure {script} { set valuemap {} foreach v [uplevel 1 {info vars}] { lappend valuemap [list $v [uplevel 1 [list set $v]]] } set body [list $valuemap $script [uplevel 1 {namespace current}]] return [list apply [list {} [list tailcall apply $body]]] } proc collectFor {var from to body} { upvar 1 $var v set result {} for {set v $from} {$v < $to} {incr v} {lappend result [uplevel 1 $body]} return $result } proc buildList {} { collectFor i 0 10 { closure { return [expr $i*$i] } } } set theClosures [buildList] foreach i {a b c d e} { set idx [expr {int(rand()*9)}]; puts $idx=>[{*}[lindex $theClosures $idx]] }
import java.util.function.Supplier; import java.util.ArrayList; public class ValueCapture { public static void main(String[] args) { ArrayList<Supplier<Integer>> funcs = new ArrayList<>(); for (int i = 0; i < 10; i++) { int j = i; funcs.add(() -> j * j); } Supplier<Integer> foo = funcs.get(3); System.out.println(foo.get()); } }
Translate the given Tcl code snippet into Java without altering its behavior.
package require Tcl 8.6; proc closure {script} { set valuemap {} foreach v [uplevel 1 {info vars}] { lappend valuemap [list $v [uplevel 1 [list set $v]]] } set body [list $valuemap $script [uplevel 1 {namespace current}]] return [list apply [list {} [list tailcall apply $body]]] } proc collectFor {var from to body} { upvar 1 $var v set result {} for {set v $from} {$v < $to} {incr v} {lappend result [uplevel 1 $body]} return $result } proc buildList {} { collectFor i 0 10 { closure { return [expr $i*$i] } } } set theClosures [buildList] foreach i {a b c d e} { set idx [expr {int(rand()*9)}]; puts $idx=>[{*}[lindex $theClosures $idx]] }
import java.util.function.Supplier; import java.util.ArrayList; public class ValueCapture { public static void main(String[] args) { ArrayList<Supplier<Integer>> funcs = new ArrayList<>(); for (int i = 0; i < 10; i++) { int j = i; funcs.add(() -> j * j); } Supplier<Integer> foo = funcs.get(3); System.out.println(foo.get()); } }
Write the same algorithm in Python as shown in this Tcl implementation.
package require Tcl 8.6; proc closure {script} { set valuemap {} foreach v [uplevel 1 {info vars}] { lappend valuemap [list $v [uplevel 1 [list set $v]]] } set body [list $valuemap $script [uplevel 1 {namespace current}]] return [list apply [list {} [list tailcall apply $body]]] } proc collectFor {var from to body} { upvar 1 $var v set result {} for {set v $from} {$v < $to} {incr v} {lappend result [uplevel 1 $body]} return $result } proc buildList {} { collectFor i 0 10 { closure { return [expr $i*$i] } } } set theClosures [buildList] foreach i {a b c d e} { set idx [expr {int(rand()*9)}]; puts $idx=>[{*}[lindex $theClosures $idx]] }
funcs = [] for i in range(10): funcs.append(lambda: i * i) print funcs[3]()
Convert this Tcl block to Python, preserving its control flow and logic.
package require Tcl 8.6; proc closure {script} { set valuemap {} foreach v [uplevel 1 {info vars}] { lappend valuemap [list $v [uplevel 1 [list set $v]]] } set body [list $valuemap $script [uplevel 1 {namespace current}]] return [list apply [list {} [list tailcall apply $body]]] } proc collectFor {var from to body} { upvar 1 $var v set result {} for {set v $from} {$v < $to} {incr v} {lappend result [uplevel 1 $body]} return $result } proc buildList {} { collectFor i 0 10 { closure { return [expr $i*$i] } } } set theClosures [buildList] foreach i {a b c d e} { set idx [expr {int(rand()*9)}]; puts $idx=>[{*}[lindex $theClosures $idx]] }
funcs = [] for i in range(10): funcs.append(lambda: i * i) print funcs[3]()
Convert this Tcl snippet to Go and keep its semantics consistent.
package require Tcl 8.6; proc closure {script} { set valuemap {} foreach v [uplevel 1 {info vars}] { lappend valuemap [list $v [uplevel 1 [list set $v]]] } set body [list $valuemap $script [uplevel 1 {namespace current}]] return [list apply [list {} [list tailcall apply $body]]] } proc collectFor {var from to body} { upvar 1 $var v set result {} for {set v $from} {$v < $to} {incr v} {lappend result [uplevel 1 $body]} return $result } proc buildList {} { collectFor i 0 10 { closure { return [expr $i*$i] } } } set theClosures [buildList] foreach i {a b c d e} { set idx [expr {int(rand()*9)}]; puts $idx=>[{*}[lindex $theClosures $idx]] }
package main import "fmt" func main() { fs := make([]func() int, 10) for i := range fs { i := i fs[i] = func() int { return i * i } } fmt.Println("func #0:", fs[0]()) fmt.Println("func #3:", fs[3]()) }
Preserve the algorithm and functionality while converting the code from Tcl to Go.
package require Tcl 8.6; proc closure {script} { set valuemap {} foreach v [uplevel 1 {info vars}] { lappend valuemap [list $v [uplevel 1 [list set $v]]] } set body [list $valuemap $script [uplevel 1 {namespace current}]] return [list apply [list {} [list tailcall apply $body]]] } proc collectFor {var from to body} { upvar 1 $var v set result {} for {set v $from} {$v < $to} {incr v} {lappend result [uplevel 1 $body]} return $result } proc buildList {} { collectFor i 0 10 { closure { return [expr $i*$i] } } } set theClosures [buildList] foreach i {a b c d e} { set idx [expr {int(rand()*9)}]; puts $idx=>[{*}[lindex $theClosures $idx]] }
package main import "fmt" func main() { fs := make([]func() int, 10) for i := range fs { i := i fs[i] = func() int { return i * i } } fmt.Println("func #0:", fs[0]()) fmt.Println("func #3:", fs[3]()) }
Write the same algorithm in PHP as shown in this Rust implementation.
fn main() { let fs: Vec<_> = (0..10).map(|i| {move || i*i} ).collect(); println!("7th val: {}", fs[7]()); }
<?php $funcs = array(); for ($i = 0; $i < 10; $i++) { $funcs[] = function () use ($i) { return $i * $i; }; } echo $funcs[3](), "\n"; // prints 9 ?>
Translate the given Rust code snippet into PHP without altering its behavior.
fn main() { let fs: Vec<_> = (0..10).map(|i| {move || i*i} ).collect(); println!("7th val: {}", fs[7]()); }
<?php $funcs = array(); for ($i = 0; $i < 10; $i++) { $funcs[] = function () use ($i) { return $i * $i; }; } echo $funcs[3](), "\n"; // prints 9 ?>
Keep all operations the same but rewrite the snippet in PHP.
with Ada.Text_IO; procedure Value_Capture is protected type Fun is entry Init(Index: Natural); function Result return Natural; private N: Natural := 0; end Fun; protected body Fun is entry Init(Index: Natural) when N=0 is begin N := Index; end Init; function Result return Natural is (N*N); end Fun; A: array (1 .. 10) of Fun; begin for I in A'Range loop A(I).Init(I); end loop; for I in A'First .. A'Last-1 loop Ada.Text_IO.Put(Integer'Image(A(I).Result)); end loop; end Value_Capture;
<?php $funcs = array(); for ($i = 0; $i < 10; $i++) { $funcs[] = function () use ($i) { return $i * $i; }; } echo $funcs[3](), "\n"; // prints 9 ?>
Translate this program into PHP but keep the logic exactly as in Ada.
with Ada.Text_IO; procedure Value_Capture is protected type Fun is entry Init(Index: Natural); function Result return Natural; private N: Natural := 0; end Fun; protected body Fun is entry Init(Index: Natural) when N=0 is begin N := Index; end Init; function Result return Natural is (N*N); end Fun; A: array (1 .. 10) of Fun; begin for I in A'Range loop A(I).Init(I); end loop; for I in A'First .. A'Last-1 loop Ada.Text_IO.Put(Integer'Image(A(I).Result)); end loop; end Value_Capture;
<?php $funcs = array(); for ($i = 0; $i < 10; $i++) { $funcs[] = function () use ($i) { return $i * $i; }; } echo $funcs[3](), "\n"; // prints 9 ?>
Produce a language-to-language conversion: from Clojure to PHP, same semantics.
(def funcs (map #(fn [] (* % %)) (range 11))) (printf "%d\n%d\n" ((nth funcs 3)) ((nth funcs 4)))
<?php $funcs = array(); for ($i = 0; $i < 10; $i++) { $funcs[] = function () use ($i) { return $i * $i; }; } echo $funcs[3](), "\n"; // prints 9 ?>
Produce a language-to-language conversion: from Clojure to PHP, same semantics.
(def funcs (map #(fn [] (* % %)) (range 11))) (printf "%d\n%d\n" ((nth funcs 3)) ((nth funcs 4)))
<?php $funcs = array(); for ($i = 0; $i < 10; $i++) { $funcs[] = function () use ($i) { return $i * $i; }; } echo $funcs[3](), "\n"; // prints 9 ?>
Write a version of this Common_Lisp function in PHP with identical behavior.
CL-USER> (defparameter alist (loop for i from 1 to 10 collect (cons i (let ((i i)) (lambda () (* i i)))))) ALIST CL-USER> (funcall (cdr (assoc 2 alist))) 4 CL-USER> (funcall (cdr (assoc 8 alist))) 64
<?php $funcs = array(); for ($i = 0; $i < 10; $i++) { $funcs[] = function () use ($i) { return $i * $i; }; } echo $funcs[3](), "\n"; // prints 9 ?>
Rewrite this program in PHP while keeping its functionality equivalent to the Common_Lisp version.
CL-USER> (defparameter alist (loop for i from 1 to 10 collect (cons i (let ((i i)) (lambda () (* i i)))))) ALIST CL-USER> (funcall (cdr (assoc 2 alist))) 4 CL-USER> (funcall (cdr (assoc 8 alist))) 64
<?php $funcs = array(); for ($i = 0; $i < 10; $i++) { $funcs[] = function () use ($i) { return $i * $i; }; } echo $funcs[3](), "\n"; // prints 9 ?>
Generate a PHP translation of this D snippet without changing its computational steps.
import std.stdio; void main() { int delegate()[] funcs; foreach (i; 0 .. 10) funcs ~= (i => () => i ^^ 2)(i); writeln(funcs[3]()); }
<?php $funcs = array(); for ($i = 0; $i < 10; $i++) { $funcs[] = function () use ($i) { return $i * $i; }; } echo $funcs[3](), "\n"; // prints 9 ?>
Change the programming language of this snippet from D to PHP without modifying what it does.
import std.stdio; void main() { int delegate()[] funcs; foreach (i; 0 .. 10) funcs ~= (i => () => i ^^ 2)(i); writeln(funcs[3]()); }
<?php $funcs = array(); for ($i = 0; $i < 10; $i++) { $funcs[] = function () use ($i) { return $i * $i; }; } echo $funcs[3](), "\n"; // prints 9 ?>
Rewrite this program in PHP while keeping its functionality equivalent to the Delphi version.
program Project1; type TFuncIntResult = reference to function: Integer; function CreateFunc(i: Integer): TFuncIntResult; begin Result := function: Integer begin Result := i * i; end; end; var Funcs: array[0..9] of TFuncIntResult; i: integer; begin for i := Low(Funcs) to High(Funcs) do Funcs[i] := CreateFunc(i); for i := Low(Funcs) to High(Funcs) do Writeln(Funcs[i]()); end.
<?php $funcs = array(); for ($i = 0; $i < 10; $i++) { $funcs[] = function () use ($i) { return $i * $i; }; } echo $funcs[3](), "\n"; // prints 9 ?>
Rewrite this program in PHP while keeping its functionality equivalent to the Delphi version.
program Project1; type TFuncIntResult = reference to function: Integer; function CreateFunc(i: Integer): TFuncIntResult; begin Result := function: Integer begin Result := i * i; end; end; var Funcs: array[0..9] of TFuncIntResult; i: integer; begin for i := Low(Funcs) to High(Funcs) do Funcs[i] := CreateFunc(i); for i := Low(Funcs) to High(Funcs) do Writeln(Funcs[i]()); end.
<?php $funcs = array(); for ($i = 0; $i < 10; $i++) { $funcs[] = function () use ($i) { return $i * $i; }; } echo $funcs[3](), "\n"; // prints 9 ?>
Translate the given Elixir code snippet into PHP without altering its behavior.
funs = for i <- 0..9, do: (fn -> i*i end) Enum.each(funs, &IO.puts &1.())
<?php $funcs = array(); for ($i = 0; $i < 10; $i++) { $funcs[] = function () use ($i) { return $i * $i; }; } echo $funcs[3](), "\n"; // prints 9 ?>
Produce a functionally identical PHP code for the snippet given in Elixir.
funs = for i <- 0..9, do: (fn -> i*i end) Enum.each(funs, &IO.puts &1.())
<?php $funcs = array(); for ($i = 0; $i < 10; $i++) { $funcs[] = function () use ($i) { return $i * $i; }; } echo $funcs[3](), "\n"; // prints 9 ?>
Write the same algorithm in PHP as shown in this Erlang implementation.
-module(capture_demo). -export([demo/0]). demo() -> Funs = lists:map(fun (X) -> fun () -> X * X end end, lists:seq(1,10)), lists:foreach(fun (F) -> io:fwrite("~B~n",[F()]) end, Funs).
<?php $funcs = array(); for ($i = 0; $i < 10; $i++) { $funcs[] = function () use ($i) { return $i * $i; }; } echo $funcs[3](), "\n"; // prints 9 ?>
Maintain the same structure and functionality when rewriting this code in PHP.
-module(capture_demo). -export([demo/0]). demo() -> Funs = lists:map(fun (X) -> fun () -> X * X end end, lists:seq(1,10)), lists:foreach(fun (F) -> io:fwrite("~B~n",[F()]) end, Funs).
<?php $funcs = array(); for ($i = 0; $i < 10; $i++) { $funcs[] = function () use ($i) { return $i * $i; }; } echo $funcs[3](), "\n"; // prints 9 ?>
Change the following F# code into PHP without altering its purpose.
[<EntryPoint>] let main argv = let fs = List.init 10 (fun i -> fun () -> i*i) do List.iter (fun f -> printfn "%d" <| f()) fs 0
<?php $funcs = array(); for ($i = 0; $i < 10; $i++) { $funcs[] = function () use ($i) { return $i * $i; }; } echo $funcs[3](), "\n"; // prints 9 ?>
Keep all operations the same but rewrite the snippet in PHP.
[<EntryPoint>] let main argv = let fs = List.init 10 (fun i -> fun () -> i*i) do List.iter (fun f -> printfn "%d" <| f()) fs 0
<?php $funcs = array(); for ($i = 0; $i < 10; $i++) { $funcs[] = function () use ($i) { return $i * $i; }; } echo $funcs[3](), "\n"; // prints 9 ?>
Translate this program into PHP but keep the logic exactly as in Factor.
USING: io kernel locals math prettyprint sequences ; [let 10 iota [ :> i [ i i * ] ] map :> seq { 3 8 } [ dup pprint " squared is " write seq nth call . ] each ]
<?php $funcs = array(); for ($i = 0; $i < 10; $i++) { $funcs[] = function () use ($i) { return $i * $i; }; } echo $funcs[3](), "\n"; // prints 9 ?>
Translate this program into PHP but keep the logic exactly as in Factor.
USING: io kernel locals math prettyprint sequences ; [let 10 iota [ :> i [ i i * ] ] map :> seq { 3 8 } [ dup pprint " squared is " write seq nth call . ] each ]
<?php $funcs = array(); for ($i = 0; $i < 10; $i++) { $funcs[] = function () use ($i) { return $i * $i; }; } echo $funcs[3](), "\n"; // prints 9 ?>
Write the same code in PHP as shown below in Forth.
: xt-array here { a } 10 cells allot 10 0 do :noname i ]] literal dup * ; [[ a i cells + ! loop a ; xt-array 5 cells + @ execute .
<?php $funcs = array(); for ($i = 0; $i < 10; $i++) { $funcs[] = function () use ($i) { return $i * $i; }; } echo $funcs[3](), "\n"; // prints 9 ?>
Produce a language-to-language conversion: from Forth to PHP, same semantics.
: xt-array here { a } 10 cells allot 10 0 do :noname i ]] literal dup * ; [[ a i cells + ! loop a ; xt-array 5 cells + @ execute .
<?php $funcs = array(); for ($i = 0; $i < 10; $i++) { $funcs[] = function () use ($i) { return $i * $i; }; } echo $funcs[3](), "\n"; // prints 9 ?>
Change the following Groovy code into PHP without altering its purpose.
def closures = (0..9).collect{ i -> { -> i*i } }
<?php $funcs = array(); for ($i = 0; $i < 10; $i++) { $funcs[] = function () use ($i) { return $i * $i; }; } echo $funcs[3](), "\n"; // prints 9 ?>
Generate a PHP translation of this Groovy snippet without changing its computational steps.
def closures = (0..9).collect{ i -> { -> i*i } }
<?php $funcs = array(); for ($i = 0; $i < 10; $i++) { $funcs[] = function () use ($i) { return $i * $i; }; } echo $funcs[3](), "\n"; // prints 9 ?>
Ensure the translated PHP code behaves exactly like the original Haskell snippet.
fs = map (\i _ -> i * i) [1 .. 10]
<?php $funcs = array(); for ($i = 0; $i < 10; $i++) { $funcs[] = function () use ($i) { return $i * $i; }; } echo $funcs[3](), "\n"; // prints 9 ?>
Write a version of this Haskell function in PHP with identical behavior.
fs = map (\i _ -> i * i) [1 .. 10]
<?php $funcs = array(); for ($i = 0; $i < 10; $i++) { $funcs[] = function () use ($i) { return $i * $i; }; } echo $funcs[3](), "\n"; // prints 9 ?>
Change the following J code into PHP without altering its purpose.
constF=:3 :0 {.''`(y "_) )
<?php $funcs = array(); for ($i = 0; $i < 10; $i++) { $funcs[] = function () use ($i) { return $i * $i; }; } echo $funcs[3](), "\n"; // prints 9 ?>