Update all new Tasks

This commit is contained in:
Ingy döt Net 2015-02-20 09:02:09 -05:00
parent 00a190b0a6
commit 91df62d461
5697 changed files with 93386 additions and 804 deletions

View file

@ -0,0 +1,5 @@
{{Wikipedia|Currying}}
Create a simple demonstrative example of [[wp:Currying|Currying]] in the specific language.
Add any historic details as to how the feature made its way into the language.
<!-- from: http://en.wikipedia.org/w/index.php?title=Currying&direction=prev&oldid=142127294 -->

View file

@ -0,0 +1,10 @@
# Raising a function to a power #
MODE FUN = PROC (REAL) REAL;
PROC pow = (FUN f, INT n, REAL x) REAL: f(x) ** n;
OP ** = (FUN f, INT n) FUN: pow (f, n, );
# Example: sin (3 x) = 3 sin (x) - 4 sin^3 (x) (follows from DeMoivre's theorem) #
REAL x = read real;
print ((new line, sin (3 * x), 3 * sin (x) - 4 * (sin ** 3) (x)))

View file

@ -0,0 +1,9 @@
public delegate int Plus(int y);
public delegate Plus CurriedPlus(int x);
public static CurriedPlus plus =
delegate(int x) {return delegate(int y) {return x + y;};};
static void Main()
{
int sum = plus(3)(4); // sum = 7
int sum2= plus(2)(plus(3)(4)) // sum2 = 9
}

View file

@ -0,0 +1,3 @@
(defun curry (function &rest args-1)
(lambda (&rest args-2)
(apply function (append args-1 args-2))))

View file

