WeixiangYan/CodeTransOcean huggingface dataset:
Task: Partial function application


C++: #include <utility> 
#include <algorithm>
#include <array>
#include <iterator>
#include <iostream>


template< class F, class Arg >
struct PApply
{
    F f;
    Arg arg;

    template< class F_, class Arg_ >
    PApply( F_&& f, Arg_&& arg )
        : f(std::forward<F_>(f)), arg(std::forward<Arg_>(arg))
    {
    }

    
    template< class ... Args >
    auto operator() ( Args&& ...args )
        -> decltype( f(arg,std::declval<Args>()...) )
    {
        return f( arg, std::forward<Args>(args)... );
    }
};

template< class F, class Arg >
PApply<F,Arg> papply( F&& f, Arg&& arg )
{
    return PApply<F,Arg>( std::forward<F>(f), std::forward<Arg>(arg) );
}


template< class F >
std::array<int,4> fs( F&& f, std::array<int,4> cont )
{
    std::transform( std::begin(cont), std::end(cont), std::begin(cont), 
                    std::forward<F>(f) );
    return cont;
}

std::ostream& operator << ( std::ostream& out, const std::array<int,4>& c )
{
    std::copy( std::begin(c), std::end(c), 
               std::ostream_iterator<int>(out, ", ") );
    return out;
}

int f1( int x ) { return x * 2; }
int f2( int x ) { return x * x; }

int main()
{
    std::array<int,4> xs = {{ 0, 1, 2, 3 }};
    std::array<int,4> ys = {{ 2, 4, 6, 8 }};

    auto fsf1 = papply( fs<decltype(f1)>, f1 );
    auto fsf2 = papply( fs<decltype(f2)>, f2 );

    std::cout << "xs:\n"
              << "\tfsf1: " << fsf1(xs) << '\n'
              << "\tfsf2: " << fsf2(xs) << "\n\n"
              << "ys:\n"
              << "\tfsf1: " << fsf1(ys) << '\n'
              << "\tfsf2: " << fsf2(ys) << '\n';
}
Task: Partial function application





Java: import java.util.Arrays;

public class PartialApplication {
	interface IntegerFunction {
		int call(int arg);
	}

	
	static int[] fs(IntegerFunction f, int[] s) {
		int[] r = new int[s.length];
		for (int i = 0; i < s.length; i++)
			r[i] = f.call(s[i]);
		return r;		
	}

	interface SequenceFunction {
		int[] call(int[] arg);
	}

	
	
	static SequenceFunction fs(final IntegerFunction f) {
		return new SequenceFunction() {
			public int[] call(int[] s) {
				
				return fs(f, s);
			}
		};
	}

	static IntegerFunction f1 = new IntegerFunction() {
		public int call(int i) {
			return i * 2;
		}
	};

	static IntegerFunction f2 = new IntegerFunction() {
		public int call(int i) {
			return i * i;
		}
	};

	static SequenceFunction fsf1 = fs(f1); 

	static SequenceFunction fsf2 = fs(f2);

	public static void main(String[] args) {
		int[][] sequences = {
			{ 0, 1, 2, 3 },
			{ 2, 4, 6, 8 },
		};

		for (int[] array : sequences) {
			System.out.printf(
			    "array: %s\n" +
			    "  fsf1(array): %s\n" +
			    "  fsf2(array): %s\n",
			    Arrays.toString(array),
			    Arrays.toString(fsf1.call(array)),
			    Arrays.toString(fsf2.call(array)));
		}
	}
}
Task: Partial function application







Python: from functools import partial

def fs(f, s): return [f(value) for value in s]

def f1(value): return value * 2

def f2(value): return value ** 2

fsf1 = partial(fs, f1)
fsf2 = partial(fs, f2)

s = [0, 1, 2, 3]
assert fs(f1, s) == fsf1(s) 
assert fs(f2, s) == fsf2(s) 

s = [2, 4, 6, 8]
assert fs(f1, s) == fsf1(s) 
assert fs(f2, s) == fsf2(s)
Task: Partial function application








