Just another update
This commit is contained in:
parent
a25938f123
commit
00a190b0a6
6591 changed files with 94363 additions and 23227 deletions
|
|
@ -0,0 +1,27 @@
|
|||
( ( fs
|
||||
=
|
||||
. '$!arg:?arg
|
||||
&
|
||||
' ( first r
|
||||
. :?r
|
||||
& whl
|
||||
' ( !arg:%?first ?arg
|
||||
& !r ($arg)$!first:?r
|
||||
)
|
||||
& !r
|
||||
)
|
||||
)
|
||||
& ( partial
|
||||
=
|
||||
. !arg:(?f.?g)
|
||||
& /('(x./('(y.($x)$($y)))$!g))$!f
|
||||
)
|
||||
& (f1=.2*!arg)
|
||||
& (f2=.!arg^2)
|
||||
& partial$(fs.f1):(=?fsf1)
|
||||
& partial$(fs.f2):(=?fsf2)
|
||||
& out$(fsf1$(0 1 2 3))
|
||||
& out$(fsf2$(0 1 2 3))
|
||||
& out$(fsf1$(2 4 6 8))
|
||||
& out$(fsf2$(2 4 6 8))
|
||||
);
|
||||
|
|
@ -1,12 +1,20 @@
|
|||
(defun fs (f s) (mapcar f s))
|
||||
(defun f1 (i) (* i 2))
|
||||
(defun f2 (i) (expt i 2))
|
||||
(defun fs (f s)
|
||||
(mapcar f s))
|
||||
(defun f1 (i)
|
||||
(* i 2))
|
||||
(defun f2 (i)
|
||||
(expt i 2))
|
||||
|
||||
(defun partial (func &rest args1)
|
||||
(lambda (&rest args2) (apply func (append args1 args2))))
|
||||
(defvar fsf1 (partial #'fs #'f1))
|
||||
(defvar fsf2 (partial #'fs #'f2))
|
||||
(lambda (&rest args2)
|
||||
(apply func (append args1 args2))))
|
||||
|
||||
(setf (symbol-function 'fsf1) (partial #'fs #'f1))
|
||||
(setf (symbol-function 'fsf2) (partial #'fs #'f2))
|
||||
|
||||
(dolist (seq '((0 1 2 3) (2 4 6 8)))
|
||||
(format t "~%seq: ~A~% fsf1 seq: ~A~% fsf2 seq: ~A"
|
||||
seq (funcall fsf1 seq) (funcall fsf2 seq)))
|
||||
(format t
|
||||
"~%seq: ~A~% fsf1 seq: ~A~% fsf2 seq: ~A"
|
||||
seq
|
||||
(fsf1 seq)
|
||||
(fsf2 seq)))
|
||||
|
|
|
|||
|
|
@ -0,0 +1,55 @@
|
|||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
// Using a method bound to a function type:
|
||||
|
||||
// fn is a simple function taking an integer and returning another.
|
||||
type fn func(int) int
|
||||
|
||||
// fs applies fn to each argument returning all results.
|
||||
func (f fn) fs(s ...int) (r []int) {
|
||||
for _, i := range s {
|
||||
r = append(r, f(i))
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
||||
// Two simple functions for demonstration.
|
||||
func f1(i int) int { return i * 2 }
|
||||
func f2(i int) int { return i * i }
|
||||
|
||||
// Another way:
|
||||
|
||||
// addn returns a function that adds n to a sequence of numbers
|
||||
func addn(n int) func(...int) []int {
|
||||
return func(s ...int) []int {
|
||||
var r []int
|
||||
for _, i := range s {
|
||||
r = append(r, n+i)
|
||||
}
|
||||
return r
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
// Turning a method into a function bound to it's reciever:
|
||||
fsf1 := fn(f1).fs
|
||||
fsf2 := fn(f2).fs
|
||||
// Or using a function that returns a function:
|
||||
fsf3 := addn(100)
|
||||
|
||||
s := []int{0, 1, 2, 3}
|
||||
fmt.Println("For s =", s)
|
||||
fmt.Println(" fsf1:", fsf1(s...)) // Called with a slice
|
||||
fmt.Println(" fsf2:", fsf2(0, 1, 2, 3)) // ... or with individual arguments
|
||||
fmt.Println(" fsf3:", fsf3(0, 1, 2, 3))
|
||||
fmt.Println(" fsf2(fsf1):", fsf2(fsf1(s...)...))
|
||||
|
||||
s = []int{2, 4, 6, 8}
|
||||
fmt.Println("For s =", s)
|
||||
fmt.Println(" fsf1:", fsf1(2, 4, 6, 8))
|
||||
fmt.Println(" fsf2:", fsf2(s...))
|
||||
fmt.Println(" fsf3:", fsf3(s...))
|
||||
fmt.Println(" fsf3(fsf1):", fsf3(fsf1(s...)...))
|
||||
}
|
||||
|
|
@ -0,0 +1,30 @@
|
|||
function map(f, ...)
|
||||
local t = {}
|
||||
for k, v in ipairs(...) do
|
||||
t[#t+1] = f(v)
|
||||
end
|
||||
return t
|
||||
end
|
||||
|
||||
function timestwo(n)
|
||||
return n * 2
|
||||
end
|
||||
|
||||
function squared(n)
|
||||
return n ^ 2
|
||||
end
|
||||
|
||||
function partial(f, ...)
|
||||
local args = ...
|
||||
return function(...)
|
||||
return f(args, ...)
|
||||
end
|
||||
end
|
||||
|
||||
timestwo_s = partial(map, timestwo)
|
||||
squared_s = partial(map, squared)
|
||||
|
||||
print(table.concat(timestwo_s{0, 1, 2, 3}, ', '))
|
||||
print(table.concat(squared_s{0, 1, 2, 3}, ', '))
|
||||
print(table.concat(timestwo_s{2, 4, 6, 8}, ', '))
|
||||
print(table.concat(squared_s{2, 4, 6, 8}, ', '))
|
||||
|
|
@ -1,11 +1,10 @@
|
|||
def fs(f:Int=>Int, s:List[Int])=s map f
|
||||
def f1(x:Int)=x*2
|
||||
def f2(x:Int)=x*x
|
||||
|
||||
def fsf1=fs(f1,_:List[Int])
|
||||
def fsf2=fs(f2,_:List[Int])
|
||||
def fs[X](f:X=>X)(s:Seq[X]) = s map f
|
||||
def f1(x:Int) = x * 2
|
||||
def f2(x:Int) = x * x
|
||||
|
||||
println(fsf1(List(0,1,2,3)))
|
||||
println(fsf1(List(2,4,6,8)))
|
||||
println(fsf2(List(0,1,2,3)))
|
||||
println(fsf2(List(2,4,6,8)))
|
||||
def fsf[X](f:X=>X) = fs(f) _
|
||||
val fsf1 = fsf(f1) // or without the fsf intermediary: val fsf1 = fs(f1) _
|
||||
val fsf2 = fsf(f2) // or without the fsf intermediary: val fsf2 = fs(f2) _
|
||||
|
||||
assert(fsf1(List(0,1,2,3)) == List(0,2,4,6))
|
||||
assert(fsf2(List(0,1,2,3)) == List(0,1,4,9))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue