Just another update

This commit is contained in:
Ingy döt Net 2015-02-20 00:35:01 -05:00
parent a25938f123
commit 00a190b0a6
6591 changed files with 94363 additions and 23227 deletions

View file

@ -1,4 +1,5 @@
Create a function, <span style="font-family:serif">compose</span>, whose two arguments <span style="font-family:serif">''f''</span> and <span style="font-family:serif">''g''</span>, are both functions with one argument. The result of <span style="font-family:serif">compose</span> is to be a function of one argument, (lets call the argument <span style="font-family:serif">''x''</span>), which works like applying function <span style="font-family:serif">''f''</span> to the result of applying function <span style="font-family:serif">''g''</span> to <span style="font-family:serif">''x''</span>, i.e,
Create a function, <span style="font-family:serif">compose</span>, whose two arguments <span style="font-family:serif">''f''</span> and <span style="font-family:serif">''g''</span>, are both functions with one argument.
The result of <span style="font-family:serif">compose</span> is to be a function of one argument, (lets call the argument <span style="font-family:serif">''x''</span>), which works like applying function <span style="font-family:serif">''f''</span> to the result of applying function <span style="font-family:serif">''g''</span> to <span style="font-family:serif">''x''</span>, i.e,
: <span style="font-family:serif">compose(''f'', ''g'') (''x'') = ''f''(''g''(''x''))</span>
Reference: [[wp:Function composition (computer science)|Function composition]]

View file

@ -0,0 +1,24 @@
-- Compose two functions where each function is
-- a script object with a call(x) handler.
on compose(f, g)
script
on call(x)
f's call(g's call(x))
end call
end script
end compose
script sqrt
on call(x)
x ^ 0.5
end call
end script
script twice
on call(x)
2 * x
end call
end script
compose(sqrt, twice)'s call(32)
-- Result: 8.0

View file

@ -1,9 +1,13 @@
#include <iostream>
#include <cmath>
#include <ext/functional>
#include <math.h>
template <class F, class G>
decltype(auto) compose(F&& f, G&& g)
{
return [=](auto x) { return f(g(x)); };
}
int main() {
std::cout << __gnu_cxx::compose1(std::ptr_fun(::sin), std::ptr_fun(::asin))(0.5) << std::endl;
std::cout << compose(sin, asin)(0.5) << "\n";
return 0;
}

View file

@ -0,0 +1,9 @@
#include <iostream>
#include <cmath>
#include <ext/functional>
int main() {
std::cout << __gnu_cxx::compose1(std::ptr_fun(::sin), std::ptr_fun(::asin))(0.5) << std::endl;
return 0;
}

View file

@ -6,8 +6,10 @@ mul2 = ( x ) -> x * 2
mulFirst = compose add2, mul2
addFirst = compose mul2, add2
multiple = compose mul2, compose add2, mul2
console.log "add2 2 #=> #{ add2 2 }"
console.log "mul2 2 #=> #{ mul2 2 }"
console.log "mulFirst 2 #=> #{ mulFirst 2 }"
console.log "addFirst 2 #=> #{ addFirst 2 }"
console.log "multiple 2 #=> #{ multiple 2 }"

View file

@ -0,0 +1,15 @@
Function::of = (f) -> (args...) => @ f args...
# Example
add2 = (x) -> x + 2
mul2 = (x) -> x * 2
mulFirst = add2.of mul2
addFirst = mul2.of add2
multiple = mul2.of add2.of mul2
console.log "add2 2 #=> #{ add2 2 }"
console.log "mul2 2 #=> #{ mul2 2 }"
console.log "mulFirst 2 #=> #{ mulFirst 2 }"
console.log "addFirst 2 #=> #{ addFirst 2 }"
console.log "multiple 2 #=> #{ multiple 2 }"

View file

@ -0,0 +1,7 @@
(defun compose (f g)
`(lambda (x) (,f (,g x))))
(let ((func (compose '1+ '1+)))
(funcall func 5))
=>
7

View file

@ -0,0 +1,7 @@
(defmacro compose (f g)
`(lambda (x) (,f (,g x))))
(let ((func (compose 1+ 1+)))
(funcall func 5))
=>
7

View file