VB: Module PartialApplication
    Function fs(Of TSource, TResult)(f As Func(Of TSource, TResult), s As IEnumerable(Of TSource)) As IEnumerable(Of TResult)
        
        Return s.Select(f)
    End Function

    Function f1(x As Integer) As Integer
        Return x * 2
    End Function

    Function f2(x As Integer) As Integer
        Return x * x
    End Function

    
    Function PartialApply(Of T1, T2, TResult)(f As Func(Of T1, T2, TResult), arg As T1) As Func(Of T2, TResult)
        Return Function(arg2) f(arg, arg2)
    End Function

    Sub Main()
        Dim args1 As Integer() = {0, 1, 2, 3}
        Dim args2 As Integer() = {2, 4, 6, 8}

        Dim fsf1 = PartialApply(Of Func(Of Integer, Integer), IEnumerable(Of Integer), IEnumerable(Of Integer))(AddressOf fs, AddressOf f1)
        Dim fsf2 = PartialApply(Of Func(Of Integer, Integer), IEnumerable(Of Integer), IEnumerable(Of Integer))(AddressOf fs, AddressOf f2)

        Console.WriteLine("fsf1, 0-3: " & String.Join(", ", fsf1(args1)))
        Console.WriteLine("fsf1, evens: " & String.Join(", ", fsf1(args2)))
        Console.WriteLine("fsf2, 0-3: " & String.Join(", ", fsf2(args1)))
        Console.WriteLine("fsf2, evens: " & String.Join(", ", fsf2(args2)))
    End Sub
End Module
Task: Partial function application




Go: package main

import "fmt"




type fn func(int) int


func (f fn) fs(s ...int) (r []int) {
	for _, i := range s {
		r = append(r, f(i))
	}
	return r
}


func f1(i int) int { return i * 2 }
func f2(i int) int { return i * i }




func addn(n int) func(...int) []int {
	return func(s ...int) []int {
		var r []int
		for _, i := range s {
			r = append(r, n+i)
		}
		return r
	}
}

func main() {
	
	fsf1 := fn(f1).fs
	fsf2 := fn(f2).fs
	
	fsf3 := addn(100)

	s := []int{0, 1, 2, 3}
	fmt.Println("For s =", s)
	fmt.Println("  fsf1:", fsf1(s...))       
	fmt.Println("  fsf2:", fsf2(0, 1, 2, 3)) 
	fmt.Println("  fsf3:", fsf3(0, 1, 2, 3))
	fmt.Println("  fsf2(fsf1):", fsf2(fsf1(s...)...))

	s = []int{2, 4, 6, 8}
	fmt.Println("For s =", s)
	fmt.Println("  fsf1:", fsf1(2, 4, 6, 8))
	fmt.Println("  fsf2:", fsf2(s...))
	fmt.Println("  fsf3:", fsf3(s...))
	fmt.Println("  fsf3(fsf1):", fsf3(fsf1(s...)...))
}
Task: Associative array_Merging



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

public class Program
{
    public static void Main() {
        var baseData = new Dictionary<string, object> {
            ["name"] = "Rocket Skates",
            ["price"] = 12.75,
            ["color"] = "yellow"
        };
        var updateData = new Dictionary<string, object> {
            ["price"] = 15.25,
            ["color"] = "red",
            ["year"] = 1974
        };
        var mergedData = new Dictionary<string, object>();
        foreach (var entry in baseData.Concat(updateData)) {
            mergedData[entry.Key] = entry.Value;
        }
        foreach (var entry in mergedData) {
            Console.WriteLine(entry);
        }
   }
}









Ada: with Ada.Text_Io;
with Ada.Containers.Indefinite_Ordered_Maps;

procedure Merge_Maps is
   use Ada.Text_Io;

   type Key_Type   is new String;
   type Value_Type is new String;

   package Maps is
     new Ada.Containers.Indefinite_Ordered_Maps (Key_Type     => Key_Type,
                                                 Element_Type => Value_Type);
   use Maps;

   function Merge (Original : Map; Update : Map) return Map is
      Result : Map    := Original;
      Cur    : Cursor := Update.First;
   begin
      while Has_Element (Cur) loop
         if Original.Contains (Key (Cur)) then
            Result.Replace_Element (Result.Find (Key (Cur)),
                                    Element (Cur));
         else
            Result.Insert (Key (Cur), Element (Cur));
         end if;
         Next (Cur);
      end loop;
      return Result;
   end Merge;

   procedure Put_Map (M : Map) is
      Cur : Cursor := M.First;
   begin
      while Has_Element (Cur) loop
         Put (String (Key (Cur)));
         Set_Col (12);
         Put (String (Element (Cur)));
         New_Line;
         Next (Cur);
      end loop;
   end Put_Map;

   Original : Map;
   Update   : Map;
   Result   : Map;
