Skip to content

bkhant1/cpp_grep

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 

Repository files navigation

cpp_grep

In python

I ran accross a quite convenient function (called grep) in python. It recursively applies a predicate to a structure (list or dictionary) and returns element matching this predicate.

def grep(struct, predicate, results=[]):
  if predicate(struct):
    results.append(struct)
  if isinstance(struct, dict):
    for elem in struct.values():
      grep(elem, predicate, results)
  elif isinstance(struct, list):
    for elem in struct:
      grep(elem, predicate, results)
  return results

data = {
  "customers":[
    {
      "name": "Jon",
      "address": "j.doe@hotmail.eu"
    },
    {
      "name": "Denis",
      "address": "denis.dude@gmail.com"
    }
  ]
}

result = grep(
  data,
  lambda x: (
    isinstance(x, dict)
    and "name" in x 
    and x["name"] == "Denis")
)

# result == [{"name": "Denis", "address": "denis.dude@gmail.com"}]}

This comes quite handy for instance when testing APIs. In the example above we're expecting the data to have in one customer named Denis. Using grep the assertion is resilient to a change in the datastructure, and avoid having to "manually" look for it.

In C++

This is an attempt to implement the same thing in C++. grep will iterate:

  • through tuples (and through classes via reflection once implemented) at compile time
  • through iterable containers at runtime

For each element encountered it will apply the predicate if the type of the element can be passed to the predicate.

It puts a pointer to the element in the list it returns.

Pseudo-example inspired from the tests (the data declaration doesn't compile in C++11 but C++ syntax makes it a bit unclear in the test):

tuple<
    vector<int>, char, tuple<
        int, string, vector<int>>>
    data {
    	{1,2,3,4,6},
    	'B',
    	{
    	    5,
    	    "Bonjour",
    	    {4,3,2,1}
	    }}
	    
auto result = grep<char>(data, equal_to<char>('B'))
// result contains the address of the 2 characters the address of the two 'B's in data. It took 1 + size("Bonjour") evaluation - one per character

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages