2016 Update
This commit is contained in:
parent
948b86eafa
commit
dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions
|
|
@ -1,3 +1,7 @@
|
|||
;Task:
|
||||
Pass a function ''as an argument'' to another function.
|
||||
|
||||
C.f. [[First-class functions]]
|
||||
|
||||
;Related task:
|
||||
* [[First-class functions]]
|
||||
<br><br>
|
||||
|
|
|
|||
|
|
@ -1,25 +1,25 @@
|
|||
-- This handler takes a script object (singer)
|
||||
-- with another handler (call).
|
||||
on sing about topic by singer
|
||||
call of singer for "Of " & topic & " I sing"
|
||||
call of singer for "Of " & topic & " I sing"
|
||||
end sing
|
||||
|
||||
-- Define a handler in a script object,
|
||||
-- then pass the script object.
|
||||
script cellos
|
||||
on call for what
|
||||
say what using "Cellos"
|
||||
end call
|
||||
on call for what
|
||||
say what using "Cellos"
|
||||
end call
|
||||
end script
|
||||
sing about "functional programming" by cellos
|
||||
|
||||
-- Pass a different handler. This one is a closure
|
||||
-- that uses a variable (voice) from its context.
|
||||
on hire for voice
|
||||
script
|
||||
on call for what
|
||||
say what using voice
|
||||
end call
|
||||
end script
|
||||
script
|
||||
on call for what
|
||||
say what using voice
|
||||
end call
|
||||
end script
|
||||
end hire
|
||||
sing about "closures" by (hire for "Pipe Organ")
|
||||
|
|
@ -0,0 +1,116 @@
|
|||
on run
|
||||
-- PASSING FUNCTIONS AS ARGUMENTS TO
|
||||
-- MAP, FOLD/REDUCE, AND FILTER, ACROSS A LIST
|
||||
|
||||
set lstRange to {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
|
||||
|
||||
map(squared, lstRange)
|
||||
--> {0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100}
|
||||
|
||||
foldl(summed, 0, map(squared, lstRange))
|
||||
--> 385
|
||||
|
||||
filter(isEven, lstRange)
|
||||
--> {0, 2, 4, 6, 8, 10}
|
||||
|
||||
|
||||
-- OR MAPPING OVER A LIST OF FUNCTIONS
|
||||
|
||||
map(testFunction, {doubled, squared, isEven})
|
||||
|
||||
--> {{0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20},
|
||||
-- {0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100},
|
||||
-- {true, false, true, false, true, false, true, false, true, false, true}}
|
||||
end run
|
||||
|
||||
-- testFunction :: (a -> b) -> [b]
|
||||
on testFunction(f)
|
||||
map(f, {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10})
|
||||
end testFunction
|
||||
|
||||
|
||||
-- MAP, REDUCE, FILTER
|
||||
|
||||
-- Returns a new list consisting of the results of applying the
|
||||
-- provided function to each element of the first list
|
||||
-- map :: (a -> b) -> [a] -> [b]
|
||||
on map(f, xs)
|
||||
tell mReturn(f)
|
||||
set lng to length of xs
|
||||
set lst to {}
|
||||
repeat with i from 1 to lng
|
||||
set end of lst to lambda(item i of xs, i, xs)
|
||||
end repeat
|
||||
return lst
|
||||
end tell
|
||||
end map
|
||||
|
||||
-- Applies a function against an accumulator and
|
||||
-- each list element (from left-to-right) to reduce it
|
||||
-- to a single return value
|
||||
|
||||
-- In some languages, like JavaScript, this is called reduce()
|
||||
|
||||
-- Arguments: function, initial value of accumulator, list
|
||||
-- foldl :: (a -> b -> a) -> a -> [b] -> a
|
||||
on foldl(f, startValue, xs)
|
||||
tell mReturn(f)
|
||||
set v to startValue
|
||||
set lng to length of xs
|
||||
repeat with i from 1 to lng
|
||||
set v to lambda(v, item i of xs, i, xs)
|
||||
end repeat
|
||||
return v
|
||||
end tell
|
||||
end foldl
|
||||
|
||||
|
||||
-- Sublist of those elements for which the predicate
|
||||
-- function returns true
|
||||
-- filter :: (a -> Bool) -> [a] -> [a]
|
||||
-- filter :: (a -> Bool) -> [a] -> [a]
|
||||
on filter(f, xs)
|
||||
tell mReturn(f)
|
||||
set lst to {}
|
||||
set lng to length of xs
|
||||
repeat with i from 1 to lng
|
||||
set v to item i of xs
|
||||
if lambda(v, i, xs) then set end of lst to v
|
||||
end repeat
|
||||
return lst
|
||||
end tell
|
||||
end filter
|
||||
|
||||
-- Lift 2nd class handler function into 1st class script wrapper
|
||||
-- mReturn :: Handler -> Script
|
||||
on mReturn(f)
|
||||
if class of f is script then
|
||||
f
|
||||
else
|
||||
script
|
||||
property lambda : f
|
||||
end script
|
||||
end if
|
||||
end mReturn
|
||||
|
||||
-- HANDLER FUNCTIONS TO BE PASSED AS ARGUMENTS
|
||||
|
||||
-- squared :: Number -> Number
|
||||
on squared(x)
|
||||
x * x
|
||||
end squared
|
||||
|
||||
-- doubled :: Number -> Number
|
||||
on doubled(x)
|
||||
x * 2
|
||||
end doubled
|
||||
|
||||
-- summed :: Number -> Number -> Number
|
||||
on summed(a, b)
|
||||
a + b
|
||||
end summed
|
||||
|
||||
-- isEven :: Int -> Bool
|
||||
on isEven(x)
|
||||
x mod 2 = 0
|
||||
end isEven
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
{{0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20},
|
||||
{0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100},
|
||||
{true, false, true, false, true, false, true, false, true, false, true}}
|
||||
|
|
@ -1,9 +1,15 @@
|
|||
f(x) {
|
||||
return x
|
||||
return "This " . x
|
||||
}
|
||||
g(x, y) {
|
||||
msgbox %x%
|
||||
msgbox %y%
|
||||
|
||||
g(x) {
|
||||
return "That " . x
|
||||
}
|
||||
g(f("RC Function as an Argument AHK implementation"), "Non-function argument")
|
||||
|
||||
show(fun) {
|
||||
msgbox % %fun%("works")
|
||||
}
|
||||
|
||||
show(Func("f")) ; either create a Func object
|
||||
show("g") ; or just name the function
|
||||
return
|
||||
|
|
|
|||
|
|
@ -0,0 +1,19 @@
|
|||
public class ListenerTest {
|
||||
public static void main(String[] args) {
|
||||
JButton testButton = new JButton("Test Button");
|
||||
testButton.addActionListener(new ActionListener(){
|
||||
@Override public void actionPerformed(ActionEvent ae){
|
||||
System.out.println("Click Detected by Anon Class");
|
||||
}
|
||||
});
|
||||
|
||||
testButton.addActionListener(e -> System.out.println("Click Detected by Lambda Listner"));
|
||||
|
||||
// Swing stuff
|
||||
JFrame frame = new JFrame("Listener Test");
|
||||
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
frame.add(testButton, BorderLayout.CENTER);
|
||||
frame.pack();
|
||||
frame.setVisible(true);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
fun main(args: Array<String>) {
|
||||
val list = listOf(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0)
|
||||
var a = list.map({ x -> x + 2 }).average()
|
||||
var h = list.map({ x -> x * x }).average()
|
||||
var g = list.map({ x -> x * x * x }).average()
|
||||
println("A = %f G = %f H = %f".format(a, g, h))
|
||||
}
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
inline fun higherOrderFunction(x: Int, y: Int, function: (Int, Int) -> Int) = function(x, y)
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
val result = higherOrderFunction(3, 5) { x, y -> x + y }
|
||||
println(result)
|
||||
}
|
||||
|
|
@ -1,20 +1,18 @@
|
|||
/*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─────────────────────*/
|
||||
/*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. */
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
cube: return n**3
|
||||
fact: !=1; do j=2 to n; !=!*j; end; return !
|
||||
fact: !=1; do j=2 to n; !=!*j; end; return !
|
||||
fib: if n<2 then return n; _=0; a=0; b=1; do j=2 to n; _=a+b; a=b; b=_; 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
|
||||
|
|
|
|||
|
|
@ -0,0 +1,2 @@
|
|||
f = { |x, y| x.(y) }; // a function that takes a function and calls it with an argument
|
||||
f.({ |x| x + 1 }, 5); // returns 5
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
10 DEF FN f(f$,x,y)=VAL ("FN "+f$+"("+STR$ (x)+","+STR$ (y)+")")
|
||||
20 DEF FN n(x,y)=(x+y)^2
|
||||
30 PRINT FN f("n",10,11)
|
||||
Loading…
Add table
Add a link
Reference in a new issue