Functional Programming Lambda Expressions
I need to program a regex with lambda expressions for a university. I got two methods in a method.
here is my code:
static String ausdruck = "abcd";
public static Function<String, String> Char = (c) -> {
return (ausdruck.startsWith(c)) ? ausdruck = ausdruck.substring(1,
ausdruck.length()) : "Value Error";
};
public static BiFunction<Function<String, String>,
Function<String, String>,
Function<String, String>>
And = (f1, f2) -> {return null;};
what I want to do in the method And
: -> Char(Char.apply("a"))
I want to call a function f2
with a parameter f1
as a parameter. Call of the And Method should look like this:
And.apply(Char.apply("a"), Char.apply("b"));
source to share
If I understand the question correctly, you want to create a function that composes a new function that performs one function with the result of another function. The best way to do this in a lambda is to return a new lambda.
Try something like this:
BiFunction<Function<String, String>, Function<String, String>, Function<String, String>> compose =
(f1, f2) -> (a -> f2.apply(f1.apply(a)));
Example:
Function<String, String> upper = s -> s.toUpperCase();
Function<String, String> twice = s -> s + s;
Function<String, String> upperTwice = compose.apply(upper, twice);
System.out.println(upperTwice.apply("foo"));
Conclusion FOOFOO
.
Regarding your specific example
The method and method call should look like this:
And.apply(Char.apply("a"), Char.apply("b");
I don't know exactly what you are trying to do, but I don't think this will work, given the current implementation Char
. It sounds like you want to create a delete lambda a
with another to delete b
, but Char.apply("a")
won't create another function instead , but actually delete "a"
from your ausdruck
String! Instead, your lambda Char
should probably also return another lambda, and that lambda shouldn't change some variable static
, but accept and return another String parameter.
Function<String, Function<String, String>> stripChar =
c -> (s -> s.startsWith(c) ? s.substring(1) : "ERROR");
Function<String, String> stripAandC = compose.apply(stripChar.apply("c"), stripChar.apply("a"));
System.out.println(stripAandC.apply("cash"));
Conclusion sh
.
Finally, if you want to use this with something other than just String
, it makes sense to make it a compose
real method instead of a lambda so you can use generics. Alternatively, you can make it a little easier using andThen
:
public static <A, B, C> Function<A, C> compose(Function<A, B> f1, Function<B,C> f2){
return f1.andThen(f2);
}
source to share
I think this is what you want
Func<Str,Str> f = and( comsume("a"), consume("b") );
f.apply("abcd"); // "cd"
Func<Str,Str> consume(String x)
return input->{
if(input.startsWith(x)) return input.substring(x.length());
else throws new IllegalArgument()
};
Func<Str,Str> and(Fun<Str,Str> f1, Func<Str,Str> f2)
return input-> f2.apply(f1.apply(input))
and
not required, see Function.andThen
method
f = consume("a").andThen( consume("b) )
Unfortunately, no "curry"; otherwise we could do it
f = consume2.curry("a") .andThen ( consume2.curry("b") );
static BiFunc<Str,Str,Str> consume2 = (input,x)-> {...return input.substring(x.length()); ..
It is better if you create your own functional interfaces with necessary methods like curry.
interface F1
String apply(String);
F1 and(F1);
interface F2
String apply(String,String);
F1 curry(String);
source to share