WeixiangYan/CodeTransOcean huggingface dataset:
Task: Higher-order functions





Java: public class NewClass {
   
   public NewClass() {
       first(new AnEventOrCallback() {
           public void call() {
               second();
           }
       });
   }
   
   public void first(AnEventOrCallback obj) {
       obj.call();
   }
   
   public void second() {
       System.out.println("Second");
   }
   
   public static void main(String[] args) {
       new NewClass();
   }
}

interface AnEventOrCallback {
   public void call();
}







Ada: with Ada.Text_Io; use Ada.Text_Io;

procedure Subprogram_As_Argument is
   type Proc_Access is access procedure;
   
   procedure Second is
   begin
      Put_Line("Second Procedure");
   end Second;
  
   procedure First(Proc : Proc_Access) is
   begin
      Proc.all;
   end First;
begin
   First(Second'Access);
end Subprogram_As_Argument;
Task: Higher-order functions







Python: def first(function):
    return function()

def second():
    return "second"

result = first(second)





Ada: with Ada.Text_Io; use Ada.Text_Io;

procedure Subprogram_As_Argument is
   type Proc_Access is access procedure;
   
   procedure Second is
   begin
      Put_Line("Second Procedure");
   end Second;
  
   procedure First(Proc : Proc_Access) is
   begin
      Proc.all;
   end First;
begin
   First(Second'Access);
end Subprogram_As_Argument;
Task: Higher-order functions








VB: 
Delegate Function Func2(a As Integer, b As Integer) As Integer

Module Program
    Function Add(a As Integer, b As Integer) As Integer
        Return a + b
    End Function

    Function Mul(a As Integer, b As Integer) As Integer
        Return a * b
    End Function

    Function Div(a As Integer, b As Integer) As Integer
        Return a \ b
    End Function

    
    Function [Call](f As Func2, a As Integer, b As Integer) As Integer
        Return f(a, b)
    End Function

    Sub Main()
        Dim a = 6
        Dim b = 2

        
        Dim add As Func2 = New Func2(AddressOf Program.Add)

        
        Dim div As New Func2(AddressOf Program.Div)

        
        Dim mul As Func2 = AddressOf Program.Mul

        Console.WriteLine("f=Add, f({0}, {1}) = {2}", a, b, [Call](add, a, b))
        Console.WriteLine("f=Mul, f({0}, {1}) = {2}", a, b, [Call](mul, a, b))
        Console.WriteLine("f=Div, f({0}, {1}) = {2}", a, b, [Call](div, a, b))
    End Sub
End Module




Ada: with Ada.Text_Io; use Ada.Text_Io;

procedure Subprogram_As_Argument is
   type Proc_Access is access procedure;
   
   procedure Second is
   begin
      Put_Line("Second Procedure");
   end Second;
  
   procedure First(Proc : Proc_Access) is
   begin
      Proc.all;
   end First;
begin
   First(Second'Access);
end Subprogram_As_Argument;
Task: Higher-order functions

C: void myFuncSimple( void (*funcParameter)(void) )
{
    
   
    (*funcParameter)();  
    funcParameter();     

    
}
Task: Higher-order functions



C#: using System;


delegate int Func2(int a, int b);

class Program
{
    static int Add(int a, int b)
    {
        return a + b;
    }
    
    static int Mul(int a, int b)
    {
        return a * b;
    }
    
    static int Div(int a, int b)
    {
        return a / b;
    }
    
    static int Call(Func2 f, int a, int b)
    {
        
        return f(a, b);
    }

    static void Main()
    {
        int a = 6;
        int b = 2;

        
        Func2 add = new Func2(Add);
        Func2 mul = new Func2(Mul);
        Func2 div = new Func2(Div);
        
        Console.WriteLine("f=Add, f({0}, {1}) = {2}", a, b, Call(add, a, b));
        Console.WriteLine("f=Mul, f({0}, {1}) = {2}", a, b, Call(mul, a, b));
        Console.WriteLine("f=Div, f({0}, {1}) = {2}", a, b, Call(div, a, b));
    }
}
Task: Higher-order functions


C++: 
#include <tr1/functional>
#include <iostream>

using namespace std;
using namespace std::tr1;

void first(function<void()> f)
{
  f();
}

void second()
{
  cout << "second\n";
}

int main()
{
  first(second);
}
Task: Higher-order functions





Java: public class NewClass {
   
   public NewClass() {
       first(new AnEventOrCallback() {
           public void call() {
               second();
           }
       });
   }
   
   public void first(AnEventOrCallback obj) {
       obj.call();
   }
   
   public void second() {
       System.out.println("Second");
   }
   
   public static void main(String[] args) {
       new NewClass();
   }
}

interface AnEventOrCallback {
   public void call();
}
Task: Higher-order functions







Python: def first(function):
    return function()

def second():
    return "second"

result = first(second)
Task: Higher-order functions








VB: 
Delegate Function Func2(a As Integer, b As Integer) As Integer

Module Program
    Function Add(a As Integer, b As Integer) As Integer
        Return a + b
    End Function

    Function Mul(a As Integer, b As Integer) As Integer
        Return a * b
    End Function

    Function Div(a As Integer, b As Integer) As Integer
        Return a \ b
    End Function

    
    Function [Call](f As Func2, a As Integer, b As Integer) As Integer
        Return f(a, b)
    End Function

    Sub Main()
        Dim a = 6
        Dim b = 2

        
        Dim add As Func2 = New Func2(AddressOf Program.Add)

        
        Dim div As New Func2(AddressOf Program.Div)

        
        Dim mul As Func2 = AddressOf Program.Mul

        Console.WriteLine("f=Add, f({0}, {1}) = {2}", a, b, [Call](add, a, b))
        Console.WriteLine("f=Mul, f({0}, {1}) = {2}", a, b, [Call](mul, a, b))
        Console.WriteLine("f=Div, f({0}, {1}) = {2}", a, b, [Call](div, a, b))
    End Sub
End Module
Task: Higher-order functions




Go: package main
import "fmt"

func func1(f func(string) string) string { return f("a string") }
func func2(s string) string { return "func2 called with " + s }
func main() { fmt.Println(func1(func2)) }
Task: Higher-order functions



C#: using System;


delegate int Func2(int a, int b);

class Program
{
    static int Add(int a, int b)
    {
        return a + b;
    }
    
    static int Mul(int a, int b)
    {
        return a * b;
    }
    
    static int Div(int a, int b)
    {
        return a / b;
    }
    
    static int Call(Func2 f, int a, int b)
    {
        
        return f(a, b);
    }

    static void Main()
    {
        int a = 6;
        int b = 2;

        
        Func2 add = new Func2(Add);
        Func2 mul = new Func2(Mul);
        Func2 div = new Func2(Div);
        
        Console.WriteLine("f=Add, f({0}, {1}) = {2}", a, b, Call(add, a, b));
        Console.WriteLine("f=Mul, f({0}, {1}) = {2}", a, b, Call(mul, a, b));
        Console.WriteLine("f=Div, f({0}, {1}) = {2}", a, b, Call(div, a, b));
    }
}








Fortran: FUNCTION FUNC3(FUNC1, FUNC2, x, y)
  REAL, EXTERNAL :: FUNC1, FUNC2 
  REAL :: FUNC3
  REAL :: x, y

  FUNC3 = FUNC1(x) * FUNC2(y)
END FUNCTION FUNC3
Task: Higher-order functions


C++: 
#include <tr1/functional>
#include <iostream>

using namespace std;
using namespace std::tr1;

void first(function<void()> f)
{
  f();
}

void second()
{
  cout << "second\n";
}

int main()
{
  first(second);
}









Fortran: FUNCTION FUNC3(FUNC1, FUNC2, x, y)
  REAL, EXTERNAL :: FUNC1, FUNC2 
  REAL :: FUNC3
  REAL :: x, y

  FUNC3 = FUNC1(x) * FUNC2(y)
END FUNCTION FUNC3
Task: Higher-order functions

C: void myFuncSimple( void (*funcParameter)(void) )
{
    
   
    (*funcParameter)();  
    funcParameter();     

    
}










Fortran: FUNCTION FUNC3(FUNC1, FUNC2, x, y)
  REAL, EXTERNAL :: FUNC1, FUNC2 
  REAL :: FUNC3
  REAL :: x, y

  FUNC3 = FUNC1(x) * FUNC2(y)
END FUNCTION FUNC3
Task: Higher-order functions





Java: public class NewClass {
   
   public NewClass() {
       first(new AnEventOrCallback() {
           public void call() {
               second();
           }
       });
   }
   
   public void first(AnEventOrCallback obj) {
       obj.call();
   }
   
   public void second() {
       System.out.println("Second");
   }
   
   public static void main(String[] args) {
       new NewClass();
   }
}

interface AnEventOrCallback {
   public void call();
}






Fortran: FUNCTION FUNC3(FUNC1, FUNC2, x, y)
  REAL, EXTERNAL :: FUNC1, FUNC2 
  REAL :: FUNC3
  REAL :: x, y

  FUNC3 = FUNC1(x) * FUNC2(y)
END FUNCTION FUNC3
Task: Higher-order functions







Python: def first(function):
    return function()

def second():
    return "second"

result = first(second)




Fortran: FUNCTION FUNC3(FUNC1, FUNC2, x, y)
  REAL, EXTERNAL :: FUNC1, FUNC2 
  REAL :: FUNC3
  REAL :: x, y

  FUNC3 = FUNC1(x) * FUNC2(y)
END FUNCTION FUNC3
Task: Higher-order functions








VB: 
Delegate Function Func2(a As Integer, b As Integer) As Integer

Module Program
    Function Add(a As Integer, b As Integer) As Integer
        Return a + b
    End Function

    Function Mul(a As Integer, b As Integer) As Integer
        Return a * b
    End Function

    Function Div(a As Integer, b As Integer) As Integer
        Return a \ b
    End Function

    
    Function [Call](f As Func2, a As Integer, b As Integer) As Integer
        Return f(a, b)
    End Function

    Sub Main()
        Dim a = 6
        Dim b = 2

        
        Dim add As Func2 = New Func2(AddressOf Program.Add)

        
        Dim div As New Func2(AddressOf Program.Div)

        
        Dim mul As Func2 = AddressOf Program.Mul

        Console.WriteLine("f=Add, f({0}, {1}) = {2}", a, b, [Call](add, a, b))
        Console.WriteLine("f=Mul, f({0}, {1}) = {2}", a, b, [Call](mul, a, b))
        Console.WriteLine("f=Div, f({0}, {1}) = {2}", a, b, [Call](div, a, b))
    End Sub
End Module



Fortran: FUNCTION FUNC3(FUNC1, FUNC2, x, y)
  REAL, EXTERNAL :: FUNC1, FUNC2 
  REAL :: FUNC3
  REAL :: x, y

  FUNC3 = FUNC1(x) * FUNC2(y)
END FUNCTION FUNC3
Task: Higher-order functions






PHP: function first($func) {
  return $func();
}

function second() {
  return 'second';
}

$result = first('second');





Fortran: FUNCTION FUNC3(FUNC1, FUNC2, x, y)
  REAL, EXTERNAL :: FUNC1, FUNC2 
  REAL :: FUNC3
  REAL :: x, y

  FUNC3 = FUNC1(x) * FUNC2(y)
END FUNCTION FUNC3
Task: Higher-order functions






PHP: function first($func) {
  return $func();
}

function second() {
  return 'second';
}

$result = first('second');
Task: Higher-order functions






PHP: function first($func) {
  return $func();
}

function second() {
  return 'second';
}

$result = first('second');






Ada: with Ada.Text_Io; use Ada.Text_Io;

procedure Subprogram_As_Argument is
   type Proc_Access is access procedure;
   
   procedure Second is
   begin
      Put_Line("Second Procedure");
   end Second;
  
   procedure First(Proc : Proc_Access) is
   begin
      Proc.all;
   end First;
begin
   First(Second'Access);
end Subprogram_As_Argument;
Task: CUSIP



C#: using System;
using System.Collections.Generic;

namespace CUSIP {
    class Program {
        static bool IsCusip(string s) {
            if (s.Length != 9) return false;
            int sum = 0;
            for (int i = 0; i <= 7; i++) {
                char c = s[i];

                int v;
                if (c >= '0' && c <= '9') {
                    v = c - 48;
                }
                else if (c >= 'A' && c <= 'Z') {
                    v = c - 55;  
                }
                else if (c == '*') {
                    v = 36;
                }
                else if (c == '#') {
                    v = 38;
                }
                else {
                    return false;
                }
                if (i % 2 == 1) v *= 2;  
                sum += v / 10 + v % 10;
            }
            return s[8] - 48 == (10 - (sum % 10)) % 10;
        }

        static void Main(string[] args) {
            List<string> candidates = new List<string>() {
                "037833100",
                "17275R102",
                "38259P508",
                "594918104",
                "68389X106",
                "68389X105"
            };
            foreach (var candidate in candidates) {
                Console.WriteLine("{0} -> {1}", candidate, IsCusip(candidate) ? "correct" : "incorrect");
            }
        }
    }
}









Ada: with Ada.Text_IO;

procedure Cusip_Test is
   use Ada.Text_IO;

   subtype Cusip is String (1 .. 9);

   function Check_Cusip (Code : Cusip) return Boolean is
      Sum : Integer := 0;
      V   : Integer;

   begin
      for I in Code'First .. Code'Last - 1 loop
         case Code (I) is
            when '0' .. '9' =>
               V := Character'Pos (Code (I)) - Character'Pos ('0');
            when 'A' .. 'Z' =>
               V := Character'Pos (Code (I)) - Character'Pos ('A') + 10;
            when '*' => V := 36;
            when '@' => V := 37;
            when '#' => V := 38;
            when others => return False;
         end case;

         if I mod 2 = 0 then
            V := V * 2;
         end if;

         Sum := Sum + V / 10 + (V mod 10);
      end loop;

      return (10 - (Sum mod 10)) mod 10 =
        Character'Pos (Code (Code'Last)) - Character'Pos ('0');
   end Check_Cusip;

   type Cusip_Array is array (Natural range <>) of Cusip;

   Test_Array : Cusip_Array :=
     ("037833100",
      "17275R102",
      "38259P508",
      "594918104",
      "68389X106",
      "68389X105");
begin
   for I in Test_Array'Range loop
      Put (Test_Array (I) & ": ");
      if Check_Cusip (Test_Array (I)) then
         Put_Line ("valid");
      else
         Put_Line ("not valid");
      end if;
   end loop;
end Cusip_Test;
Task: CUSIP

C: #include<stdlib.h>
#include<stdio.h>

int cusipCheck(char str[10]){
	int sum=0,i,v;
	
	for(i=0;i<8;i++){
		if(str[i]>='0'&&str[i]<='9')
			v = str[i]-'0';
		else if(str[i]>='A'&&str[i]<='Z')
			v = (str[i] - 'A' + 10);
		else if(str[i]=='*')
			v = 36;
		else if(str[i]=='@')
			v = 37;
		else if(str[i]=='#')
			v = 38;
		if(i%2!=0)
			v*=2;
		
		sum += ((int)(v/10) + v%10);
	}
	return ((10 - (sum%10))%10);
}

int main(int argC,char* argV[])
{
	char cusipStr[10];
	
	int i,numLines;
	
	if(argC==1)
		printf("Usage : %s <full path of CUSIP Data file>",argV[0]);
	
	else{
		FILE* fp = fopen(argV[1],"r");
	
		fscanf(fp,"%d",&numLines);
		
		printf("CUSIP       Verdict\n");
		printf("-------------------");
		
		for(i=0;i<numLines;i++){
		
			fscanf(fp,"%s",cusipStr);
		
			printf("\n%s : %s",cusipStr,(cusipCheck(cusipStr)==(cusipStr[8]-'0'))?"Valid":"Invalid");
		}
	
		fclose(fp);
	}
	return 0;
}











Ada: with Ada.Text_IO;

procedure Cusip_Test is
   use Ada.Text_IO;

   subtype Cusip is String (1 .. 9);

   function Check_Cusip (Code : Cusip) return Boolean is
      Sum : Integer := 0;
      V   : Integer;

   begin
      for I in Code'First .. Code'Last - 1 loop
         case Code (I) is
            when '0' .. '9' =>
               V := Character'Pos (Code (I)) - Character'Pos ('0');
            when 'A' .. 'Z' =>
               V := Character'Pos (Code (I)) - Character'Pos ('A') + 10;
            when '*' => V := 36;
            when '@' => V := 37;
            when '#' => V := 38;
            when others => return False;
         end case;

         if I mod 2 = 0 then
            V := V * 2;
         end if;

         Sum := Sum + V / 10 + (V mod 10);
      end loop;

      return (10 - (Sum mod 10)) mod 10 =
        Character'Pos (Code (Code'Last)) - Character'Pos ('0');
   end Check_Cusip;

   type Cusip_Array is array (Natural range <>) of Cusip;

   Test_Array : Cusip_Array :=
     ("037833100",
      "17275R102",
      "38259P508",
      "594918104",
      "68389X106",
      "68389X105");
