![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
King of Portal
|
Function too convoluted?
Made this function to filter out results from an array of arrays:
php Syntax (Toggle Plain Text)
Array
(
[2] => Array
(
[volume] => 85
[edition] => 6
)
[3] => Array
(
[volume] => 98
[edition] => 2
)
[4] => Array
(
[volume] => 86
[edition] => 6
)
)
__________________
Lo, there do I see my father. 'Lo, there do I see My mother, and my sisters, and my brothers. 'Lo, there do I see The line of my people... Back to the beginning. 'Lo, they do call to me. They bid me take my place among them. In the halls of Valhalla... Where the brave... May live... ...forever.. GrimBB | Mimesis |
|
|
|
|
|
#2 |
|
Programming Guru
![]() |
Re: Function too convoluted?
Yikes. That does not look like a healthy piece of code at all.
Could you specify the input/output in the format of a problem statement? More specifically, for every possible inputted array, what is the desired output? I can't bother myself to decipher your cryptic code. It would be easier if you explained. |
|
|
|
|
|
#3 |
|
King of Portal
|
Re: Function too convoluted?
Input: array of values or of nested arrays
Procedure: unset values that are =, <, >, <=, >= (and permutations of those) of some range Output: filtered array
__________________
Lo, there do I see my father. 'Lo, there do I see My mother, and my sisters, and my brothers. 'Lo, there do I see The line of my people... Back to the beginning. 'Lo, they do call to me. They bid me take my place among them. In the halls of Valhalla... Where the brave... May live... ...forever.. GrimBB | Mimesis |
|
|
|
|
|
#4 |
|
Expert Programmer
|
Re: Function too convoluted?
I would create a function for each sort descriptor and build an array of containing references to the appropriate sort descriptors.
|
|
|
|
|
|
#5 |
|
Programming Guru
![]() |
Re: Function too convoluted?
I'm not sure if it's the same thing as what titaniumdecoy is saying, but I'm sure your code would be much more stable, readable, extendable, and simpler, if you were to write a callback function that does the desired filtering, and pass that callback function into 'attenuate'.
Attenuate then asks the callback function whether or not a specific item should be filtered. Then it's left up to the one who's implementing the function to modify the callback, or pass through his/her own callback. It's also more powerful than leaving it up to the limitations of your debilitating 'GT/LT/EQ' scheme. |
|
|
|
|
|
#6 |
|
King of Portal
|
Re: Function too convoluted?
Attenuate is the filtering function. It doesn't make much sense to me to write another function that would aid this one. I'd still have to know how they want to filter the results so I don't understand how the GT/LT/EQ is debilitating. I can explain algorithmically what the function is doing.
__________________
Lo, there do I see my father. 'Lo, there do I see My mother, and my sisters, and my brothers. 'Lo, there do I see The line of my people... Back to the beginning. 'Lo, they do call to me. They bid me take my place among them. In the halls of Valhalla... Where the brave... May live... ...forever.. GrimBB | Mimesis |
|
|
|
|
|
#7 |
|
Programming Guru
![]() |
Re: Function too convoluted?
But you have one function that processes the array. And in that same function, you test whether or not it's a valid item to include. If you want to modify the method of filtering (not the values involved) you need to modify the entire outerworking of the same function that's doing the processing. That's bad coding.
Pass through a static callback function that will determine whether or not its argument is a valid item. The parts of your program that change become isolated from the static parts. Just leave it up to the one who's writing the callback function to worry about how it's being filtered. If these specifications need to be set dynamically on run-time, according to the user's request, then you can work your way around that. But it's silly to invent your own ASCII logic gates to be interpreted with yet more boolean logic. With this change you don't need to worry about your own wonky optional parameters. It would make your code capable of being changed in a second flat. The code would be much shorter and clearer. It would also fix your nest of around 11 indents for some of the blocks (horrific!). I suggest you stick to conventional coding unless there's a clear reason why it can not work more efficiently any other way. As an aside, this very much reminds me of something from over a year ago... I went through almost this exact same situation as you are going through now. I wrote a hash table that would take optional parameters, in its lookup() function, to filter results on a set of logical conditions. I thought I was being clever because it replicated behaviour similar to MySQL, which does use ASCII logic. But what did not occur to me, is my (brilliant) own logic scheme became absolutely useless as soon as I tried to interface a database client over top. My client could not control the optional parameters that went through, because optional parameters are specified statically on run-time. My method only worked as long as all interfacing to the lookup function was hard-coded. The reason this logic scheme works for MySQL is because it's not as though their string is parsed into a list of parameters and then passed through a function, and reinterpreted. It's all handled in a very direct way. Presumably with some very clever algorithms too, such as the Horn formula framework. This isn't the only reason your custom approach can fail; it can be plain out difficult to work with, or the requirements of the algorithm can change. In which case you might need to extend the inner portion of your function to be even worse off than it is right now. |
|
|
|
|
|
#8 |
|
King of Portal
|
Re: Function too convoluted?
Alright Sane this is what I managed to come up with after checking out the PHP site and reading what you were saying. I didn't quite understand you at first until I found this example on the PHP site.
PHP Syntax (Toggle Plain Text)
__________________
Lo, there do I see my father. 'Lo, there do I see My mother, and my sisters, and my brothers. 'Lo, there do I see The line of my people... Back to the beginning. 'Lo, they do call to me. They bid me take my place among them. In the halls of Valhalla... Where the brave... May live... ...forever.. GrimBB | Mimesis |
|
|
|
|
|
#9 |
|
Programming Guru
![]() |
Re: Function too convoluted?
Very nice!
![]() If that still meets your initial requirements, I'd say that's a much nicer solution, yes? ![]() And if you don't want to worry about the bad garbage cleanup of "create_function", you could also pass through a named function like so, I believe... PHP Syntax (Toggle Plain Text)
I'm not as sure about that. But it should work. By the way, ? true : false is a redundant statement. |
|
|
|
|
|
#10 |
|
King of Portal
|
Re: Function too convoluted?
Ok here's what all of this was intended to do: PHP Syntax (Toggle Plain Text)
|