functional-programming

python reduce to find the union of sets

怎甘沉沦 提交于 2020-03-15 21:56:30
问题 I am trying to find the union of set of sets. Specifically I want the union of the list of nodes for each key in the dictionary of networkx graphs called periodic_gs . I would like to use the reduce function as it seems reasonable to take the union of all periodic_gs[x].nodes() where x is a key of periodic_gs . Here is my attempt: reduce(lambda x,y: set(periodic_gs[x].nodes()).union(set(periodic_gs[y].nodes())), periodic_gs.keys(), {}) To me, this says take the union of the nodes across each

python reduce to find the union of sets

情到浓时终转凉″ 提交于 2020-03-15 21:56:21
问题 I am trying to find the union of set of sets. Specifically I want the union of the list of nodes for each key in the dictionary of networkx graphs called periodic_gs . I would like to use the reduce function as it seems reasonable to take the union of all periodic_gs[x].nodes() where x is a key of periodic_gs . Here is my attempt: reduce(lambda x,y: set(periodic_gs[x].nodes()).union(set(periodic_gs[y].nodes())), periodic_gs.keys(), {}) To me, this says take the union of the nodes across each

For a binary search tree I need to compute the length of the diameter of the tree. I need some inputs for scala program

独自空忆成欢 提交于 2020-03-06 11:08:30
问题 The diameter of a binary tree is the length of the longest path between any two nodes in a tree. This path may or may not pass through the root. Here is my logic in scala. Can anyone check the logic and confirm if there is a simpler method. Also I am seeing this error when I run the program: error: not found: value Solution (in solution.scala) var ret = Solution.diameterOfBinaryTree(param1) class Solution { var ans: Int = _ def diameterOfBinaryTree(root: TreeNode): Int = { ans = 1 depth(root)

For a binary search tree I need to compute the length of the diameter of the tree. I need some inputs for scala program

雨燕双飞 提交于 2020-03-06 11:07:30
问题 The diameter of a binary tree is the length of the longest path between any two nodes in a tree. This path may or may not pass through the root. Here is my logic in scala. Can anyone check the logic and confirm if there is a simpler method. Also I am seeing this error when I run the program: error: not found: value Solution (in solution.scala) var ret = Solution.diameterOfBinaryTree(param1) class Solution { var ans: Int = _ def diameterOfBinaryTree(root: TreeNode): Int = { ans = 1 depth(root)

how to parse json with field of optional and variant type in Haskell?

假装没事ソ 提交于 2020-03-06 09:31:15
问题 How I can parse the input json inside this file ? https://github.com/smogon/pokemon-showdown/blob/master/data/moves.js For the secondary and flags properties? They are optional and contains variant type. A minimal example would be this one: [ {}, { "secondary": false }, { "secondary": { "chance": 10, "boosts": { "spd": -1 } } }, { "secondary": { "chance": 30, "volatileStatus": "flinch" } }, { "secondary": { "chance": 30 } }, { "secondary": { "chance": 10, "self": { "boosts": { "atk": 1, "def"

Update item in tree structure by reference and return updated tree structure

前提是你 提交于 2020-03-05 06:23:50
问题 I am currently learning functional programming using HyperappJS (V2) and RamdaJS. My first project is a simple blog app where users can comment on posts or other comments. The comments are represented as a tree structure. My state looks something like this: // state.js export default { posts: [ { topic: `Topic A`, comments: [] }, { topic: `Topic B`, comments: [ { text: `Comment`, comments: [ /* ... */ ] } ] }, { topic: `Topic C`, comments: [] } ], otherstuff: ... } When the user wants to add

How to simulate let expressions in JavaScript?

◇◆丶佛笑我妖孽 提交于 2020-03-03 08:43:06
问题 Consider the following implementation of take: const take = (n, [x, ...xs]) => n === 0 || x === undefined ? [] : [x, ...take(n - 1, xs)]; console.log(take(7, [1, 2, 3, 4, 5])); // [1, 2, 3, 4, 5] console.log(take(3, [1, 2, 3, 4, 5])); // [1, 2, 3] console.log(take(1, [undefined, 1])); // [] As you can see, it doesn't work for arrays with undefined because x === undefined is not the best way to test whether an array is empty. The following code fixes this problem: const take = (n, xs) => n ===

Kotlin's reduce() function with different types

不羁的心 提交于 2020-03-01 18:50:47
问题 I was looking through array extension functions and found reduce() one inline fun <S, T: S> Array<out T>.reduce(operation: (acc: S, T) -> S): S { if (isEmpty()) throw UnsupportedOperationException("Empty array can't be reduced.") var accumulator: S = this[0] for (index in 1..lastIndex) { accumulator = operation(accumulator, this[index]) } return accumulator } here the accumulator variable of type S assigned with first element from the array with type T . Can't wrap my head around the real use

Fill a nested structure with values from a linear supply stream

微笑、不失礼 提交于 2020-02-28 18:46:07
问题 I got stuck in the resolution of the next problem: Imagine we have an array structure, any structure, but for this example let's use: [ [ [1, 2], [3, 4], [5, 6] ], [ 7, 8, 9, 10 ] ] For convenience, I transform this structure into a flat array like: [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ] Imagine that after certain operations our array looks like this: [ 1, 2, 3, 4, 12515, 25125, 12512, 8, 9, 10] NOTE: those values are a result of some operation, I just want to point out that is independent from

How to fork and merge monads?

前提是你 提交于 2020-02-28 17:27:23
问题 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