WeixiangYan/CodeTransOcean huggingface dataset:
Task: Currying




Go: package main

import (
        "fmt"
        "math"
)

func PowN(b float64) func(float64) float64 {
        return func(e float64) float64 { return math.Pow(b, e) }
}

func PowE(e float64) func(float64) float64 {
        return func(b float64) float64 { return math.Pow(b, e) }
}

type Foo int

func (f Foo) Method(b int) int {
        return int(f) + b
}

func main() {
        pow2 := PowN(2)
        cube := PowE(3)

        fmt.Println("2^8 =", pow2(8))
        fmt.Println("4³ =", cube(4))

        var a Foo = 2
        fn1 := a.Method   
        fn2 := Foo.Method 

        fmt.Println("2 + 2 =", a.Method(2)) 
        fmt.Println("2 + 3 =", fn1(3))
        fmt.Println("2 + 4 =", fn2(a, 4))
        fmt.Println("3 + 5 =", fn2(Foo(3), 5))
}
Task: Currying






PHP: <?php

function curry($callable)
{
    if (_number_of_required_params($callable) === 0) {
        return _make_function($callable);
    }
    if (_number_of_required_params($callable) === 1) {
        return _curry_array_args($callable, _rest(func_get_args()));
    }
    return _curry_array_args($callable, _rest(func_get_args()));
}

function _curry_array_args($callable, $args, $left = true)
{
    return function () use ($callable, $args, $left) {
        if (_is_fullfilled($callable, $args)) {
            return _execute($callable, $args, $left);
        }
        $newArgs = array_merge($args, func_get_args());
        if (_is_fullfilled($callable, $newArgs)) {
            return _execute($callable, $newArgs, $left);
        }
        return _curry_array_args($callable, $newArgs, $left);
    };
}

function _number_of_required_params($callable)
{
    if (is_array($callable)) {
        $refl = new \ReflectionClass($callable[0]);
        $method = $refl->getMethod($callable[1]);
        return $method->getNumberOfRequiredParameters();
    }
    $refl = new \ReflectionFunction($callable);
    return $refl->getNumberOfRequiredParameters();
}

function _make_function($callable)
{
    if (is_array($callable))
        return function() use($callable) {
            return call_user_func_array($callable, func_get_args());
        };
    return $callable;
}

function _execute($callable, $args, $left)
{
    if (! $left) {
        $args = array_reverse($args);
    }
    $placeholders = _placeholder_positions($args);
    if (0 < count($placeholders)) {
        $n = _number_of_required_params($callable);
        if ($n <= _last($placeholders[count($placeholders) - 1])) {
            throw new \Exception('Argument Placeholder found on unexpected position!');
        }
        foreach ($placeholders as $i) {
            $args[$i] = $args[$n];
            array_splice($args, $n, 1);
        }
    }
    return call_user_func_array($callable, $args);
}

function _placeholder_positions($args)
{
    return array_keys(array_filter($args, '_is_placeholder'));
}

function _is_fullfilled($callable, $args)
{
    $args = array_filter($args, function($arg) {
        return ! _is_placeholder($arg);
    });
    return count($args) >= _number_of_required_params($callable);
}

function _is_placeholder($arg)
{
    return $arg instanceof Placeholder;
}

function _rest(array $args)
{
    return array_slice($args, 1);
}

function product($a, $b)
{
    return $a * $b;
}

echo json_encode(array_map(curry('product', 7), [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]));
Task: Fork



C#: using System;
using System.Threading;

namespace Fork {
    class Program {
        static void Fork() {
            Console.WriteLine("Spawned Thread");
        }

        static void Main(string[] args) {
            Thread t = new Thread(new ThreadStart(Fork));
            t.Start();

            Console.WriteLine("Main Thread");
            t.Join();

            Console.ReadLine();
        }
    }
}









Ada: with Ada.Text_IO,
     POSIX.Process_Identification,
     POSIX.Unsafe_Process_Primitives;

procedure Fork is
   use Ada.Text_IO,
       POSIX.Process_Identification,
       POSIX.Unsafe_Process_Primitives;
begin
   if Fork = Null_Process_ID then
      Put_Line ("This is the new process.");
   else
      Put_Line ("This is the original process.");
   end if;
exception
   when others =>
      Put_Line ("Something went wrong.");
end Fork;
Task: Fork

C: #include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <err.h>