@ -0,0 +1,11 @@
(eval-when-compile (require 'cl)) ;; for `lexical-let' macro
(defun compose (f g)
(lexical-let ((f f)
(g g))
(lambda (x)
(funcall f (funcall g x)))))
(let ((func (compose '1+ '1+)))
(funcall func 5))
=>
7

View file

@ -1,2 +1,3 @@
compose: procedure; parse arg f,g,x; interpret 'return' f"(" g'(' x "))"
exit /*control never gets here, but this was added just in case.*/

View file

@ -1,9 +1,137 @@
fn compose f g {
result @ {$g | $f}
#compose a new function consisting of the application of 2 unary functions
compose () { f="$1"; g="$2"; x="$3"; "$f" "$("$g" "$x")";}
chartolowervowel()
# Usage: chartolowervowel "A" --> "a"
#Based on a to_upper script in Chris F. A. Johnson's book Pro Bash Programming Ch7. String Manipulation
#(with minor tweaks to use local variables and return the value of the converted character
#http://cfajohnson.com/books/cfajohnson/pbp/
#highly recommended I have a copy and have bought another for a friend
{
local LWR="";
case $1 in
A*) _LWR=a ;;
# B*) _LWR=b ;;
# C*) _LWR=c ;;
# D*) _LWR=d ;;
E*) _LWR=e ;;
# F*) _LWR=f ;;
# G*) _LWR=g ;;
# H*) _LWR=h ;;
I*) _LWR=i ;;
# J*) _LWR=j ;;
# K*) _LWR=k ;;
# L*) _LWR=L ;;
# M*) _LWR=m ;;
# N*) _LWR=n ;;
O*) _LWR=o ;;
# P*) _LWR=p ;;
# Q*) _LWR=q ;;
# R*) _LWR=r ;;
# S*) _LWR=s ;;
# T*) _LWR=t ;;
U*) _LWR=u ;;
# V*) _LWR=v ;;
# W*) _LWR=w ;;
# X*) _LWR=x ;;
# Y*) _LWR=y ;;
# Z*) _LWR=z ;;
*) _LWR=${1%${1#?}} ;;
esac;
echo "$_LWR";
}
strdownvowel()
# Usage: strdownvowel "STRING" --> "STRiNG"
#Based on an upword script in Chris F. A. Johnson's book Pro Bash Programming Ch7. String Manipulation
#(with minor tweaks to use local variables and return the value of the converted string
#http://cfajohnson.com/books/cfajohnson/pbp/
#highly recommended I have a copy and have bought another for a friend
{
local _DWNWORD=""
local word="$1"
while [ -n "$word" ] ## loop until nothing is left in $word
do
chartolowervowel "$word" >> /dev/null
_DWNWORD=$_DWNWORD$_LWR
word=${word#?} ## remove the first character from $word
done
Echo "$_DWNWORD"
}
fn downvowel {tr AEIOU aeiou}
fn upcase {tr a-z A-Z}
fn-c = <={compose $fn-downvowel $fn-upcase}
echo 'Cozy lummox gives smart squid who asks for job pen.' | c
# => CoZY LuMMoX GiVeS SMaRT SQuiD WHo aSKS FoR JoB PeN.
chartoupper()
# Usage: chartoupper "s" --> "S"
#From Chris F. A. Johnson's book Pro Bash Programming Ch7. String Manipulation
#(with minor tweaks to use local variables and return the value of the converted character
#http://cfajohnson.com/books/cfajohnson/pbp/
#highly recommended I have a copy and have bought another for a friend
{
local UPR="";
case $1 in
a*) _UPR=A ;;
b*) _UPR=B ;;
c*) _UPR=C ;;
d*) _UPR=D ;;
e*) _UPR=E ;;
f*) _UPR=F ;;
g*) _UPR=G ;;
h*) _UPR=H ;;
i*) _UPR=I ;;
j*) _UPR=J ;;
k*) _UPR=K ;;
l*) _UPR=L ;;
m*) _UPR=M ;;
n*) _UPR=N ;;
o*) _UPR=O ;;
p*) _UPR=P ;;
q*) _UPR=Q ;;
r*) _UPR=R ;;
s*) _UPR=S ;;
t*) _UPR=T ;;
u*) _UPR=U ;;
v*) _UPR=V ;;
w*) _UPR=W ;;
x*) _UPR=X ;;
y*) _UPR=Y ;;
z*) _UPR=Z ;;
*) _UPR=${1%${1#?}} ;;
esac;
echo "$_UPR";
}
strupcase()
# Usage: strupcase "string" --> "STRING"
#Based on an upword script in Chris F. A. Johnson's book Pro Bash Programming Ch7. String Manipulation
#(with minor tweaks to use local variables and return the value of the converted string
#http://cfajohnson.com/books/cfajohnson/pbp/
#highly recommended I have a copy and have bought another for a friend
{
local _UPWORD=""
local word="$1"
while [ -n "$word" ] ## loop until nothing is left in $word
do
chartoupper "$word" >> /dev/null
_UPWORD=$_UPWORD$_UPR
word=${word#?} ## remove the first character from $word
done
Echo "$_UPWORD"
}
compose strdownvowel strupcase "Cozy lummox gives smart squid who asks for job pen."
# --> CoZY LuMMoX GiVeS SMaRT SQuiD WHo aSKS FoR JoB PeN.

View file

@ -1,9 +1,9 @@
fn compose f g {
result @ x {result <={$f <={$g $x}}}
result @ {$g | $f}
}
fn downvowel x {result `` '' {tr AEIOU aeiou <<< $x}}
fn upcase x {result `` '' {tr a-z A-Z <<< $x}}
fn downvowel {tr AEIOU aeiou}
fn upcase {tr a-z A-Z}
fn-c = <={compose $fn-downvowel $fn-upcase}
echo <={c 'Cozy lummox gives smart squid who asks for job pen.'}
echo 'Cozy lummox gives smart squid who asks for job pen.' | c
# => CoZY LuMMoX GiVeS SMaRT SQuiD WHo aSKS FoR JoB PeN.

View file

@ -0,0 +1,9 @@
fn compose f g {
result @ x {result <={$f <={$g $x}}}
}
fn downvowel x {result `` '' {tr AEIOU aeiou <<< $x}}
fn upcase x {result `` '' {tr a-z A-Z <<< $x}}
fn-c = <={compose $fn-downvowel $fn-upcase}
echo <={c 'Cozy lummox gives smart squid who asks for job pen.'}
# => CoZY LuMMoX GiVeS SMaRT SQuiD WHo aSKS FoR JoB PeN.