begin
   Original.Insert ("name", "Rocket Skates");
   Original.Insert ("price", "12.75");
   Original.Insert ("color", "yellow");

   Update.Insert ("price", "15.25");
   Update.Insert ("color", "red");
   Update.Insert ("year", "1974");

   Result := Merge (Original, Update);

   Put_Line ("Original:");
   Put_Map (Original);
   New_Line;

   Put_Line ("Update:");
   Put_Map (Update);
   New_Line;

   Put_Line ("Result of merge:");
   Put_Map (Result);
   New_Line;

end Merge_Maps;
Task: Associative array_Merging


C++: #include <iostream>
#include <string>
#include <map>

template<typename map_type>
map_type merge(const map_type& original, const map_type& update) {
    map_type result(update);
    result.insert(original.begin(), original.end());
    return result;
}

int main() {
    typedef std::map<std::string, std::string> map;
    map original{
        {"name", "Rocket Skates"},
        {"price", "12.75"},
        {"color", "yellow"}
    };
    map update{
        {"price", "15.25"},
        {"color", "red"},
        {"year", "1974"}
    };
    map merged(merge(original, update));
    for (auto&& i : merged)
        std::cout << "key: " << i.first << ", value: " << i.second << '\n';
    return 0;
}










Ada: with Ada.Text_Io;
with Ada.Containers.Indefinite_Ordered_Maps;

procedure Merge_Maps is
   use Ada.Text_Io;

   type Key_Type   is new String;
   type Value_Type is new String;

   package Maps is
     new Ada.Containers.Indefinite_Ordered_Maps (Key_Type     => Key_Type,
                                                 Element_Type => Value_Type);
   use Maps;

   function Merge (Original : Map; Update : Map) return Map is
      Result : Map    := Original;
      Cur    : Cursor := Update.First;
   begin
      while Has_Element (Cur) loop
         if Original.Contains (Key (Cur)) then
            Result.Replace_Element (Result.Find (Key (Cur)),
                                    Element (Cur));
         else
            Result.Insert (Key (Cur), Element (Cur));
         end if;
         Next (Cur);
      end loop;
      return Result;
   end Merge;

   procedure Put_Map (M : Map) is
      Cur : Cursor := M.First;
   begin
      while Has_Element (Cur) loop
         Put (String (Key (Cur)));
         Set_Col (12);
         Put (String (Element (Cur)));
         New_Line;
         Next (Cur);
      end loop;
   end Put_Map;

   Original : Map;
   Update   : Map;
   Result   : Map;
begin
   Original.Insert ("name", "Rocket Skates");
   Original.Insert ("price", "12.75");
   Original.Insert ("color", "yellow");

   Update.Insert ("price", "15.25");
   Update.Insert ("color", "red");
   Update.Insert ("year", "1974");

   Result := Merge (Original, Update);

   Put_Line ("Original:");
   Put_Map (Original);
   New_Line;

   Put_Line ("Update:");
   Put_Map (Update);
   New_Line;

   Put_Line ("Result of merge:");
   Put_Map (Result);
   New_Line;

end Merge_Maps;
Task: Associative array_Merging




Go: package main

import "fmt"

type assoc map[string]interface{}

func merge(base, update assoc) assoc {
    result := make(assoc)
    for k, v := range base {
        result[k] = v
    }
    for k, v := range update {
        result[k] = v
    }
    return result
}

func main() {
    base := assoc{"name": "Rocket Skates", "price": 12.75, "color": "yellow"}
    update := assoc{"price": 15.25, "color": "red", "year": 1974}
    result := merge(base, update)
    fmt.Println(result)
}








Ada: with Ada.Text_Io;
with Ada.Containers.Indefinite_Ordered_Maps;

