functional-programming

How to implement the Elvis operator in Java 8?

百般思念 提交于 2020-12-30 05:16:51
问题 I have the classic "Elvis operator" case, where I'm calling methods that each may return null and chaining them together: thing?:nullableMethod1(a)?:nullableMethod2(b)?:nullableMethod3() In Java 8, the most faithful implementation I've found is something like this: return Optional.ofNullable(thing) .flatMap(x -> Optional.ofNullable(x.nullableMethod1(a))) .flatMap(y -> Optional.ofNullable(y.nullableMethod2(b))) .flatMap(z -> Optional.ofNullable(z.nullableMethod3())) I wish that Java's Optional

How to implement the Elvis operator in Java 8?

可紊 提交于 2020-12-30 05:14:08
问题 I have the classic "Elvis operator" case, where I'm calling methods that each may return null and chaining them together: thing?:nullableMethod1(a)?:nullableMethod2(b)?:nullableMethod3() In Java 8, the most faithful implementation I've found is something like this: return Optional.ofNullable(thing) .flatMap(x -> Optional.ofNullable(x.nullableMethod1(a))) .flatMap(y -> Optional.ofNullable(y.nullableMethod2(b))) .flatMap(z -> Optional.ofNullable(z.nullableMethod3())) I wish that Java's Optional

How to implement the Elvis operator in Java 8?

对着背影说爱祢 提交于 2020-12-30 05:14:03
问题 I have the classic "Elvis operator" case, where I'm calling methods that each may return null and chaining them together: thing?:nullableMethod1(a)?:nullableMethod2(b)?:nullableMethod3() In Java 8, the most faithful implementation I've found is something like this: return Optional.ofNullable(thing) .flatMap(x -> Optional.ofNullable(x.nullableMethod1(a))) .flatMap(y -> Optional.ofNullable(y.nullableMethod2(b))) .flatMap(z -> Optional.ofNullable(z.nullableMethod3())) I wish that Java's Optional

Is mutating accumulator in reduce function considered bad practice?

不问归期 提交于 2020-12-29 12:22:25
问题 I'm new to functional programming and I'm trying rewrite some code to make it more functional-ish to grasp the concepts. Just now I've discovered Array.reduce() function and used it to create an object of arrays of combinations (I've used for loop before that). However, I'm not sure about something. Look at this code: const sortedCombinations = combinations.reduce( (accum, comb) => { if(accum[comb.strength]) { accum[comb.strength].push(comb); } else { accum[comb.strength] = [comb]; } return

Reduce with extra arguments to the function in R [duplicate]

血红的双手。 提交于 2020-12-29 06:10:37
问题 This question already has answers here : How to write the dataframes in a list to a single csv file (2 answers) Closed 4 years ago . I'm trying to use the Reduce function in R to use the merge function across multiple dataframes. The problem is, I would like to use the merge function with the argument all=T , and there seems to be nowhere to specify this in the higher-order Reduce function. So I'd like: a <- data.frame(id=c(1, 2, 3, 4), a=c('a', 'b', 'c', 'd')) b <- data.frame(id=c(1, 2, 5, 6

Reduce with extra arguments to the function in R [duplicate]

吃可爱长大的小学妹 提交于 2020-12-29 06:09:31
问题 This question already has answers here : How to write the dataframes in a list to a single csv file (2 answers) Closed 4 years ago . I'm trying to use the Reduce function in R to use the merge function across multiple dataframes. The problem is, I would like to use the merge function with the argument all=T , and there seems to be nowhere to specify this in the higher-order Reduce function. So I'd like: a <- data.frame(id=c(1, 2, 3, 4), a=c('a', 'b', 'c', 'd')) b <- data.frame(id=c(1, 2, 5, 6

Reduce with extra arguments to the function in R [duplicate]

天涯浪子 提交于 2020-12-29 06:05:49
问题 This question already has answers here : How to write the dataframes in a list to a single csv file (2 answers) Closed 4 years ago . I'm trying to use the Reduce function in R to use the merge function across multiple dataframes. The problem is, I would like to use the merge function with the argument all=T , and there seems to be nowhere to specify this in the higher-order Reduce function. So I'd like: a <- data.frame(id=c(1, 2, 3, 4), a=c('a', 'b', 'c', 'd')) b <- data.frame(id=c(1, 2, 5, 6

How to view higher-order functions and IO-actions from a mathematical perspective?

左心房为你撑大大i 提交于 2020-12-26 04:03:51
问题 I am trying to understand functional programming from first principles, yet I am stuck on the interface between the pure functional world and the impure real world that has state and side effects. From a mathematical perspective, what is a function that returns a function? what is a function that returns an IO action (like Haskell's IO type)? To elaborate: In my understanding, a pure function is a map from domain to co-domain. Ultimately, it is a map from some values in computer memory to

How to view higher-order functions and IO-actions from a mathematical perspective?

别等时光非礼了梦想. 提交于 2020-12-26 04:00:09
问题 I am trying to understand functional programming from first principles, yet I am stuck on the interface between the pure functional world and the impure real world that has state and side effects. From a mathematical perspective, what is a function that returns a function? what is a function that returns an IO action (like Haskell's IO type)? To elaborate: In my understanding, a pure function is a map from domain to co-domain. Ultimately, it is a map from some values in computer memory to

Scala compile time error: No implicits found for parameter evidence$2: BodyWritable[Map[String, Object]]

五迷三道 提交于 2020-12-15 04:28:10
问题 I am working on scala play framework application. I am trying to call a web service API which takes request payload data as follows { "toID": [ "email1@email.com", "email2@email.com" ], "fromID": "info@test.com", "userID": "ervd12fdsfksdjnfn9832rbjfdsnf", "mailContent": "Dear Sir, ..." } And for this I am using following code ws.url(Utils.messengerServiceUrl + "service/email") .post( Map("userID" -> userID, "mailContent" -> userData.message, "fromID" -> "info@test.com", "toID" -> userData