coconut --display
to look at what various different uses of the pipe operator compile to.
hello, i'm a super noob to functional programming and the Coconut language. I went over the tutorial section to get a grasp of few of the concepts and was hoping to apply some of the learnings to a messy piece of code but i'm struggling to get started.
my current solution is to create layers of nesting to get some values out of a json object. i'm looking for some advise on how to take a functional approach to solve this problem.
here is my test json
{
"mobile/stats": {
"nestedStats": {
"kind": "stats",
"entries": {
"activeMemberCnt": {
"value": "3"
},
"totRequests": {
"value": 600
},
"mobile/members/stats": {
"nestedStats": {
"entries": {
"firePunch/stats": {
"nestedStats": {
"entries": {
"power": {
"value": 25
},
"pp": {
"value": 10
},
"type" : {
"value" : "Fire"
}
}
}
},
"icePunch/stats": {
"nestedStats": {
"entries": {
"power": {
"value": 75
},
"pp": {
"value": 15
},
"type" : {
"value" : "Ice"
}
}
}
},
"karateChop/stats": {
"nestedStats": {
"entries": {
"power": {
"value": 25
},
"pp": {
"value": 10
},
"type" : {
"value" : "Fighting"
}
}
}
}
}
}
}
}
}
}
}
stats = {}
for k, v in test_json.items():
for kk, vv in v.items():
if 'nestedStats' in kk:
for kkk, vvv in vv.items():
if 'entries' in kkk:
for kkk1, vvv1 in vvv.items():
if '/stats' in kkk1:
for kkkk, vvvv in vvv1.items():
for kkkkk, vvvvv in vvvv.items():
for kkkkkk, vvvvvv in vvvvv.items():
for kkkkkkk, vvvvvvv in vvvvvv.items():
for kkkkkkkk, vvvvvvvv in vvvvvvv.items():
stats[kkkkkk] = vvvvvvvv
poke_stats = json.dumps(stats).replace('\'', '"').replace('description', '').replace('{"":', '').replace('{"value":', '').replace('},', ',')[:-1]
nalix
> <@gitter_rawvnode:matrix.org> hello, i'm a super noob to functional programming and the Coconut language. I went over the tutorial section to get a grasp of few of the concepts and was hoping to apply some of the learnings to a messy piece of code but i'm struggling to get started.
my current solution is to create layers of nesting to get some values out of a json object. i'm looking for some advise on how to take a functional approach to solve this problem.
test json
```
The function approach to deeply nested data structures is lenses (at least the only one I know of). This seems like what you'd need, though I haven't used the package myself.
nalix
* The functional approach to deeply nested data structures is lenses (at least the only one I know of). This seems like what you'd need, though I haven't used the package myself.
i think i've somewhat simplified the code to be slightly less convoluted by adding a few things...
also i made a few assumptions to reduce the complexity within the flow like separating the logic to handle parent stats and its member stats.
the new function walks along the json tree path going from parent, child all the way down until it encounters an attribute labeled "value" and add its value to the member_stats
dict when there is a match and the value
is of type int. The key will be the second but previous element in the path
list.
I've replaced the deep nesting of for loops with a recursive function. I'm hoping to add more features like pattern matching and map/filter if possible and also eliminate the global level dicts to manage state :)
parent_stats = {}
member_stats = {}
def traverse(obj, path=None):
if path is None:
path = []
if isinstance(obj, dict):
value = { k: traverse(v, path + [k]) for k, v in obj.items()}
elif isinstance(obj, int):
value = obj
if "value" in path:
node_key = list(filter(lambda x: re.search('https.*members.*members/(.+)/stats\Z', x), path))
if node_key:
if node_key[0] not in member_stats.keys():
member_stats[node_key[0]] = {path[-2] : value}
else:
member_stats[node_key[0]].update({path[-2] : value})
else:
parent_stats[path[-2]] = value
else:
value = obj
return value
traverse(poke_stats)
parent_stats
{
"mobile/stats": {
"activeMemberCnt" : 3,
"totRequests" : 600
}
}
member_stats
{
"firePunch/stats" : {
"power" : 25,
"pp" : 10
},
"icePunch/stats" : {
"power" : 25,
"pp" : 15
},
"karateChop/stats" : {
"power" : 25,
"pp" : 10
}
}
@evhub Im planning to use data
to build a toolkit around network analysis. The big issue with graph data structures I've encountered is the need to constantly keep track of whether you're using an edge-oriented function or a node-oriented one. So, does my function operate on adjacency matrices, edgelists, etc.
The most elegant way I've seen this done imo is tidygraph https://tidygraph.data-imaginist.com/ which lets you switch modes.
in my case, I would like to build a graph data
object that stores both. Then, there are functions that work on either adjacency matrices, or on edgelists, etc. I would like to be able to pass a graph object to those functions and have them behave like the correct contents were passed
my intuition is to write a pattern-matching fmap
to turn the graph object into a sort of two-faced functor: if the function passed to fmap accepts e.g. square matrices, the fmap uses the adjacency representation....if it accepts lists/vectors, it instead uses the edgelist view.
A bit of a functional noob here, so just wanting some feedback if this is something ridiculous to do, w.r.t. coconut's design haha
__fmap__