Another update from ingydotnet^djgoku
This commit is contained in:
parent
91df62d461
commit
948b86eafa
7604 changed files with 108452 additions and 22726 deletions
1
Task/Call-a-function/Bracmat/call-a-function-1.bracmat
Normal file
1
Task/Call-a-function/Bracmat/call-a-function-1.bracmat
Normal file
|
|
@ -0,0 +1 @@
|
|||
aFunctionWithoutArguments$
|
||||
1
Task/Call-a-function/Bracmat/call-a-function-2.bracmat
Normal file
1
Task/Call-a-function/Bracmat/call-a-function-2.bracmat
Normal file
|
|
@ -0,0 +1 @@
|
|||
aFunctionWithoutArguments'
|
||||
1
Task/Call-a-function/Bracmat/call-a-function-3.bracmat
Normal file
1
Task/Call-a-function/Bracmat/call-a-function-3.bracmat
Normal file
|
|
@ -0,0 +1 @@
|
|||
func$!myargument;
|
||||
1
Task/Call-a-function/Bracmat/call-a-function-4.bracmat
Normal file
1
Task/Call-a-function/Bracmat/call-a-function-4.bracmat
Normal file
|
|
@ -0,0 +1 @@
|
|||
(yourfunc=local vars.function body)
|
||||
1
Task/Call-a-function/Bracmat/call-a-function-5.bracmat
Normal file
1
Task/Call-a-function/Bracmat/call-a-function-5.bracmat
Normal file
|
|
@ -0,0 +1 @@
|
|||
('$myfunc:(=?yourfunc))
|
||||
1
Task/Call-a-function/Bracmat/call-a-function-6.bracmat
Normal file
1
Task/Call-a-function/Bracmat/call-a-function-6.bracmat
Normal file
|
|
@ -0,0 +1 @@
|
|||
myfunc$!myarg:?myresult
|
||||
1
Task/Call-a-function/Bracmat/call-a-function-7.bracmat
Normal file
1
Task/Call-a-function/Bracmat/call-a-function-7.bracmat
Normal file
|
|
@ -0,0 +1 @@
|
|||
myfunc$!myarg&yourfunc$!yourarg
|
||||
1
Task/Call-a-function/Bracmat/call-a-function-8.bracmat
Normal file
1
Task/Call-a-function/Bracmat/call-a-function-8.bracmat
Normal file
|
|
@ -0,0 +1 @@
|
|||
`(myfunc$!myarg)&yourfunc$!yourarg
|
||||
10
Task/Call-a-function/Bracmat/call-a-function-9.bracmat
Normal file
10
Task/Call-a-function/Bracmat/call-a-function-9.bracmat
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
( ( plus
|
||||
= a b
|
||||
. !arg:%?a ?b
|
||||
& !b:
|
||||
& '(.!arg+$a)
|
||||
| !a+!b
|
||||
)
|
||||
& out$("1+2, not partial:" plus$(1 2))
|
||||
& out$("1+2, partial:" (plus$1)$2)
|
||||
);
|
||||
2
Task/Call-a-function/C++/call-a-function-1.cpp
Normal file
2
Task/Call-a-function/C++/call-a-function-1.cpp
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
/* function with no arguments */
|
||||
foo();
|
||||
5
Task/Call-a-function/C++/call-a-function-2.cpp
Normal file
5
Task/Call-a-function/C++/call-a-function-2.cpp
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
/* passing arguments by value*/
|
||||
/* function with one argument */
|
||||
bar(arg1);
|
||||
/* function with multiple arguments */
|
||||
baz(arg1, arg2);
|
||||
2
Task/Call-a-function/C++/call-a-function-3.cpp
Normal file
2
Task/Call-a-function/C++/call-a-function-3.cpp
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
/* get return value of a function */
|
||||
variable = function(args);
|
||||
14
Task/Call-a-function/C++/call-a-function-4.cpp
Normal file
14
Task/Call-a-function/C++/call-a-function-4.cpp
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
#include <iostream>
|
||||
using namespace std;
|
||||
/* passing arguments by reference */
|
||||
void f(int &y) /* variable is now passed by reference */
|
||||
{
|
||||
y++;
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int x = 0;
|
||||
cout<<"x = "<<x<<endl; /* should produce result "x = 0" */
|
||||
f(x); /* call function f */
|
||||
cout<<"x = "<<x<<endl; /* should produce result "x = 1" */
|
||||
}
|
||||
25
Task/Call-a-function/Common-Lisp/call-a-function.lisp
Normal file
25
Task/Call-a-function/Common-Lisp/call-a-function.lisp
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
;Calling a function that requires no arguments
|
||||
(defun a () "This is the 'A' function")
|
||||
(a)
|
||||
;Calling a function with a fixed number of arguments
|
||||
(defun b (x y) (list x y))
|
||||
(b 1 2)
|
||||
;Calling a function with optional arguments
|
||||
(defun c (&optional x y) (list x y))
|
||||
(c 1)
|
||||
;Calling a function with a variable number of arguments
|
||||
(defun d (&rest args) args)
|
||||
(d 1 2 3 4 5 6 7 8)
|
||||
;Calling a function with named arguments
|
||||
(defun e (&key (x 1) (y 2)) (list x y))
|
||||
(e :x 10 :y 20)
|
||||
;Using a function in first-class context within an expression
|
||||
(defun f (func) (funcall func))
|
||||
(f #'a)
|
||||
;Obtaining the return value of a function
|
||||
(defvar return-of-a (a))
|
||||
;Is partial application possible and how
|
||||
(defun curry (function &rest args-1)
|
||||
(lambda (&rest args-2)
|
||||
(apply function (append args-1 args-2))))
|
||||
(funcall (curry #'+ 1) 2)
|
||||
|
|
@ -1,5 +1,7 @@
|
|||
f(); \\ zero arguments
|
||||
sin(Pi/2); \\ fixed number of arguments
|
||||
Str("gg", 1, "hh"); \\ variable number of arguments
|
||||
vecsort([5,6]) != vecsort([5,6],,4) \\ optional arguments
|
||||
Str("gg", 1, "hh") \\ variable number of arguments
|
||||
call(Str, ["gg", 1, "hh"]) \\ variable number of arguments in a vector
|
||||
(x->x^2)(3); \\ first-class
|
||||
x = sin(0); \\ get function value
|
||||
|
|
|
|||
4
Task/Call-a-function/Ruby/call-a-function-1.rb
Normal file
4
Task/Call-a-function/Ruby/call-a-function-1.rb
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
def foo() p "foo" end
|
||||
|
||||
foo #=> "foo"
|
||||
foo() #=> "foo"
|
||||
13
Task/Call-a-function/Ruby/call-a-function-10.rb
Normal file
13
Task/Call-a-function/Ruby/call-a-function-10.rb
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
# return value substance
|
||||
i = 3
|
||||
p 1 + i #=> 4 1.+(i)
|
||||
p i < 5 #=> true i.<(5)
|
||||
p 2 ** i #=> 8 2.**(i)
|
||||
p -i #=> -3 i.-@()
|
||||
a = [1,2,3]
|
||||
p a[0] #=> 1 a.[](0)
|
||||
a[2] = "0" # a.[]=(2,"0")
|
||||
p a << 5 #=> [1, 2, "0", 5] a.<<(5)
|
||||
p a & [4,2] #=> [2] a.&([4,2])
|
||||
p "abcde"[1..3] #=> "bcd" "abcde".[](1..3)
|
||||
p "%2d %4s" % [1,"xyz"] #=> " 1 xyz" "%2d %4s".%([1,"xyz"])
|
||||
5
Task/Call-a-function/Ruby/call-a-function-2.rb
Normal file
5
Task/Call-a-function/Ruby/call-a-function-2.rb
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
def foo arg; p arg end # one argument
|
||||
|
||||
foo(1) #=> 1
|
||||
foo "1" #=> "1"
|
||||
foo [0,1,2] #=> [0, 1, 2] (one Array)
|
||||
6
Task/Call-a-function/Ruby/call-a-function-3.rb
Normal file
6
Task/Call-a-function/Ruby/call-a-function-3.rb
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
def foo(x=0, y=x, flag=true) p [x,y,flag] end
|
||||
|
||||
foo #=> [0, 0, true]
|
||||
foo(1) #=> [1, 1, true]
|
||||
foo(1,2) #=> [1, 2, true]
|
||||
foo 1,2,false #=> [1, 2, false]
|
||||
4
Task/Call-a-function/Ruby/call-a-function-4.rb
Normal file
4
Task/Call-a-function/Ruby/call-a-function-4.rb
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
def foo(*args) p args end
|
||||
|
||||
foo #=> []
|
||||
foo(1,2,3,4,5) #=> [1, 2, 3, 4, 5]
|
||||
3
Task/Call-a-function/Ruby/call-a-function-5.rb
Normal file
3
Task/Call-a-function/Ruby/call-a-function-5.rb
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
def foo(id:0, name:"", age:0) p [id, name, age] end
|
||||
|
||||
foo(age:22, name:"Tom") #=> [0, "Tom", 22]
|
||||
12
Task/Call-a-function/Ruby/call-a-function-6.rb
Normal file
12
Task/Call-a-function/Ruby/call-a-function-6.rb
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
def foo(a,b) a + b end
|
||||
|
||||
bar = foo 10,20
|
||||
p bar #=> 30
|
||||
p foo("abc","def") #=> "abcdef"
|
||||
|
||||
# return multiple values
|
||||
def sum_and_product(a,b) return a+b,a*b end
|
||||
|
||||
x,y = sum_and_product(3,5)
|
||||
p x #=> 8
|
||||
p y #=> 15
|
||||
12
Task/Call-a-function/Ruby/call-a-function-7.rb
Normal file
12
Task/Call-a-function/Ruby/call-a-function-7.rb
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
puts "OK!" # Kernel#puts
|
||||
raise "Error input" # Kernel#raise
|
||||
Integer("123") # Kernel#Integer
|
||||
rand(6) # Kernel#rand
|
||||
throw(:exit) # Kernel#throw
|
||||
|
||||
# method which can be seen like a reserved word.
|
||||
attr_accessor # Module#attr_accessor
|
||||
include # Module#include
|
||||
private # Module#private
|
||||
require # Kernel#require
|
||||
loop { } # Kernel#loop
|
||||
14
Task/Call-a-function/Ruby/call-a-function-8.rb
Normal file
14
Task/Call-a-function/Ruby/call-a-function-8.rb
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
class Array
|
||||
def sum(init=0, &blk)
|
||||
if blk
|
||||
inject(init){|s, n| s + blk.call(n)}
|
||||
else
|
||||
inject(init){|s, n| s + n}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
ary = [1,2,3,4,5]
|
||||
p ary.sum #=> 15
|
||||
p ary.sum(''){|n| (-n).to_s} #=> "-1-2-3-4-5"
|
||||
p (ary.sum do |n| n * n end) #=> 55
|
||||
6
Task/Call-a-function/Ruby/call-a-function-9.rb
Normal file
6
Task/Call-a-function/Ruby/call-a-function-9.rb
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
def foo(a,b,c) p [a,b,c] end
|
||||
|
||||
args = [1,2,3]
|
||||
foo *args #=> [1, 2, 3]
|
||||
args = [1,2]
|
||||
foo(0,*args) #=> [0, 1, 2]
|
||||
Loading…
Add table
Add a link
Reference in a new issue