procedure Merge_Maps is
   use Ada.Text_Io;

   type Key_Type   is new String;
   type Value_Type is new String;

   package Maps is
     new Ada.Containers.Indefinite_Ordered_Maps (Key_Type     => Key_Type,
                                                 Element_Type => Value_Type);
   use Maps;

   function Merge (Original : Map; Update : Map) return Map is
      Result : Map    := Original;
      Cur    : Cursor := Update.First;
   begin
      while Has_Element (Cur) loop
         if Original.Contains (Key (Cur)) then
            Result.Replace_Element (Result.Find (Key (Cur)),
                                    Element (Cur));
         else
            Result.Insert (Key (Cur), Element (Cur));
         end if;
         Next (Cur);
      end loop;
      return Result;
   end Merge;

   procedure Put_Map (M : Map) is
      Cur : Cursor := M.First;
   begin
      while Has_Element (Cur) loop
         Put (String (Key (Cur)));
         Set_Col (12);
         Put (String (Element (Cur)));
         New_Line;
         Next (Cur);
      end loop;
   end Put_Map;

   Original : Map;
   Update   : Map;
   Result   : Map;
begin
   Original.Insert ("name", "Rocket Skates");
   Original.Insert ("price", "12.75");
   Original.Insert ("color", "yellow");

   Update.Insert ("price", "15.25");
   Update.Insert ("color", "red");
   Update.Insert ("year", "1974");

   Result := Merge (Original, Update);

   Put_Line ("Original:");
   Put_Map (Original);
   New_Line;

   Put_Line ("Update:");
   Put_Map (Update);
   New_Line;

   Put_Line ("Result of merge:");
   Put_Map (Result);
   New_Line;

end Merge_Maps;
Task: Associative array_Merging





Java: import java.util.*;

class MergeMaps {
    public static void main(String[] args) {
        Map<String, Object> base = new HashMap<>();
        base.put("name", "Rocket Skates");
        base.put("price", 12.75);
        base.put("color", "yellow");
        Map<String, Object> update = new HashMap<>();
        update.put("price", 15.25);
        update.put("color", "red");
        update.put("year", 1974);

        Map<String, Object> result = new HashMap<>(base);
        result.putAll(update);

        System.out.println(result);
    }
}







Ada: with Ada.Text_Io;
with Ada.Containers.Indefinite_Ordered_Maps;

procedure Merge_Maps is
   use Ada.Text_Io;

   type Key_Type   is new String;
   type Value_Type is new String;

   package Maps is
     new Ada.Containers.Indefinite_Ordered_Maps (Key_Type     => Key_Type,
                                                 Element_Type => Value_Type);
   use Maps;

   function Merge (Original : Map; Update : Map) return Map is
      Result : Map    := Original;
      Cur    : Cursor := Update.First;
   begin
      while Has_Element (Cur) loop
         if Original.Contains (Key (Cur)) then
            Result.Replace_Element (Result.Find (Key (Cur)),
                                    Element (Cur));
         else
            Result.Insert (Key (Cur), Element (Cur));
         end if;
         Next (Cur);
      end loop;
      return Result;
   end Merge;

   procedure Put_Map (M : Map) is
      Cur : Cursor := M.First;
   begin
      while Has_Element (Cur) loop
         Put (String (Key (Cur)));
         Set_Col (12);
         Put (String (Element (Cur)));
         New_Line;
         Next (Cur);
      end loop;
   end Put_Map;

   Original : Map;
   Update   : Map;
   Result   : Map;
begin
   Original.Insert ("name", "Rocket Skates");
   Original.Insert ("price", "12.75");
   Original.Insert ("color", "yellow");

   Update.Insert ("price", "15.25");
   Update.Insert ("color", "red");
   Update.Insert ("year", "1974");

   Result := Merge (Original, Update);

   Put_Line ("Original:");
   Put_Map (Original);
   New_Line;

   Put_Line ("Update:");
   Put_Map (Update);
   New_Line;

   Put_Line ("Result of merge:");
   Put_Map (Result);
   New_Line;

end Merge_Maps;
Task: Associative array_Merging







Python: base = {"name":"Rocket Skates", "price":12.75, "color":"yellow"}
update = {"price":15.25, "color":"red", "year":1974}

result = {**base, **update}

print(result)





Ada: with Ada.Text_Io;
with Ada.Containers.Indefinite_Ordered_Maps;

