functional-programming

How to fork and merge monads?

心不动则不痛 提交于 2020-02-28 17:26:49
问题 I am learning monads and doing my first "login" case. The steps are simple: User input userName and password; From database, get user by userName, and get its password saved in database; compare the input password with the one in the database. From the above, there is a requirements to fork 2 monads and merge them to compare. I read nearly all javascripts FP books and monads implementations I can think of, but still cannot found any solutions. The below is what I got so far, it works, but it

Designing React Hooks prevent react-hooks/exhaustive-deps warning

折月煮酒 提交于 2020-02-25 04:13:50
问题 I am designing a hook to fetch data only when the hooks dependencies change. It works as expected but I receive the linter warnings: React Hook useEffect was passed a dependency list that is not an array literal. This means we can't statically verify whether you've passed the correct dependencies. and React Hook useEffect has missing dependencies: 'data', 'errorHandler', 'route', and 'successHandler'. Either include them or remove the dependency array. If 'successHandler' changes too often,

Designing React Hooks prevent react-hooks/exhaustive-deps warning

烂漫一生 提交于 2020-02-25 04:13:25
问题 I am designing a hook to fetch data only when the hooks dependencies change. It works as expected but I receive the linter warnings: React Hook useEffect was passed a dependency list that is not an array literal. This means we can't statically verify whether you've passed the correct dependencies. and React Hook useEffect has missing dependencies: 'data', 'errorHandler', 'route', and 'successHandler'. Either include them or remove the dependency array. If 'successHandler' changes too often,

Return generic functional interface in Java 8

痞子三分冷 提交于 2020-02-24 04:40:20
问题 I want to write kind of function factory. It should be a function, which is called once which different strategies as parameters. It should return a function, which selects one of this strategies dependent on the parameter, which is to be fulfilled by a predicate. Well, better look at condition3 for better understanding. The problem is, that it is not compiling. I think because the compiler can't figure out, that the functional interface H can be realized by the implementation. Without

Javascript Nested for loop with ES6

时光总嘲笑我的痴心妄想 提交于 2020-02-23 04:02:23
问题 I guys, I have a nested for loop but I want to do it with an Array map/ES6 way but how does that work with nested forloops? for (var i = 0; i < enemy.ships.length; i++) { for (var n = 0; n < enemy.ships[i].location.length; n++) { if (obj.coordination == enemy.ships[i].location[n]) hit = true; } }; I know how to do it when it as not a forloop players.map(function(player){if(player.id != socket.id) return enemy = player}); But I can't seem to understand how it should be with Array Maps or

Why don't purely functional languages use reference counting?

北战南征 提交于 2020-02-17 07:01:33
问题 In purely functional languages, data is immutable. With reference counting, creating a reference cycle requires changing already created data. It seems like purely functional languages could use reference counting without worrying about the possibility of cycles. Am is right? If so, why don't they? I understand that reference counting is slower than GC in many cases, but at least it reduces pause times. It would be nice to have the option to use reference counting in cases where pause times

Why don't purely functional languages use reference counting?

百般思念 提交于 2020-02-17 06:56:55
问题 In purely functional languages, data is immutable. With reference counting, creating a reference cycle requires changing already created data. It seems like purely functional languages could use reference counting without worrying about the possibility of cycles. Am is right? If so, why don't they? I understand that reference counting is slower than GC in many cases, but at least it reduces pause times. It would be nice to have the option to use reference counting in cases where pause times

How to reduce code duplication when dealing with recursive sum types

好久不见. 提交于 2020-02-17 05:16:34
问题 I am currently working on a simple interpreter for a programming language and I have a data type like this: data Expr = Variable String | Number Int | Add [Expr] | Sub Expr Expr And I have many functions that do simple things like: -- Substitute a value for a variable substituteName :: String -> Int -> Expr -> Expr substituteName name newValue = go where go (Variable x) | x == name = Number newValue go (Add xs) = Add $ map go xs go (Sub x y) = Sub (go x) (go y) go other = other -- Replace

How to reduce code duplication when dealing with recursive sum types

百般思念 提交于 2020-02-17 05:15:07
问题 I am currently working on a simple interpreter for a programming language and I have a data type like this: data Expr = Variable String | Number Int | Add [Expr] | Sub Expr Expr And I have many functions that do simple things like: -- Substitute a value for a variable substituteName :: String -> Int -> Expr -> Expr substituteName name newValue = go where go (Variable x) | x == name = Number newValue go (Add xs) = Add $ map go xs go (Sub x y) = Sub (go x) (go y) go other = other -- Replace

Where's the code for a lambda located in a java class file?

妖精的绣舞 提交于 2020-02-16 08:14:42
问题 I've this java source file: import java.util.function.*; public class t { public static void main(String[] args) { Function<Integer,Integer> r = (a) -> a*a+2*a+1; System.out.println(r.apply(2)); } } I compile it and it works as expected. Here's the output of javap -c -v t , and I can't find the location of lambda in it. Where's the bytecode which tells the jvm to compute the expression with the input Integer whenever the lambda is envoked? 回答1: If you want to see the code of your lambda body