@ -0,0 +1,3 @@
(funcall (curry #'+ 10) 10)
20

View file

@ -0,0 +1,11 @@
void main() {
import std.stdio, std.functional;
int add(int a, int b) {
return a + b;
}
alias add2 = curry!(add, 2);
writeln("Add 2 to 3: ", add(2, 3));
writeln("Add 2 to 3 (curried): ", add2(3));
}

View file

@ -0,0 +1,62 @@
-module(currying).
-compile(export_all).
% Function that curry the first or the second argument of a given function of arity 2
curry_first(F,X) ->
fun(Y) -> F(X,Y) end.
curry_second(F,Y) ->
fun(X) -> F(X,Y) end.
% Usual curry
curry(Fun,Arg) ->
case erlang:fun_info(Fun,arity) of
{arity,0} ->
erlang:error(badarg);
{arity,ArityFun} ->
create_ano_fun(ArityFun,Fun,Arg);
_ ->
erlang:error(badarg)
end.
create_ano_fun(Arity,Fun,Arg) ->
Pars =
[{var,1,list_to_atom(lists:flatten(io_lib:format("X~p", [N])))}
|| N <- lists:seq(2,Arity)],
Ano =
{'fun',1,
{clauses,[{clause,1,Pars,[],
[{call,1,{var,1,'Fun'},[{var,1,'Arg'}] ++ Pars}]}]}},
{_,Result,_} = erl_eval:expr(Ano, [{'Arg',Arg},{'Fun',Fun}]),
Result.
% Generalization of the currying
curry_gen(Fun,GivenArgs,PosGivenArgs,PosParArgs) ->
Pos = PosGivenArgs ++ PosParArgs,
case erlang:fun_info(Fun,arity) of
{arity,ArityFun} ->
case ((length(GivenArgs) + length(PosParArgs)) == ArityFun) and
(length(GivenArgs) == length(PosGivenArgs)) and
(length(Pos) == sets:size(sets:from_list(Pos))) of
true ->
fun(ParArgs) ->
case length(ParArgs) == length(PosParArgs) of
true ->
Given = lists:zip(PosGivenArgs,GivenArgs),
Pars = lists:zip(PosParArgs,ParArgs),
{_,Args} = lists:unzip(lists:sort(Given ++ Pars)),
erlang:apply(Fun,Args);
false ->
erlang:error(badarg)
end
end;
false ->
erlang:error(badarg)
end;
_ ->
erlang:error(badarg)
end.

View file

@ -0,0 +1,6 @@
: curry ( x xt1 -- xt2 )
swap 2>r :noname r> postpone literal r> compile, postpone ; ;
5 ' + curry constant +5
5 +5 execute .
7 +5 execute .

View file

@ -0,0 +1,37 @@
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 // A "method value", like currying 'a'
fn2 := Foo.Method // A "method expression", like uncurrying
fmt.Println("2 + 2 =", a.Method(2)) // regular method call
fmt.Println("2 + 3 =", fn1(3))
fmt.Println("2 + 4 =", fn2(a, 4))
fmt.Println("3 + 5 =", fn2(Foo(3), 5))
}

View file

@ -0,0 +1,7 @@
def divide = { Number x, Number y ->
x / y
}
def partsOf120 = divide.curry(120)
println "120: half: ${partsOf120(2)}, third: ${partsOf120(3)}, quarter: ${partsOf120(4)}"

View file

@ -0,0 +1,5 @@
def half = divide.rcurry(2)
def third = divide.rcurry(3)
def quarter = divide.rcurry(4)
println "30: half: ${half(30)}; third: ${third(30)}, quarter: ${quarter(30)}"

View file

@ -0,0 +1 @@
\ ->

View file

@ -0,0 +1 @@
\

View file

@ -0,0 +1 @@
->

View file

@ -0,0 +1 @@
\x y -> x + y

View file

@ -0,0 +1 @@
\x -> \y -> x + y

View file

@ -0,0 +1,13 @@
procedure main(A)
add2 := addN(2)
write("add2(7) = ",add2(7))
write("add2(1) = ",add2(1))
end
procedure addN(n)
return makeProc{ repeat { (x := (x@&source)[1], x +:= n) } }
end
procedure makeProc(A)
return (@A[1], A[1])
end

View file

@ -0,0 +1,12 @@
curry := method(fn,
a := call evalArgs slice(1)
block(
b := a clone appendSeq(call evalArgs)
performWithArgList("fn", b)
)
)
// example:
increment := curry( method(a,b,a+b), 1 )
increment call(5)
// result => 6

View file

@ -0,0 +1,7 @@
threePlus=: 3&+
threePlus 7
10
halve =: %&2 NB. % means divide
halve 20
10
someParabola =: _2 3 1 &p. NB. x^2 + 3x - 2

View file

@ -0,0 +1,38 @@
public class Currier<ARG1, ARG2, RET> {
public interface CurriableFunctor<ARG1, ARG2, RET> {
RET evaluate(ARG1 arg1, ARG2 arg2);
}
public interface CurriedFunctor<ARG2, RET> {
RET evaluate(ARG2 arg);
}
final CurriableFunctor<ARG1, ARG2, RET> functor;
public Currier(CurriableFunctor<ARG1, ARG2, RET> fn) { functor = fn; }
public CurriedFunctor<ARG2, RET> curry(final ARG1 arg1) {
return new CurriedFunctor<ARG2, RET>() {
public RET evaluate(ARG2 arg2) {
return functor.evaluate(arg1, arg2);
}
};
}
public static void main(String[] args) {
Currier.CurriableFunctor<Integer, Integer, Integer> add
= new Currier.CurriableFunctor<Integer, Integer, Integer>() {
public Integer evaluate(Integer arg1, Integer arg2) {
return new Integer(arg1.intValue() + arg2.intValue());
}
};
Currier<Integer, Integer, Integer> currier
= new Currier<Integer, Integer, Integer>(add);
Currier.CurriedFunctor<Integer, Integer> add5
= currier.curry(new Integer(5));
System.out.println(add5.evaluate(new Integer(2)));
}
}

View file

@ -0,0 +1,10 @@
function addN(n) {
var curry = function(x) {
return x + n;
};
return curry;
}
add2 = addN(2);
alert(add2);
alert(add2(7));

View file

@ -0,0 +1,20 @@
using System;
using System.Console;
module Curry
{
Curry[T, U, R](f : T * U -> R) : T -> U -> R
{
fun (x) { fun (y) { f(x, y) } }
}
Main() : void
{
def f(x, y) { x + y }
def g = Curry(f);
def h = Curry(f)(12); // partial application
WriteLine($"$(Curry(f)(20)(22))");
WriteLine($"$(g(21)(21))");
WriteLine($"$(h(30))")
}
}

View file

@ -0,0 +1,4 @@
let addnums x y = x+y (* declare a curried function *)
let add1 = addnums 1 (* bind the first argument to get another function *)
add1 42 (* apply to actually compute a result, 43 *)

View file

@ -0,0 +1,2 @@
let curry f x y = f (x,y)
(* Type signature: ('a * 'b -> 'c) -> 'a -> 'b -> 'c *)

View file

@ -0,0 +1,2 @@
curriedPlus(x)=y->x+y;
curriedPlus(1)(2)

View file

@ -0,0 +1,2 @@
my &negative = &infix:<->.assuming(0);
say negative 1;

View file

@ -0,0 +1,15 @@
sub curry{
my ($func, @args) = @_;
sub {
#This @_ is later
&$func(@args, @_);
}
}
sub plusXY{
$_[0] + $_[1];
}
my $plusXOne = curry(\&plusXY, 1);
print &$plusXOne(3), "\n";

View file

@ -0,0 +1,4 @@
def addN(n):
def adder(x):
return x + n
return adder

View file

@ -0,0 +1,5 @@
>>> add2 = addN(2)
>>> add2
<function adder at 0x009F1E30>
>>> add2(7)
9

1
Task/Currying/README Normal file
View file

@ -0,0 +1 @@
Data source: http://rosettacode.org/wiki/Currying

View file

@ -0,0 +1,7 @@
/*REXX program demonstrates a REXX currying method to perform addition. */
say 'add 2 to 3: ' add(2 ,3)
say 'add 2 to 3 (curried):' add2(3)
exit /*stick a fork in it, we're done.*/
/*──────────────────────────────────subroutines─────────────────────────*/
add: procedure; $=arg(1); do j=2 to arg(); $=$+arg(j); end; return $
add2: procedure; return add(arg(1), 2)

View file

@ -0,0 +1,12 @@
/*REXX program demonstrates a REXX currying method to perform addition. */
say 'add 2 to 3: ' add(2 ,3)
say 'add 2 to 3 (curried):' add2(3)
exit /*stick a fork in it, we're done.*/
/*──────────────────────────────────ADD subroutine──────────────────────*/
add: procedure; $=0; do j=1 for arg()
do k=1 for words(arg(j)); $=$+word(arg(j),k)
end /*k*/
end /*j*/
return $
/*──────────────────────────────────ADD2 subroutine─────────────────────*/
add2: procedure; return add(arg(1), 2)

View file

@ -0,0 +1,2 @@
#lang racket
(((curry +) 3) 2) ; =>5

View file

@ -0,0 +1,6 @@
#lang racket
(define ((curried+ a) b)
(+ a b))
((curried+ 3) 2) ; => 5

View file

@ -0,0 +1,13 @@
b = proc {|x, y, z| (x||0) + (y||0) + (z||0) }
p b.curry[1][2][3] #=> 6
p b.curry[1, 2][3, 4] #=> 6
p b.curry(5)[1][2][3][4][5] #=> 6
p b.curry(5)[1, 2][3, 4][5] #=> 6
p b.curry(1)[1] #=> 1
b = proc {|x, y, z, *w| (x||0) + (y||0) + (z||0) + w.inject(0, &:+) }
p b.curry[1][2][3] #=> 6
p b.curry[1, 2][3, 4] #=> 10
p b.curry(5)[1][2][3][4][5] #=> 15
p b.curry(5)[1, 2][3, 4][5] #=> 15
p b.curry(1)[1] #=> 1

View file

@ -0,0 +1,9 @@
#![feature(box_syntax)]
fn add_n(n : i32) -> Box<Fn(i32) -> i32 + 'static> {
box move |&: x| n + x
}
fn main() {
let adder = add_n(40);
println!("The answer to life is {}.", adder(2));
}

View file

@ -0,0 +1,4 @@
fun addnums (x:int) y = x+y (* declare a curried function *)
val add1 = addnums 1 (* bind the first argument to get another function *)
add1 42 (* apply to actually compute a result, 43 *)

View file

@ -0,0 +1,2 @@
fun curry f x y = f(x,y)
(* Type signature: ('a * 'b -> 'c) -> 'a -> 'b -> 'c *)

View file

@ -0,0 +1,2 @@
interp alias {} addone {} ::tcl::mathop::+ 1
puts [addone 6]; # => 7