begin
   for I in Test_Array'Range loop
      Put (Test_Array (I) & ": ");
      if Check_Cusip (Test_Array (I)) then
         Put_Line ("valid");
      else
         Put_Line ("not valid");
      end if;
   end loop;
end Cusip_Test;
Task: CUSIP


C++: #include <iostream>
#include <vector>

bool isCusip(const std::string& s) {
    if (s.size() != 9) return false;

    int sum = 0;
    for (int i = 0; i <= 7; ++i) {
        char c = s[i];

        int v;
        if ('0' <= c && c <= '9') {
            v = c - '0';
        } else if ('A' <= c && c <= 'Z') {
            v = c - '@';
        } else if (c = '*') {
            v = 36;
        } else if (c = '#') {
            v = 38;
        } else {
            return false;
        }
        if (i % 2 == 1) {
            v *= 2;
        }
        sum += v / 10 + v % 10;
    }
    return s[8] - '0' == (10 - (sum % 10)) % 10;
}

int main() {
    using namespace std;

    vector<string> candidates{
        "037833100",
        "17275R102",
        "38259P508",
        "594918104",
        "68389X106",
        "68389X105"
    };

    for (auto str : candidates) {
        auto res = isCusip(str) ? "correct" : "incorrect";
        cout << str.c_str() << " -> " << res << "\n";
    }

    return 0;
}










