WeixiangYan/CodeTransOcean huggingface dataset:
Task: First-class functions






PHP: $compose = function ($f, $g) {
    return function ($x) use ($f, $g) {
        return $f($g($x));
    };
};

$fn  = array('sin', 'cos', function ($x) { return pow($x, 3); });
$inv = array('asin', 'acos', function ($x) { return pow($x, 1/3); });

for ($i = 0; $i < 3; $i++) {
    $f = $compose($inv[$i], $fn[$i]);
    echo $f(0.5), PHP_EOL;
}






Ada: with Ada.Float_Text_IO,
     Ada.Integer_Text_IO,
     Ada.Text_IO,
     Ada.Numerics.Elementary_Functions;

procedure First_Class_Functions is
   use Ada.Float_Text_IO,
       Ada.Integer_Text_IO,
       Ada.Text_IO,
       Ada.Numerics.Elementary_Functions;

   function Sqr (X : Float) return Float is
   begin
      return X ** 2;
   end Sqr;

   type A_Function is access function (X : Float) return Float;

   generic
      F, G : A_Function;
   function Compose (X : Float) return Float;

   function Compose (X : Float) return Float is
   begin
      return F (G (X));
   end Compose;

   Functions : array (Positive range <>) of A_Function := (Sin'Access,
                                                           Cos'Access,
                                                           Sqr'Access);
   Inverses  : array (Positive range <>) of A_Function := (Arcsin'Access,
                                                           Arccos'Access,
                                                           Sqrt'Access);
begin
   for I in Functions'Range loop
      declare
         function Identity is new Compose (Functions (I), Inverses (I));
         Test_Value : Float := 0.5;
         Result     : Float;
      begin
         Result := Identity (Test_Value);

         if Result = Test_Value then
            Put      ("Example ");
            Put      (I, Width => 0);
            Put_Line (" is perfect for the given test value.");
         else
            Put      ("Example ");
            Put      (I, Width => 0);
            Put      (" is off by");
            Put      (abs (Result - Test_Value));
            Put_Line (" for the given test value.");
         end if;
      end;
   end loop;
end First_Class_Functions;
Task: Function prototype



C#: using System;
abstract class Printer
{
    public abstract void Print();
}

class PrinterImpl : Printer
{
    public override void Print() {
        Console.WriteLine("Hello world!");
    }
}









Ada: function noargs return Integer;
function twoargs (a, b : Integer) return Integer;

function optionalargs (a, b : Integer := 0) return Integer;

procedure dostuff (a : Integer);
Task: Function prototype

C: int noargs(void); 
int twoargs(int a,int b); 
int twoargs(int ,int); 
int anyargs(); 
int atleastoneargs(int, ...); 











Ada: function noargs return Integer;
function twoargs (a, b : Integer) return Integer;

function optionalargs (a, b : Integer := 0) return Integer;

procedure dostuff (a : Integer);
Task: Function prototype


C++: int noargs(); 
int twoargs(int a,int b); 
int twoargs(int ,int); 
int anyargs(...); 
int atleastoneargs(int, ...); 
template<typename T> T declval(T); 
template<typename ...T> tuple<T...> make_tuple(T...); 










Ada: function noargs return Integer;
function twoargs (a, b : Integer) return Integer;

function optionalargs (a, b : Integer := 0) return Integer;

procedure dostuff (a : Integer);
Task: Function prototype




Go: func a()          
func b(x, y int)  
func c(...int)    








Ada: function noargs return Integer;
function twoargs (a, b : Integer) return Integer;

function optionalargs (a, b : Integer := 0) return Integer;

procedure dostuff (a : Integer);
Task: Function prototype

C: int noargs(void); 
int twoargs(int a,int b); 
int twoargs(int ,int); 
int anyargs(); 
int atleastoneargs(int, ...);
Task: Function prototype



C#: using System;
abstract class Printer
{
    public abstract void Print();
}

class PrinterImpl : Printer
{
    public override void Print() {
        Console.WriteLine("Hello world!");
    }
}
Task: Function prototype


C++: int noargs(); 
int twoargs(int a,int b); 
int twoargs(int ,int); 
int anyargs(...); 
int atleastoneargs(int, ...); 
template<typename T> T declval(T); 
template<typename ...T> tuple<T...> make_tuple(T...);
Task: Function prototype




Go: func a()          
func b(x, y int)  
func c(...int)
Task: Function prototype

C: int noargs(void); 
int twoargs(int a,int b); 
int twoargs(int ,int); 
int anyargs(); 
int atleastoneargs(int, ...); 









COBOL:        
       PROGRAM-ID. no-args PROTOTYPE.
       END PROGRAM no-args.

       
       
       PROGRAM-ID. two-args PROTOTYPE.
       DATA DIVISION.
       LINKAGE SECTION.
       01  arg-1 PIC 9(8).
       01  arg-2 PIC 9(8).
       01  ret   PIC 9(8).
       PROCEDURE DIVISION USING arg-1, arg-2 RETURNING ret.
       END PROGRAM two-args.

       
       
       
       PROGRAM-ID. optional-args PROTOTYPE.
       DATA DIVISION.
       LINKAGE SECTION.
       01  arg-1 PIC 9(8).
       01  arg-2 PIC 9(8).
       PROCEDURE DIVISION USING OPTIONAL arg-1, OPTIONAL arg-2.
       END PROGRAM optional-args.

       

       
       
       PROGRAM-ID. foreign-func PROTOTYPE.
       OPTIONS.
           ENTRY-CONVENTION some-langauge.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01  arg PIC S9(9) USAGE COMP-5.
       01  ret PIC S9(9) USAGE COMP-5.
       PROCEDURE DIVISION USING arg RETURNING ret.
       END PROGRAM foreign-func.
Task: Function prototype



C#: using System;
abstract class Printer
{
    public abstract void Print();
}

class PrinterImpl : Printer
{
    public override void Print() {
        Console.WriteLine("Hello world!");
    }
}







COBOL:        
       PROGRAM-ID. no-args PROTOTYPE.
       END PROGRAM no-args.

       
       
       PROGRAM-ID. two-args PROTOTYPE.
       DATA DIVISION.
       LINKAGE SECTION.
       01  arg-1 PIC 9(8).
       01  arg-2 PIC 9(8).
       01  ret   PIC 9(8).
       PROCEDURE DIVISION USING arg-1, arg-2 RETURNING ret.
       END PROGRAM two-args.

       
       
       
       PROGRAM-ID. optional-args PROTOTYPE.
       DATA DIVISION.
       LINKAGE SECTION.
       01  arg-1 PIC 9(8).
       01  arg-2 PIC 9(8).
       PROCEDURE DIVISION USING OPTIONAL arg-1, OPTIONAL arg-2.
       END PROGRAM optional-args.

       

       
       
       PROGRAM-ID. foreign-func PROTOTYPE.
       OPTIONS.
           ENTRY-CONVENTION some-langauge.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01  arg PIC S9(9) USAGE COMP-5.
       01  ret PIC S9(9) USAGE COMP-5.
       PROCEDURE DIVISION USING arg RETURNING ret.
       END PROGRAM foreign-func.
Task: Function prototype


C++: int noargs(); 
int twoargs(int a,int b); 
int twoargs(int ,int); 
int anyargs(...); 
int atleastoneargs(int, ...); 
template<typename T> T declval(T); 
template<typename ...T> tuple<T...> make_tuple(T...); 








COBOL:        
       PROGRAM-ID. no-args PROTOTYPE.
       END PROGRAM no-args.

       
       
       PROGRAM-ID. two-args PROTOTYPE.
       DATA DIVISION.
       LINKAGE SECTION.
       01  arg-1 PIC 9(8).
       01  arg-2 PIC 9(8).
       01  ret   PIC 9(8).
       PROCEDURE DIVISION USING arg-1, arg-2 RETURNING ret.
       END PROGRAM two-args.

       
       
       
       PROGRAM-ID. optional-args PROTOTYPE.
       DATA DIVISION.
       LINKAGE SECTION.
       01  arg-1 PIC 9(8).
       01  arg-2 PIC 9(8).
       PROCEDURE DIVISION USING OPTIONAL arg-1, OPTIONAL arg-2.
       END PROGRAM optional-args.

       

       
       
       PROGRAM-ID. foreign-func PROTOTYPE.
       OPTIONS.
           ENTRY-CONVENTION some-langauge.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01  arg PIC S9(9) USAGE COMP-5.
       01  ret PIC S9(9) USAGE COMP-5.
       PROCEDURE DIVISION USING arg RETURNING ret.
       END PROGRAM foreign-func.
Task: Function prototype




Go: func a()          
func b(x, y int)  
func c(...int)    






COBOL:        
       PROGRAM-ID. no-args PROTOTYPE.
       END PROGRAM no-args.

       
       
       PROGRAM-ID. two-args PROTOTYPE.
       DATA DIVISION.
       LINKAGE SECTION.
       01  arg-1 PIC 9(8).
       01  arg-2 PIC 9(8).
       01  ret   PIC 9(8).
       PROCEDURE DIVISION USING arg-1, arg-2 RETURNING ret.
       END PROGRAM two-args.

       
       
       
       PROGRAM-ID. optional-args PROTOTYPE.
       DATA DIVISION.
       LINKAGE SECTION.
       01  arg-1 PIC 9(8).
       01  arg-2 PIC 9(8).
       PROCEDURE DIVISION USING OPTIONAL arg-1, OPTIONAL arg-2.
       END PROGRAM optional-args.

       

       
       
       PROGRAM-ID. foreign-func PROTOTYPE.
       OPTIONS.
           ENTRY-CONVENTION some-langauge.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01  arg PIC S9(9) USAGE COMP-5.
       01  ret PIC S9(9) USAGE COMP-5.
       PROCEDURE DIVISION USING arg RETURNING ret.
       END PROGRAM foreign-func.
Task: Test a function



C#: using Microsoft.VisualStudio.TestTools.UnitTesting;
using PalindromeDetector.ConsoleApp;

namespace PalindromeDetector.VisualStudioTests
{
    [TestClass]
    public class VSTests
    {
        [TestMethod]
        public void PalindromeDetectorCanUnderstandPalindrome()
        {
            
            bool expected = true;
            bool actual;
            actual = Program.IsPalindrome("1");
            Assert.AreEqual(expected, actual);
            actual = Program.IsPalindromeNonRecursive("1");
            Assert.AreEqual(expected, actual);
            actual = Program.IsPalindrome("ingirumimusnocteetconsumimurigni");
            Assert.AreEqual(expected, actual);
            actual = Program.IsPalindromeNonRecursive("ingirumimusnocteetconsumimurigni");
            Assert.AreEqual(expected, actual);
        }
        [TestMethod]
        public void PalindromeDetecotryCanUnderstandNonPalindrome()
        {
            bool notExpected = true;
            bool actual = Program.IsPalindrome("ThisIsNotAPalindrome");
            Assert.AreNotEqual(notExpected, actual);
            actual = Program.IsPalindromeNonRecursive("ThisIsNotAPalindrome");
            Assert.AreNotEqual(notExpected, actual);
        }
    }
}









Ada: with Ada.Text_IO;

procedure Test_Function is

   function Palindrome (Text : String) return Boolean is
   begin
      for Offset in 0 .. Text'Length / 2 - 1 loop
         if Text (Text'First + Offset) /= Text (Text'Last - Offset) then
            return False;
         end if;
      end loop;
      return True;
   end Palindrome;

   str1 : String := "racecar";
   str2 : String := "wombat";

begin
   begin
      pragma Assert(False); 
      Ada.Text_IO.Put_Line("Skipping the test! Please compile with assertions switched on!"); 
   exception
      when others => 
         pragma Assert (Palindrome (str1) = True,  "Assertion on str1 failed");
         pragma Assert (Palindrome (str2) = False, "Assertion on str2 failed");
         Ada.Text_IO.Put_Line("Test Passed!");
   end;
end Test_Function;
Task: Test a function

C: #include <assert.h>
int IsPalindrome(char *Str);

int main()
{
    assert(IsPalindrome("racecar"));
    assert(IsPalindrome("alice"));
}











Ada: with Ada.Text_IO;

procedure Test_Function is

   function Palindrome (Text : String) return Boolean is
   begin
      for Offset in 0 .. Text'Length / 2 - 1 loop
         if Text (Text'First + Offset) /= Text (Text'Last - Offset) then
            return False;
         end if;
      end loop;
      return True;
   end Palindrome;

   str1 : String := "racecar";
   str2 : String := "wombat";

begin
   begin
      pragma Assert(False); 
      Ada.Text_IO.Put_Line("Skipping the test! Please compile with assertions switched on!"); 
   exception
      when others => 
         pragma Assert (Palindrome (str1) = True,  "Assertion on str1 failed");
         pragma Assert (Palindrome (str2) = False, "Assertion on str2 failed");
         Ada.Text_IO.Put_Line("Test Passed!");
   end;
end Test_Function;
Task: Test a function


C++: #include <algorithm>
#include <string>

constexpr bool is_palindrome(std::string_view s)
{
  return std::equal(s.begin(), s.begin()+s.length()/2, s.rbegin());
}










Ada: with Ada.Text_IO;

procedure Test_Function is

   function Palindrome (Text : String) return Boolean is
   begin
      for Offset in 0 .. Text'Length / 2 - 1 loop
         if Text (Text'First + Offset) /= Text (Text'Last - Offset) then
            return False;
         end if;
      end loop;
      return True;
   end Palindrome;

   str1 : String := "racecar";
   str2 : String := "wombat";

begin
   begin
      pragma Assert(False); 
      Ada.Text_IO.Put_Line("Skipping the test! Please compile with assertions switched on!"); 
   exception
      when others => 
         pragma Assert (Palindrome (str1) = True,  "Assertion on str1 failed");
         pragma Assert (Palindrome (str2) = False, "Assertion on str2 failed");
         Ada.Text_IO.Put_Line("Test Passed!");
   end;
end Test_Function;
Task: Test a function




Go: package pal

import "testing"

func TestPals(t *testing.T) {
    pals := []string{
        "",
        ".",
        "11",
        "ere",
        "ingirumimusnocteetconsumimurigni",
    }
    for _, s := range pals {
        if !IsPal(s) {
            t.Error("IsPal returned false on palindrome,", s)
        }
    }
}

func TestNonPals(t *testing.T) {
    nps := []string{
        "no",
        "odd",
        "salàlas",
    }
    for _, s := range nps {
        if IsPal(s) {
            t.Error("IsPal returned true on non-palindrome,", s)
        }
    }
}








Ada: with Ada.Text_IO;

procedure Test_Function is

   function Palindrome (Text : String) return Boolean is
   begin
      for Offset in 0 .. Text'Length / 2 - 1 loop
         if Text (Text'First + Offset) /= Text (Text'Last - Offset) then
            return False;
         end if;
      end loop;
      return True;
   end Palindrome;

   str1 : String := "racecar";
   str2 : String := "wombat";

begin
   begin
      pragma Assert(False); 
      Ada.Text_IO.Put_Line("Skipping the test! Please compile with assertions switched on!"); 
   exception
      when others => 
         pragma Assert (Palindrome (str1) = True,  "Assertion on str1 failed");
         pragma Assert (Palindrome (str2) = False, "Assertion on str2 failed");
         Ada.Text_IO.Put_Line("Test Passed!");
   end;
end Test_Function;
Task: Test a function





Java: import static ExampleClass.pali; 
import static ExampleClass.rPali; 
import org.junit.*;
public class PalindromeTest extends junit.framework.TestCase {
    @Before
    public void setUp(){
        
        
    }
    @After
    public void tearDown(){
        
        
    }

    
    @Test
    public void testNonrecursivePali() throws Exception {
        assertTrue(pali("abcba"));
        assertTrue(pali("aa"));
        assertTrue(pali("a"));
        assertTrue(pali(""));
        assertFalse(pali("ab"));
        assertFalse(pali("abcdba"));
    }
    
    @Test
    public void testRecursivePali() throws Exception {
        assertTrue(rPali("abcba"));
        assertTrue(rPali("aa"));
        assertTrue(rPali("a"));
        assertTrue(rPali(""));
        assertFalse(rPali("ab"));
        assertFalse(rPali("abcdba"));
    }

    
    @Test(expected=WhateverException.class)
    public void except(){
        
    }
}







Ada: with Ada.Text_IO;

procedure Test_Function is

   function Palindrome (Text : String) return Boolean is
   begin
      for Offset in 0 .. Text'Length / 2 - 1 loop
         if Text (Text'First + Offset) /= Text (Text'Last - Offset) then
            return False;
         end if;
      end loop;
      return True;
   end Palindrome;

   str1 : String := "racecar";
   str2 : String := "wombat";

begin
   begin
      pragma Assert(False); 
      Ada.Text_IO.Put_Line("Skipping the test! Please compile with assertions switched on!"); 
   exception
      when others => 
         pragma Assert (Palindrome (str1) = True,  "Assertion on str1 failed");
         pragma Assert (Palindrome (str2) = False, "Assertion on str2 failed");
         Ada.Text_IO.Put_Line("Test Passed!");
   end;
end Test_Function;
Task: Test a function







Python: def is_palindrome(s):
    
    return s == s[::-1]

def _test():
    import doctest
    doctest.testmod()
    

if __name__ == "__main__":
    _test()





Ada: with Ada.Text_IO;

procedure Test_Function is

   function Palindrome (Text : String) return Boolean is
   begin
      for Offset in 0 .. Text'Length / 2 - 1 loop
         if Text (Text'First + Offset) /= Text (Text'Last - Offset) then
            return False;
         end if;
      end loop;
      return True;
   end Palindrome;

   str1 : String := "racecar";
   str2 : String := "wombat";

begin
   begin
      pragma Assert(False); 
      Ada.Text_IO.Put_Line("Skipping the test! Please compile with assertions switched on!"); 
   exception
      when others => 
         pragma Assert (Palindrome (str1) = True,  "Assertion on str1 failed");
         pragma Assert (Palindrome (str2) = False, "Assertion on str2 failed");
         Ada.Text_IO.Put_Line("Test Passed!");
   end;
end Test_Function;
Task: Test a function








VB: Option Explicit

Sub Test_a_function()
Dim a, i&
    a = Array("abba", "mom", "dennis sinned", "Un roc lamina l animal cornu", "palindrome", "ba _ ab", "racecars", "racecar", "wombat", "in girum imus nocte et consumimur igni")
    For i = 0 To UBound(a)
        Debug.Print a(i) & " is a palidrome ? " & IsPalindrome(CStr(a(i)))
    Next
End Sub

Function IsPalindrome(txt As String) As Boolean
Dim tempTxt As String
    tempTxt = LCase(Replace(txt, " ", ""))
    IsPalindrome = (tempTxt = StrReverse(tempTxt))
End Function




Ada: with Ada.Text_IO;

procedure Test_Function is

   function Palindrome (Text : String) return Boolean is
   begin
      for Offset in 0 .. Text'Length / 2 - 1 loop
         if Text (Text'First + Offset) /= Text (Text'Last - Offset) then
            return False;
         end if;
      end loop;
      return True;
   end Palindrome;

   str1 : String := "racecar";
   str2 : String := "wombat";

begin
   begin
      pragma Assert(False); 
      Ada.Text_IO.Put_Line("Skipping the test! Please compile with assertions switched on!"); 
   exception
      when others => 
         pragma Assert (Palindrome (str1) = True,  "Assertion on str1 failed");
         pragma Assert (Palindrome (str2) = False, "Assertion on str2 failed");
         Ada.Text_IO.Put_Line("Test Passed!");
   end;
end Test_Function;
Task: Test a function

C: #include <assert.h>
int IsPalindrome(char *Str);

int main()
{
    assert(IsPalindrome("racecar"));
    assert(IsPalindrome("alice"));
}
Task: Test a function



C#: using Microsoft.VisualStudio.TestTools.UnitTesting;
using PalindromeDetector.ConsoleApp;

namespace PalindromeDetector.VisualStudioTests
{
    [TestClass]
    public class VSTests
    {
        [TestMethod]
        public void PalindromeDetectorCanUnderstandPalindrome()
        {
            
            bool expected = true;
            bool actual;
            actual = Program.IsPalindrome("1");
            Assert.AreEqual(expected, actual);
            actual = Program.IsPalindromeNonRecursive("1");
            Assert.AreEqual(expected, actual);
            actual = Program.IsPalindrome("ingirumimusnocteetconsumimurigni");
            Assert.AreEqual(expected, actual);
            actual = Program.IsPalindromeNonRecursive("ingirumimusnocteetconsumimurigni");
            Assert.AreEqual(expected, actual);
        }
        [TestMethod]
        public void PalindromeDetecotryCanUnderstandNonPalindrome()
        {
            bool notExpected = true;
            bool actual = Program.IsPalindrome("ThisIsNotAPalindrome");
            Assert.AreNotEqual(notExpected, actual);
            actual = Program.IsPalindromeNonRecursive("ThisIsNotAPalindrome");
            Assert.AreNotEqual(notExpected, actual);
        }
    }
}
Task: Test a function


C++: #include <algorithm>
#include <string>

constexpr bool is_palindrome(std::string_view s)
{
  return std::equal(s.begin(), s.begin()+s.length()/2, s.rbegin());
}
Task: Test a function





Java: import static ExampleClass.pali; 
import static ExampleClass.rPali; 
import org.junit.*;
public class PalindromeTest extends junit.framework.TestCase {
    @Before
    public void setUp(){
        
        
    }
    @After
    public void tearDown(){
        
        
    }

    
    @Test
    public void testNonrecursivePali() throws Exception {
        assertTrue(pali("abcba"));
        assertTrue(pali("aa"));
        assertTrue(pali("a"));
        assertTrue(pali(""));
        assertFalse(pali("ab"));
        assertFalse(pali("abcdba"));
    }
    
    @Test
    public void testRecursivePali() throws Exception {
        assertTrue(rPali("abcba"));
        assertTrue(rPali("aa"));
        assertTrue(rPali("a"));
        assertTrue(rPali(""));
        assertFalse(rPali("ab"));
        assertFalse(rPali("abcdba"));
    }

    
    @Test(expected=WhateverException.class)
    public void except(){
        
    }
}
Task: Test a function







Python: def is_palindrome(s):
    
    return s == s[::-1]

def _test():
    import doctest
    doctest.testmod()
    

if __name__ == "__main__":
    _test()
Task: Test a function








VB: Option Explicit

Sub Test_a_function()
Dim a, i&
    a = Array("abba", "mom", "dennis sinned", "Un roc lamina l animal cornu", "palindrome", "ba _ ab", "racecars", "racecar", "wombat", "in girum imus nocte et consumimur igni")
    For i = 0 To UBound(a)
        Debug.Print a(i) & " is a palidrome ? " & IsPalindrome(CStr(a(i)))
    Next
End Sub

Function IsPalindrome(txt As String) As Boolean
Dim tempTxt As String
    tempTxt = LCase(Replace(txt, " ", ""))
    IsPalindrome = (tempTxt = StrReverse(tempTxt))
End Function
Task: Test a function




Go: package pal

import "testing"

func TestPals(t *testing.T) {
    pals := []string{
        "",
        ".",
        "11",
        "ere",
        "ingirumimusnocteetconsumimurigni",
    }
    for _, s := range pals {
        if !IsPal(s) {
            t.Error("IsPal returned false on palindrome,", s)
        }
    }
}

func TestNonPals(t *testing.T) {
    nps := []string{
        "no",
        "odd",
        "salàlas",
    }
    for _, s := range nps {
        if IsPal(s) {
            t.Error("IsPal returned true on non-palindrome,", s)
        }
    }
}