int main()
{
	pid_t pid;

	if (!(pid = fork())) {
		usleep(10000);
		printf("\tchild process: done\n");
	} else if (pid < 0) {
		err(1, "fork error");
	} else {
		printf("waiting for child %d...\n", (int)pid);
		printf("child %d finished\n", (int)wait(0));
	}

	return 0;
}











Ada: with Ada.Text_IO,
     POSIX.Process_Identification,
     POSIX.Unsafe_Process_Primitives;

procedure Fork is
   use Ada.Text_IO,
       POSIX.Process_Identification,
       POSIX.Unsafe_Process_Primitives;
begin
   if Fork = Null_Process_ID then
      Put_Line ("This is the new process.");
   else
      Put_Line ("This is the original process.");
   end if;
exception
   when others =>
      Put_Line ("Something went wrong.");
end Fork;
Task: Fork


C++: #include<iostream>
#include<unistd.h>

int main()
{
  pid_t pid = fork();

  if (pid == 0)
  {
    std::cout << "This is the new process\n";
  }
  else if (pid > 0)
  {
    std::cout << "This is the original process\n";
  }
  else
  {
    std::cerr << "ERROR: Something went wrong\n";
  }

  return 0;
}










Ada: with Ada.Text_IO,
     POSIX.Process_Identification,
     POSIX.Unsafe_Process_Primitives;

procedure Fork is
   use Ada.Text_IO,
       POSIX.Process_Identification,
       POSIX.Unsafe_Process_Primitives;
begin
   if Fork = Null_Process_ID then
      Put_Line ("This is the new process.");
   else
      Put_Line ("This is the original process.");
   end if;
exception
   when others =>
      Put_Line ("Something went wrong.");
end Fork;
Task: Fork




Go: package main

import (
    "fmt"
    "os"
)

func main() {
    fmt.Printf("PID: %v\n", os.Getpid())
    if len(os.Args) < 2 {
        fmt.Println("Done.")
        return
    }
    cp, err := os.StartProcess(os.Args[0], nil,
        &os.ProcAttr{Files: []*os.File{nil, os.Stdout}},
    )
    if err != nil {
        fmt.Println(err)
    }
    
    
    fmt.Printf("Child's PID: %v\n", cp.Pid)
    if _, err = cp.Wait(); err != nil {
        fmt.Println(err)
    }
}








Ada: with Ada.Text_IO,
     POSIX.Process_Identification,
     POSIX.Unsafe_Process_Primitives;

procedure Fork is
   use Ada.Text_IO,
       POSIX.Process_Identification,
       POSIX.Unsafe_Process_Primitives;
begin
   if Fork = Null_Process_ID then
      Put_Line ("This is the new process.");
   else
      Put_Line ("This is the original process.");
   end if;
exception
   when others =>
      Put_Line ("Something went wrong.");
end Fork;
Task: Fork





Java: import java.io.IOException;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

public class RFork {

  public static void main(String[] args) {
    ProcessBuilder pb;
    Process pp;
    List<String> command;
    Map<String, String> env;
    BufferedReader ir;
    String currentuser;
    String line;
    try {
      command = Arrays.asList("");
      pb = new ProcessBuilder(command);
      env = pb.environment();
      currentuser = env.get("USER");
      command = Arrays.asList("ps", "-f", "-U", currentuser);
      pb.command(command);
      pp = pb.start();
      ir = new BufferedReader(new InputStreamReader(pp.getInputStream()));
      line = "Output of running " + command.toString() + " is:";
      do {
        System.out.println(line);
      } while ((line = ir.readLine()) != null);
    }
    catch (IOException iox) {
      iox.printStackTrace();
    }

    return;
  }
}







Ada: with Ada.Text_IO,
     POSIX.Process_Identification,
     POSIX.Unsafe_Process_Primitives;

procedure Fork is
   use Ada.Text_IO,
       POSIX.Process_Identification,
       POSIX.Unsafe_Process_Primitives;
begin
   if Fork = Null_Process_ID then
      Put_Line ("This is the new process.");
   else
      Put_Line ("This is the original process.");
   end if;
exception
   when others =>
      Put_Line ("Something went wrong.");
end Fork;
Task: Fork







Python: import os

pid = os.fork()
if pid > 0:
 
else:
 





Ada: with Ada.Text_IO,
     POSIX.Process_Identification,
     POSIX.Unsafe_Process_Primitives;

procedure Fork is
   use Ada.Text_IO,
       POSIX.Process_Identification,
       POSIX.Unsafe_Process_Primitives;