Ada: with Ada.Text_IO;

procedure Cusip_Test is
   use Ada.Text_IO;

   subtype Cusip is String (1 .. 9);

   function Check_Cusip (Code : Cusip) return Boolean is
      Sum : Integer := 0;
      V   : Integer;

   begin
      for I in Code'First .. Code'Last - 1 loop
         case Code (I) is
            when '0' .. '9' =>
               V := Character'Pos (Code (I)) - Character'Pos ('0');
            when 'A' .. 'Z' =>
               V := Character'Pos (Code (I)) - Character'Pos ('A') + 10;
            when '*' => V := 36;
            when '@' => V := 37;
            when '#' => V := 38;
            when others => return False;
         end case;

         if I mod 2 = 0 then
            V := V * 2;
         end if;

         Sum := Sum + V / 10 + (V mod 10);
      end loop;

      return (10 - (Sum mod 10)) mod 10 =
        Character'Pos (Code (Code'Last)) - Character'Pos ('0');
   end Check_Cusip;

   type Cusip_Array is array (Natural range <>) of Cusip;

   Test_Array : Cusip_Array :=
     ("037833100",
      "17275R102",
      "38259P508",
      "594918104",
      "68389X106",
      "68389X105");
begin
   for I in Test_Array'Range loop
      Put (Test_Array (I) & ": ");
      if Check_Cusip (Test_Array (I)) then
         Put_Line ("valid");
      else
         Put_Line ("not valid");
      end if;
   end loop;
