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,17 @@
iex(1)> defmodule RC do
...(1)> def first(f), do: f.()
...(1)> def second, do: :hello
...(1)> end
{:module, RC,
<<70, 79, 82, 49, 0, 0, 4, 224, 66, 69, 65, 77, 69, 120, 68, 99, 0, 0, 0, 142,
131, 104, 2, 100, 0, 14, 101, 108, 105, 120, 105, 114, 95, 100, 111, 99, 115, 95
, 118, 49, 108, 0, 0, 0, 2, 104, 2, ...>>,
{:second, 0}}
iex(2)> RC.first(fn -> RC.second end)
:hello
iex(3)> RC.first(&RC.second/0) # Another expression
:hello
iex(4)> f = fn -> :world end # Anonymous function
#Function<20.54118792/0 in :erl_eval.expr/5>
iex(5)> RC.first(f)
:world

View file

@ -0,0 +1,4 @@
cmpFunc = {|a,b| length[a] <=> length[b]}
a = ["tree", "apple", "bee", "monkey", "z"]
sort[a, cmpFunc]

View file

@ -0,0 +1,5 @@
lengthCompare[a,b] := length[a] <=> length[b]
func = getFunction["lengthCompare", 2]
a = ["tree", "apple", "bee", "monkey", "z"]
sort[a, func]

View file

@ -11,6 +11,14 @@
+/\. 3 1 4 1 5 9 NB. sum suffix
23 20 19 15 14 9
2&% 1 2 3 NB. divide 2 by
2 1 0.666667
%&2 (1 2 3) NB. divide by 2 (need parenthesis to break up list formation)
0.5 1 1.5
-: 1 2 3 NB. but divide by 2 happens a lot so it's a primitive
0.5 1 1.5
f=: -:@(+ 2&%) NB. one Newton iteration
f 1
1.5

View file

@ -0,0 +1,6 @@
function f ($y) {
$y*$y
}
function g (${function:f}, $y) {
(f $y)
}

View file

@ -0,0 +1,6 @@
function g2($y) {
function f2($y) {
$y*$y
}
(f2 $y)
}

View file

@ -0,0 +1,2 @@
g f 5
g2 9

View file

@ -1,4 +1,4 @@
first(Predicate):-Predicate.
second(Argument):-print(Argument).
first(Predicate) :- call(Predicate).
second(Argument) :- write(Argument).
:-first(second('Hello World!')).

View file

@ -0,0 +1,6 @@
q)sayHi:{-1"Hello ",x;}
q)callFuncWithParam:{x["Peter"]}
q)callFuncWithParam sayHi
Hello Peter
q)callFuncWithParam[sayHi]
Hello Peter

View file

@ -1,21 +1,20 @@
/*REXX program demonstrates passing a function as a name to a function.*/
n=3735928559
funcName='fib' ; q= 10; call someFunk funcName, q; call tell
funcName='fact' ; q= 6; call someFunk funcName, q; call tell
funcName='square' ; q= 13; call someFunk funcName, q; call tell
funcName='cube' ; q= 3; call someFunk funcName, q; call tell
q=721; call someFunk 'reverse',q; call tell
say copies('',30) /*display a nice separator fence.*/
say 'done as' d2x(n)"." /*prove that var N still intact. */
exit /*stick a fork in it, we're done.*/
/*──────────────────────────────────subroutines─────────────────────────*/
/*REXX program demonstrates passing a function as a name to another function. */
n=3735928559
funcName = 'fib' ; q= 10; call someFunk funcName, q; call tell
funcName = 'fact' ; q= 6; call someFunk funcName, q; call tell
funcName = 'square' ; q= 13; call someFunk funcName, q; call tell
funcName = 'cube' ; q= 3; call someFunk funcName, q; call tell
q=721; call someFunk 'reverse',q; call tell
say copies('', 30) /*display a nice separator fence */
say 'done as' d2x(n)"." /*prove that variable N is still intact*/
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────one─liner subroutines─────────────────────*/
cube: return n**3
fact: !=1; do j=2 to n; !=!*j; end; return !
fact: !=1; do j=2 to n; !=!*j; end; return !
reverse: return 'REVERSE'(n)
someFunk: procedure; arg ?,n; signal value (?); say result 'result'; return
someFunk: procedure; arg ?,n; signal value (?); say result 'result'; return
square: return n**2
tell: say right(funcName'('q") = ",20) result; return
fib: if n==0 | n==1 then return n; _=0; a=0; b=1
do j=2 to n; _=a+b; a=b; b=_; end; return _
tell: say right(funcName'('q") = ",20) result; return
/*────────────────────────────────────────────────────────────────────────────*/
fib: if n==0 | n==1 then return n; _=0; a=0; b=1
do j=2 to n; _=a+b; a=b; b=_; end; return _

View file

@ -0,0 +1,12 @@
fn execute_with_10<F: Fn(u64) -> u64> (f: F) -> u64 {
f(10)
}
fn square(n: u64) -> u64 {
n*n
}
fn main() {
println!("{}", execute_with_10(|n| n*n )); // closure
println!("{}", execute_with_10(square)); // function
}