begin
   if Fork = Null_Process_ID then
      Put_Line ("This is the new process.");
   else
      Put_Line ("This is the original process.");
   end if;
exception
   when others =>
      Put_Line ("Something went wrong.");
end Fork;
Task: Fork








VB: Module Module1

    Sub Fork()
        Console.WriteLine("Spawned Thread")
    End Sub

    Sub Main()
        Dim t As New System.Threading.Thread(New Threading.ThreadStart(AddressOf Fork))
        t.Start()

        Console.WriteLine("Main Thread")
        t.Join()
    End Sub

End Module




Ada: with Ada.Text_IO,
     POSIX.Process_Identification,
     POSIX.Unsafe_Process_Primitives;

procedure Fork is
   use Ada.Text_IO,
       POSIX.Process_Identification,
       POSIX.Unsafe_Process_Primitives;
begin
   if Fork = Null_Process_ID then
      Put_Line ("This is the new process.");
   else
      Put_Line ("This is the original process.");
   end if;
exception
   when others =>
      Put_Line ("Something went wrong.");
end Fork;
Task: Fork

C: #include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <err.h>

int main()
{
	pid_t pid;

	if (!(pid = fork())) {
		usleep(10000);
		printf("\tchild process: done\n");
	} else if (pid < 0) {
		err(1, "fork error");
	} else {
		printf("waiting for child %d...\n", (int)pid);
		printf("child %d finished\n", (int)wait(0));
	}

	return 0;
}
Task: Fork



C#: using System;
using System.Threading;

namespace Fork {
    class Program {
        static void Fork() {
            Console.WriteLine("Spawned Thread");
        }

        static void Main(string[] args) {
            Thread t = new Thread(new ThreadStart(Fork));
            t.Start();

            Console.WriteLine("Main Thread");
            t.Join();

            Console.ReadLine();
        }
    }
}
Task: Fork


C++: #include<iostream>
#include<unistd.h>

int main()
{
  pid_t pid = fork();

  if (pid == 0)
  {
    std::cout << "This is the new process\n";
  }
  else if (pid > 0)
  {
    std::cout << "This is the original process\n";
  }
  else
  {
    std::cerr << "ERROR: Something went wrong\n";
  }

  return 0;
}
Task: Fork





Java: import java.io.IOException;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

public class RFork {

  public static void main(String[] args) {
    ProcessBuilder pb;
    Process pp;
    List<String> command;
    Map<String, String> env;
    BufferedReader ir;
    String currentuser;
    String line;
    try {
      command = Arrays.asList("");
      pb = new ProcessBuilder(command);
      env = pb.environment();
      currentuser = env.get("USER");
      command = Arrays.asList("ps", "-f", "-U", currentuser);
      pb.command(command);
      pp = pb.start();
      ir = new BufferedReader(new InputStreamReader(pp.getInputStream()));
      line = "Output of running " + command.toString() + " is:";
      do {
        System.out.println(line);
      } while ((line = ir.readLine()) != null);
    }
    catch (IOException iox) {
      iox.printStackTrace();
    }

    return;
  }
}
Task: Fork







Python: import os

pid = os.fork()
if pid > 0:
 
else:
Task: Fork








VB: Module Module1

    Sub Fork()
        Console.WriteLine("Spawned Thread")
    End Sub

    Sub Main()
        Dim t As New System.Threading.Thread(New Threading.ThreadStart(AddressOf Fork))
        t.Start()

        Console.WriteLine("Main Thread")
        t.Join()
    End Sub

End Module
Task: Fork




Go: package main

import (
    "fmt"
    "os"
)

func main() {
    fmt.Printf("PID: %v\n", os.Getpid())
    if len(os.Args) < 2 {
        fmt.Println("Done.")
        return
    }
    cp, err := os.StartProcess(os.Args[0], nil,
        &os.ProcAttr{Files: []*os.File{nil, os.Stdout}},
    )
    if err != nil {
        fmt.Println(err)
    }
    
    
    fmt.Printf("Child's PID: %v\n", cp.Pid)
    if _, err = cp.Wait(); err != nil {
        fmt.Println(err)
    }
}
Task: Fork

C: #include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <err.h>

int main()
{
	pid_t pid;

	if (!(pid = fork())) {
		usleep(10000);
		printf("\tchild process: done\n");
	} else if (pid < 0) {
		err(1, "fork error");
	} else {
		printf("waiting for child %d...\n", (int)pid);
		printf("child %d finished\n", (int)wait(0));
	}

	return 0;
}









COBOL:        identification division.
       program-id. forking.

       data division.
       working-storage section.
       01 pid usage binary-long.

       procedure division.
       display "attempting fork"

       call "fork" returning pid
           on exception
               display "error: no fork linkage" upon syserr
       end-call

       evaluate pid
          when = 0
              display "    child sleeps"
              call "C$SLEEP" using 3
              display "    child task complete"
          when < 0
              display "error: fork result not ok" upon syserr
          when > 0
              display "parent waits for child..."
              call "wait" using by value 0
              display "parental responsibilities fulfilled"
       end-evaluate

       goback.
       end program forking.
Task: Fork



C#: using System;
using System.Threading;

namespace Fork {
    class Program {
        static void Fork() {
            Console.WriteLine("Spawned Thread");
        }

        static void Main(string[] args) {
            Thread t = new Thread(new ThreadStart(Fork));
            t.Start();

            Console.WriteLine("Main Thread");
            t.Join();

            Console.ReadLine();
        }
    }
}







COBOL:        identification division.
       program-id. forking.

       data division.
       working-storage section.
       01 pid usage binary-long.

       procedure division.
       display "attempting fork"

       call "fork" returning pid
           on exception
               display "error: no fork linkage" upon syserr
       end-call

       evaluate pid
          when = 0
              display "    child sleeps"
              call "C$SLEEP" using 3
              display "    child task complete"
          when < 0
              display "error: fork result not ok" upon syserr
          when > 0
              display "parent waits for child..."
              call "wait" using by value 0
              display "parental responsibilities fulfilled"
       end-evaluate

       goback.
       end program forking.
Task: Fork


C++: #include<iostream>
#include<unistd.h>

int main()
{
  pid_t pid = fork();

  if (pid == 0)
  {
    std::cout << "This is the new process\n";
  }
  else if (pid > 0)
  {
    std::cout << "This is the original process\n";
  }
  else
  {
    std::cerr << "ERROR: Something went wrong\n";
  }

  return 0;
}








COBOL:        identification division.
       program-id. forking.

       data division.
       working-storage section.
       01 pid usage binary-long.

       procedure division.
       display "attempting fork"

       call "fork" returning pid
           on exception
               display "error: no fork linkage" upon syserr
       end-call

       evaluate pid
          when = 0
              display "    child sleeps"
              call "C$SLEEP" using 3
              display "    child task complete"
          when < 0
              display "error: fork result not ok" upon syserr
          when > 0
              display "parent waits for child..."
              call "wait" using by value 0
              display "parental responsibilities fulfilled"
       end-evaluate

       goback.
       end program forking.
Task: Fork





Java: import java.io.IOException;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

public class RFork {

  public static void main(String[] args) {
    ProcessBuilder pb;
    Process pp;
    List<String> command;
    Map<String, String> env;
    BufferedReader ir;
    String currentuser;
    String line;
    try {
      command = Arrays.asList("");
      pb = new ProcessBuilder(command);
      env = pb.environment();
      currentuser = env.get("USER");
      command = Arrays.asList("ps", "-f", "-U", currentuser);
      pb.command(command);
      pp = pb.start();
      ir = new BufferedReader(new InputStreamReader(pp.getInputStream()));
      line = "Output of running " + command.toString() + " is:";
      do {
        System.out.println(line);
      } while ((line = ir.readLine()) != null);
    }
    catch (IOException iox) {
      iox.printStackTrace();
    }

    return;
  }
}





COBOL:        identification division.
       program-id. forking.

       data division.
       working-storage section.
       01 pid usage binary-long.

       procedure division.
       display "attempting fork"

       call "fork" returning pid
           on exception
               display "error: no fork linkage" upon syserr
       end-call

       evaluate pid
          when = 0
              display "    child sleeps"
              call "C$SLEEP" using 3
              display "    child task complete"
          when < 0
              display "error: fork result not ok" upon syserr
          when > 0
              display "parent waits for child..."
              call "wait" using by value 0
              display "parental responsibilities fulfilled"
       end-evaluate

       goback.
       end program forking.
Task: Fork







Python: import os

pid = os.fork()
if pid > 0:
 
else:
 



COBOL:        identification division.
       program-id. forking.

       data division.
       working-storage section.
       01 pid usage binary-long.

       procedure division.
       display "attempting fork"

       call "fork" returning pid
           on exception
               display "error: no fork linkage" upon syserr
       end-call

       evaluate pid
          when = 0
              display "    child sleeps"
              call "C$SLEEP" using 3
              display "    child task complete"
          when < 0
              display "error: fork result not ok" upon syserr
          when > 0
              display "parent waits for child..."
              call "wait" using by value 0
              display "parental responsibilities fulfilled"
       end-evaluate

       goback.
       end program forking.
Task: Fork








VB: Module Module1

    Sub Fork()
        Console.WriteLine("Spawned Thread")
    End Sub

    Sub Main()
        Dim t As New System.Threading.Thread(New Threading.ThreadStart(AddressOf Fork))
        t.Start()

        Console.WriteLine("Main Thread")
        t.Join()
    End Sub

End Module


COBOL:        identification division.
       program-id. forking.

       data division.
       working-storage section.
       01 pid usage binary-long.

       procedure division.
       display "attempting fork"

       call "fork" returning pid
           on exception
               display "error: no fork linkage" upon syserr
       end-call

       evaluate pid
          when = 0
              display "    child sleeps"
              call "C$SLEEP" using 3
              display "    child task complete"
          when < 0
              display "error: fork result not ok" upon syserr
          when > 0
              display "parent waits for child..."
              call "wait" using by value 0
              display "parental responsibilities fulfilled"
       end-evaluate

       goback.
       end program forking.
Task: Fork




Go: package main

import (
    "fmt"
    "os"
)

func main() {
    fmt.Printf("PID: %v\n", os.Getpid())
    if len(os.Args) < 2 {
        fmt.Println("Done.")
        return
    }
    cp, err := os.StartProcess(os.Args[0], nil,
        &os.ProcAttr{Files: []*os.File{nil, os.Stdout}},
    )
    if err != nil {
        fmt.Println(err)
    }
    
    
    fmt.Printf("Child's PID: %v\n", cp.Pid)
    if _, err = cp.Wait(); err != nil {
        fmt.Println(err)
    }
}






COBOL:        identification division.
       program-id. forking.

       data division.
       working-storage section.
       01 pid usage binary-long.

       procedure division.
       display "attempting fork"

       call "fork" returning pid
           on exception
               display "error: no fork linkage" upon syserr
       end-call

       evaluate pid
          when = 0
              display "    child sleeps"
              call "C$SLEEP" using 3
              display "    child task complete"
          when < 0
              display "error: fork result not ok" upon syserr
          when > 0
              display "parent waits for child..."
              call "wait" using by value 0
              display "parental responsibilities fulfilled"
       end-evaluate

       goback.
       end program forking.
Task: Fork






PHP: <?php
$pid = pcntl_fork();
if ($pid == 0)
  echo "This is the new process\n";
else if ($pid > 0)
  echo "This is the original process\n";
else
  echo "ERROR: Something went wrong\n";
?>
Task: Fork






PHP: <?php
$pid = pcntl_fork();
if ($pid == 0)
  echo "This is the new process\n";
else if ($pid > 0)
  echo "This is the original process\n";
else
  echo "ERROR: Something went wrong\n";
?>






Ada: with Ada.Text_IO,
     POSIX.Process_Identification,
     POSIX.Unsafe_Process_Primitives;

procedure Fork is
   use Ada.Text_IO,
       POSIX.Process_Identification,
       POSIX.Unsafe_Process_Primitives;
begin
   if Fork = Null_Process_ID then
      Put_Line ("This is the new process.");
   else
      Put_Line ("This is the original process.");
   end if;
exception
   when others =>
      Put_Line ("Something went wrong.");
end Fork;
Task: Fork






PHP: <?php
$pid = pcntl_fork();
if ($pid == 0)
  echo "This is the new process\n";
else if ($pid > 0)
  echo "This is the original process\n";
else
  echo "ERROR: Something went wrong\n";
?>




COBOL:        identification division.
       program-id. forking.

       data division.
       working-storage section.
       01 pid usage binary-long.

       procedure division.
       display "attempting fork"

       call "fork" returning pid
           on exception
               display "error: no fork linkage" upon syserr
       end-call

       evaluate pid
          when = 0
              display "    child sleeps"
              call "C$SLEEP" using 3
              display "    child task complete"
          when < 0
              display "error: fork result not ok" upon syserr
          when > 0
              display "parent waits for child..."
              call "wait" using by value 0
              display "parental responsibilities fulfilled"
       end-evaluate

       goback.
       end program forking.