end Cusip_Test;
Task: CUSIP




Go: package main

import "fmt"

func isCusip(s string) bool {
    if len(s) != 9 { return false }
    sum := 0
    for i := 0; i < 8; i++ {
        c := s[i]
        var v int
        switch {
            case c >= '0' && c <= '9':
                v = int(c) - 48
            case c >= 'A' && c <= 'Z':
                v = int(c) - 55
            case c == '*':
                v = 36
            case c == '@':
                v = 37
            case c == '#':
                v = 38
            default:
                return false
        }
        if i % 2 == 1 { v *= 2 }  
        sum += v/10 + v%10
    }
    return int(s[8]) - 48 == (10 - (sum%10)) % 10
}

func main() {
    candidates := []string {
        "037833100",
        "17275R102",
        "38259P508",
        "594918104",
        "68389X106",
        "68389X105",
    }

    for _, candidate := range candidates {
        var b string
        if isCusip(candidate) {
            b = "correct"
        } else {
            b = "incorrect"
        }
        fmt.Printf("%s -> %s\n", candidate, b)
    }
}








Ada: with Ada.Text_IO;

procedure Cusip_Test is
   use Ada.Text_IO;

   subtype Cusip is String (1 .. 9);

   function Check_Cusip (Code : Cusip) return Boolean is
      Sum : Integer := 0;
      V   : Integer;

   begin
      for I in Code'First .. Code'Last - 1 loop
         case Code (I) is
            when '0' .. '9' =>
               V := Character'Pos (Code (I)) - Character'Pos ('0');
            when 'A' .. 'Z' =>
               V := Character'Pos (Code (I)) - Character'Pos ('A') + 10;
            when '*' => V := 36;
            when '@' => V := 37;
            when '#' => V := 38;
            when others => return False;
         end case;

         if I mod 2 = 0 then
            V := V * 2;
         end if;

         Sum := Sum + V / 10 + (V mod 10);
      end loop;

      return (10 - (Sum mod 10)) mod 10 =
        Character'Pos (Code (Code'Last)) - Character'Pos ('0');
   end Check_Cusip;

   type Cusip_Array is array (Natural range <>) of Cusip;

   Test_Array : Cusip_Array :=
     ("037833100",
      "17275R102",
      "38259P508",
      "594918104",
      "68389X106",
      "68389X105");
