Another update from ingydotnet^djgoku
This commit is contained in:
parent
91df62d461
commit
948b86eafa
7604 changed files with 108452 additions and 22726 deletions
11
Task/Function-composition/Elixir/function-composition.elixir
Normal file
11
Task/Function-composition/Elixir/function-composition.elixir
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
defmodule RC do
|
||||
def compose(f, g), do: fn(x) -> f.(g.(x)) end
|
||||
|
||||
def multicompose(fs), do: List.foldl(fs, fn(x) -> x end, &compose/2)
|
||||
end
|
||||
|
||||
sin_asin = RC.compose(&:math.sin/1, &:math.asin/1)
|
||||
IO.puts sin_asin.(0.5)
|
||||
|
||||
IO.puts RC.multicompose([&:math.sin/1, &:math.asin/1, fn x->1/x end]).(0.5)
|
||||
IO.puts RC.multicompose([&(&1*&1), &(1/&1), &(&1*&1)]).(0.5)
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
final times2 = { it * 2 }
|
||||
final plus1 = { it + 1 }
|
||||
|
||||
final plus1_then_times2 = times2 << plus1
|
||||
final times2_then_plus1 = times2 >> plus1
|
||||
|
||||
assert plus1_then_times2(3) == 8
|
||||
assert times2_then_plus1(3) == 7
|
||||
|
|
@ -1,12 +1,8 @@
|
|||
import java.util.function.Function;
|
||||
|
||||
public class Compose {
|
||||
public static <A,B,C> Function<A,C> compose(Function<B,C> f, Function<A,B> g) {
|
||||
return x -> f.apply(g.apply(x));
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
Function<Double,Double> sin_asin = compose(Math::sin, Math::asin);
|
||||
Function<Double,Double> sin_asin = ((Function<Double,Double>)Math::sin).compose(Math::asin);
|
||||
|
||||
System.out.println(sin_asin.apply(0.5)); // prints "0.5"
|
||||
}
|
||||
|
|
|
|||
13
Task/Function-composition/Java/function-composition-3.java
Normal file
13
Task/Function-composition/Java/function-composition-3.java
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
import java.util.function.Function;
|
||||
|
||||
public class Compose {
|
||||
public static <A,B,C> Function<A,C> compose(Function<B,C> f, Function<A,B> g) {
|
||||
return x -> f.apply(g.apply(x));
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
Function<Double,Double> sin_asin = compose(Math::sin, Math::asin);
|
||||
|
||||
System.out.println(sin_asin.apply(0.5)); // prints "0.5"
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
sub triple($n) { 3 * $n }
|
||||
my &f = &triple ∘ &prefix:<-> ∘ { $^n + 2 };
|
||||
say &f(5); # Prints "-21".
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
function g ($x) {
|
||||
$x + $x
|
||||
}
|
||||
function f ($x) {
|
||||
$x*$x*$x
|
||||
}
|
||||
f (g 1)
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
function fg (${function:f}, ${function:g}, $x) {
|
||||
f (g $x)
|
||||
}
|
||||
fg f g 1
|
||||
Loading…
Add table
Add a link
Reference in a new issue