java - Lambda Expressions functional programming -


i have programm regular expression lambda expressions university. got stuck 2 methods in method.

here 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>>                  , = (f1, f2) -> {return null;}; 

what want in and method is: char(char.apply("a")) -> want call function f2 f1 parameter. call of , method have like:

and.apply(char.apply("a"), char.apply("b")); 

if understand question correctly, want create function compones new function, executing 1 function result of function. best way in lambda return new lambda.

try 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")); 

output foofoo.


concerning concrete example

the call of , method have like: and.apply(char.apply("a"), char.apply("b");

i not know trying do, don't think work, given current implementation of char. seems want compose lambda remove a remove b, instead char.apply("a") not create function, remove "a" ausdruck string! instead, char lambda should return lambda, , lambda should not modify static variable, take , return 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")); 

output sh.


finally, in case want use other string, might make sense make compose actual method instead of lambda, can make use of generics. also, can make bit simpler using andthen:

public static <a, b, c> function<a, c> compose(function<a, b> f1, function<b,c> f2){     return f1.andthen(f2); } 

Comments

Popular posts from this blog

facebook - android ACTION_SEND to share with specific application only -

python - Creating a new virtualenv gives a permissions error -

javascript - cocos2d-js draw circle not instantly -