begin
   for I in Test_Array'Range loop
      Put (Test_Array (I) & ": ");
      if Check_Cusip (Test_Array (I)) then
         Put_Line ("valid");
      else
         Put_Line ("not valid");
      end if;
   end loop;
end Cusip_Test;
Task: CUSIP





Java: import java.util.List;

public class Cusip {
    private static Boolean isCusip(String s) {
        if (s.length() != 9) return false;
        int sum = 0;
        for (int i = 0; i <= 7; i++) {
            char c = s.charAt(i);

            int v;
            if (c >= '0' && c <= '9') {
                v = c - 48;
            } else if (c >= 'A' && c <= 'Z') {
                v = c - 55;  
            } else if (c == '*') {
                v = 36;
            } else if (c == '@') {
                v = 37;
            } else if (c == '#') {
                v = 38;
            } else {
                return false;
            }
            if (i % 2 == 1) v *= 2;  
            sum += v / 10 + v % 10;
        }
        return s.charAt(8) - 48 == (10 - (sum % 10)) % 10;
    }

    public static void main(String[] args) {
        List<String> candidates = List.of(
                "037833100", "17275R102", "38259P508", "594918104", "68389X106", "68389X105", "EXTRACRD8",
                "EXTRACRD9", "BADCUSIP!", "683&9X106", "68389x105", "683$9X106", "68389}105", "87264ABE4"
        );
        for (String candidate : candidates) {
            System.out.printf("%s -> %s%n", candidate, isCusip(candidate) ? "correct" : "incorrect");
        }
    }
}