procedure Merge_Maps is
   use Ada.Text_Io;

   type Key_Type   is new String;
   type Value_Type is new String;

   package Maps is
     new Ada.Containers.Indefinite_Ordered_Maps (Key_Type     => Key_Type,
                                                 Element_Type => Value_Type);
   use Maps;

   function Merge (Original : Map; Update : Map) return Map is
      Result : Map    := Original;
      Cur    : Cursor := Update.First;
   begin
      while Has_Element (Cur) loop
         if Original.Contains (Key (Cur)) then
            Result.Replace_Element (Result.Find (Key (Cur)),
                                    Element (Cur));
         else
            Result.Insert (Key (Cur), Element (Cur));
         end if;
         Next (Cur);
      end loop;
      return Result;
   end Merge;

   procedure Put_Map (M : Map) is
      Cur : Cursor := M.First;
   begin
      while Has_Element (Cur) loop
         Put (String (Key (Cur)));
         Set_Col (12);
         Put (String (Element (Cur)));
         New_Line;
         Next (Cur);
      end loop;
   end Put_Map;

   Original : Map;
   Update   : Map;
   Result   : Map;
begin
   Original.Insert ("name", "Rocket Skates");
   Original.Insert ("price", "12.75");
   Original.Insert ("color", "yellow");

   Update.Insert ("price", "15.25");
   Update.Insert ("color", "red");
   Update.Insert ("year", "1974");

   Result := Merge (Original, Update);

   Put_Line ("Original:");
   Put_Map (Original);
   New_Line;

   Put_Line ("Update:");
   Put_Map (Update);
   New_Line;

   Put_Line ("Result of merge:");
   Put_Map (Result);
   New_Line;

end Merge_Maps;
Task: Associative array_Merging



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

public class Program
{
    public static void Main() {
        var baseData = new Dictionary<string, object> {
            ["name"] = "Rocket Skates",
            ["price"] = 12.75,
            ["color"] = "yellow"
        };
        var updateData = new Dictionary<string, object> {
            ["price"] = 15.25,
            ["color"] = "red",
            ["year"] = 1974
        };
        var mergedData = new Dictionary<string, object>();
        foreach (var entry in baseData.Concat(updateData)) {
            mergedData[entry.Key] = entry.Value;
        }
        foreach (var entry in mergedData) {
            Console.WriteLine(entry);
        }
   }
}
Task: Associative array_Merging


C++: #include <iostream>
#include <string>
#include <map>

template<typename map_type>
map_type merge(const map_type& original, const map_type& update) {
    map_type result(update);
    result.insert(original.begin(), original.end());
    return result;
}

int main() {
    typedef std::map<std::string, std::string> map;
    map original{
        {"name", "Rocket Skates"},
        {"price", "12.75"},
        {"color", "yellow"}
    };
    map update{
        {"price", "15.25"},
        {"color", "red"},
        {"year", "1974"}
    };
    map merged(merge(original, update));
    for (auto&& i : merged)
        std::cout << "key: " << i.first << ", value: " << i.second << '\n';
    return 0;
}
Task: Associative array_Merging





Java: import java.util.*;

class MergeMaps {
    public static void main(String[] args) {
        Map<String, Object> base = new HashMap<>();
        base.put("name", "Rocket Skates");
        base.put("price", 12.75);
        base.put("color", "yellow");
        Map<String, Object> update = new HashMap<>();
        update.put("price", 15.25);
        update.put("color", "red");
        update.put("year", 1974);

        Map<String, Object> result = new HashMap<>(base);
        result.putAll(update);

        System.out.println(result);
    }
}
Task: Associative array_Merging







Python: base = {"name":"Rocket Skates", "price":12.75, "color":"yellow"}
update = {"price":15.25, "color":"red", "year":1974}

result = {**base, **update}

print(result)
Task: Associative array_Merging








VB: Private Type Associative
    Key As String
    Value As Variant
End Type
Sub Main_Array_Associative()
Dim BaseArray(2) As Associative, UpdateArray(2) As Associative
    FillArrays BaseArray, UpdateArray
    ReDim Result(UBound(BaseArray)) As Associative
    MergeArray Result, BaseArray, UpdateArray
    PrintOut Result
End Sub
Private Sub MergeArray(Res() As Associative, Base() As Associative, Update() As Associative)
Dim i As Long, Respons As Long
    Res = Base
    For i = LBound(Update) To UBound(Update)
        If Exist(Respons, Base, Update(i).Key) Then
            Res(Respons).Value = Update(i).Value
        Else
            ReDim Preserve Res(UBound(Res) + 1)
            Res(UBound(Res)).Key = Update(i).Key
            Res(UBound(Res)).Value = Update(i).Value
        End If
    Next
End Sub
Private Function Exist(R As Long, B() As Associative, K As String) As Boolean
Dim i As Long
    Do
        If B(i).Key = K Then
            Exist = True
            R = i
        End If
        i = i + 1
    Loop While i <= UBound(B) And Not Exist
End Function
Private Sub FillArrays(B() As Associative, U() As Associative)
    B(0).Key = "name"
    B(0).Value = "Rocket Skates"
    B(1).Key = "price"
    B(1).Value = 12.75
    B(2).Key = "color"
    B(2).Value = "yellow"
    U(0).Key = "price"
    U(0).Value = 15.25
    U(1).Key = "color"
    U(1).Value = "red"
    U(2).Key = "year"
    U(2).Value = 1974
End Sub
Private Sub PrintOut(A() As Associative)
Dim i As Long
    Debug.Print "Key", "Value"
    For i = LBound(A) To UBound(A)
        Debug.Print A(i).Key, A(i).Value
    Next i
    Debug.Print "-----------------------------"
End Sub
Task: Associative array_Merging




Go: package main

import "fmt"

type assoc map[string]interface{}

func merge(base, update assoc) assoc {
    result := make(assoc)
    for k, v := range base {
        result[k] = v
    }
    for k, v := range update {
        result[k] = v
    }
    return result
}

func main() {
    base := assoc{"name": "Rocket Skates", "price": 12.75, "color": "yellow"}
    update := assoc{"price": 15.25, "color": "red", "year": 1974}
    result := merge(base, update)
    fmt.Println(result)
}
Task: Associative array_Merging






PHP: <?
$base = array("name" => "Rocket Skates", "price" => 12.75, "color" => "yellow");
$update = array("price" => 15.25, "color" => "red", "year" => 1974);

$result = $update + $base; // Notice that the order is reversed
print_r($result);
?>
Task: Associative array_Merging






PHP: <?
$base = array("name" => "Rocket Skates", "price" => 12.75, "color" => "yellow");
$update = array("price" => 15.25, "color" => "red", "year" => 1974);

$result = $update + $base; // Notice that the order is reversed
print_r($result);
?>






Ada: with Ada.Text_Io;
with Ada.Containers.Indefinite_Ordered_Maps;

procedure Merge_Maps is
   use Ada.Text_Io;

   type Key_Type   is new String;
   type Value_Type is new String;

   package Maps is
     new Ada.Containers.Indefinite_Ordered_Maps (Key_Type     => Key_Type,
                                                 Element_Type => Value_Type);
   use Maps;

   function Merge (Original : Map; Update : Map) return Map is
      Result : Map    := Original;
      Cur    : Cursor := Update.First;
   begin
      while Has_Element (Cur) loop
         if Original.Contains (Key (Cur)) then
            Result.Replace_Element (Result.Find (Key (Cur)),
                                    Element (Cur));
         else
            Result.Insert (Key (Cur), Element (Cur));
         end if;
         Next (Cur);
      end loop;
      return Result;
   end Merge;

   procedure Put_Map (M : Map) is
      Cur : Cursor := M.First;
   begin
      while Has_Element (Cur) loop
         Put (String (Key (Cur)));
         Set_Col (12);
         Put (String (Element (Cur)));
         New_Line;
         Next (Cur);
      end loop;
   end Put_Map;

   Original : Map;
   Update   : Map;
   Result   : Map;
begin
   Original.Insert ("name", "Rocket Skates");
   Original.Insert ("price", "12.75");
   Original.Insert ("color", "yellow");

   Update.Insert ("price", "15.25");
   Update.Insert ("color", "red");
   Update.Insert ("year", "1974");

   Result := Merge (Original, Update);

   Put_Line ("Original:");
   Put_Map (Original);
   New_Line;

   Put_Line ("Update:");
   Put_Map (Update);
   New_Line;

   Put_Line ("Result of merge:");
   Put_Map (Result);
   New_Line;

end Merge_Maps;