Another update from ingydotnet^djgoku

This commit is contained in:
Ingy döt Net 2015-11-18 06:14:39 +00:00
parent 91df62d461
commit 948b86eafa
7604 changed files with 108452 additions and 22726 deletions

View file

@ -0,0 +1 @@
aFunctionWithoutArguments$

View file

@ -0,0 +1 @@
aFunctionWithoutArguments'

View file

@ -0,0 +1 @@
func$!myargument;

View file

@ -0,0 +1 @@
(yourfunc=local vars.function body)

View file

@ -0,0 +1 @@
('$myfunc:(=?yourfunc))

View file

@ -0,0 +1 @@
myfunc$!myarg:?myresult

View file

@ -0,0 +1 @@
myfunc$!myarg&yourfunc$!yourarg

View file

@ -0,0 +1 @@
`(myfunc$!myarg)&yourfunc$!yourarg

View 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)
);

View file

@ -0,0 +1,2 @@
/* function with no arguments */
foo();

View file

@ -0,0 +1,5 @@
/* passing arguments by value*/
/* function with one argument */
bar(arg1);
/* function with multiple arguments */
baz(arg1, arg2);

View file

@ -0,0 +1,2 @@
/* get return value of a function */
variable = function(args);

View 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" */
}

View 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)

View file

@ -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

View file

@ -0,0 +1,4 @@
def foo() p "foo" end
foo #=> "foo"
foo() #=> "foo"

View 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"])

View 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)

View 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]

View file

@ -0,0 +1,4 @@
def foo(*args) p args end
foo #=> []
foo(1,2,3,4,5) #=> [1, 2, 3, 4, 5]

View 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]

View 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

View 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

View 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

View 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]