Ada: with Ada.Text_IO;

procedure Cusip_Test is
   use Ada.Text_IO;

   subtype Cusip is String (1 .. 9);

   function Check_Cusip (Code : Cusip) return Boolean is
      Sum : Integer := 0;
      V   : Integer;

   begin
      for I in Code'First .. Code'Last - 1 loop
         case Code (I) is
            when '0' .. '9' =>
               V := Character'Pos (Code (I)) - Character'Pos ('0');
            when 'A' .. 'Z' =>
               V := Character'Pos (Code (I)) - Character'Pos ('A') + 10;
            when '*' => V := 36;
            when '@' => V := 37;
            when '#' => V := 38;
            when others => return False;
         end case;

         if I mod 2 = 0 then
            V := V * 2;
         end if;

         Sum := Sum + V / 10 + (V mod 10);
      end loop;

      return (10 - (Sum mod 10)) mod 10 =
        Character'Pos (Code (Code'Last)) - Character'Pos ('0');
   end Check_Cusip;

   type Cusip_Array is array (Natural range <>) of Cusip;

   Test_Array : Cusip_Array :=
     ("037833100",
      "17275R102",
      "38259P508",
      "594918104",
      "68389X106",
      "68389X105");
begin
   for I in Test_Array'Range loop
      Put (Test_Array (I) & ": ");
      if Check_Cusip (Test_Array (I)) then
         Put_Line ("valid");
      else
         Put_Line ("not valid");
      end if;
   end loop;
end Cusip_Test;