Last active
May 13, 2022 13:44
-
-
Save polyglothacker/183bfcbb5661749cbaa39539426f1502 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Let us say you have a piece of JSON data as follows. | |
basket = {'grape': 160, | |
'banana': 80, | |
'pear': 320, | |
'orange': 120, | |
'red apple': 290, | |
'green apple': 380, | |
'papaya': 40} | |
And you have a price constraint as follows. | |
constraints = {'grape': ('<', 200), | |
'banana': ('<', 100), | |
'green apple': ('<',500), ' | |
red_apple': ('>', 1000)} | |
When you mentally apply the constraint to the data the answer would be, | |
{"grape": 160, "banana": 80, "green apple": 380"} | |
Now let us extend it to another level of nesting. | |
fuel = {'diesel': ('<', 100), | |
'petrol': ('<', 120), | |
'cng': ('<', 80)} | |
fuel_prices = {'bangalore': { 'diesel': 85, | |
'petrol': 100, | |
'cng': 75}, | |
'delhi': {'diesel': 120, | |
'petrol': 130, | |
'cng': '70'}} | |
Now if you apply it the "bangalore" element will be selected and "delhi" wont be. | |
Come up with a generic design approach to solve constraint problems like this. The things to discuss | |
and have in your design are, | |
1. A DSL or generic "path" selector for applying the price constraint on to the data | |
2. How to find the data at the right nesting if you are not using a selector ? | |
3. How to apply "pattern" matching to the problem ? | |
4. How to do this in a performant way ? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment