class M
{
function double($a) {
return $a * 2;
}
}
$m = new M;
$list = array(1, 2, 3);
$list = array_map(array($m, 'double'), $list);
print_r($list);
Array
(
[0] => 2
[1] => 4
[2] => 6
)
Showing posts with label objects. Show all posts
Showing posts with label objects. Show all posts
Friday, December 5, 2008
PHP: Invoking a method with Map
Ever since I learned the basics of functional programming, I've been a fan of the map function. In the right context, it can be an elegant solution for array transformation. Of course, like anything, it can be misused. Using PHP's map, it's non-obvious how to make it invoke an instance method rather than a function. The following example illustrates how this can be accomplished.
Friday, September 12, 2008
Vimgrep Without Object Names
My friend Chris (Vim user extraordinaire) has contributed the following tip. It looks like it could be quite useful to any Java hackers in the crowd.
I constantly want to use :vimgrep to find usages/references to certain object types in my java code. However, when I :vimgrep for the object name, it (naturally) matches all of the 'import' statements, too. I've known for awhile that there was an idiom in the regex syntax to negate a previous atom (in this case, the 'import' at the beginning of the line) but I've never found a way to phrase it that works. Here it is:
.*ObjectName\&^\(import\)\@!
This matches any line meeting the following two constraints:
1) contains anything (.*) followed by ObjectName
2) starts (^) with something which isn't 'import' (\(import\)\@!)
the \@! says, match if the preceding atom does not match here -- since we put ^\(import\) this means match when the first thing in the line isn't import.
There may be better ways to do this, but it's the first I've found and I'm using it like crazy now... Enjoy!
Thanks Chris!
I constantly want to use :vimgrep to find usages/references to certain object types in my java code. However, when I :vimgrep for the object name, it (naturally) matches all of the 'import' statements, too. I've known for awhile that there was an idiom in the regex syntax to negate a previous atom (in this case, the 'import' at the beginning of the line) but I've never found a way to phrase it that works. Here it is:
.*ObjectName\&^\(import\)\@!
This matches any line meeting the following two constraints:
1) contains anything (.*) followed by ObjectName
2) starts (^) with something which isn't 'import' (\(import\)\@!)
the \@! says, match if the preceding atom does not match here -- since we put ^\(import\) this means match when the first thing in the line isn't import.
There may be better ways to do this, but it's the first I've found and I'm using it like crazy now... Enjoy!
Thanks Chris!
Friday, November 30, 2007
Text Objects
Text objects are commands that can be used in combination with an operator or when in visual mode. Basically, you give a command followed by a description and Vim applies it to the relevant text object. Available commands include things like:
aw - a word
iw - inner word
aW - a word (leading and trailing whitespace included)
as - a sentence
etc...
In practice, these allow quick manipulation on the applied block of text. For example, you're in normal mode, and your cursor is positioned in the middle of a word. You type caw and Vim executes "change a word" deleting the existing word and switching to insert mode for you to perform your edit. For the full details on this feature see :help text-objects.
aw - a word
iw - inner word
aW - a word (leading and trailing whitespace included)
as - a sentence
etc...
In practice, these allow quick manipulation on the applied block of text. For example, you're in normal mode, and your cursor is positioned in the middle of a word. You type caw and Vim executes "change a word" deleting the existing word and switching to insert mode for you to perform your edit. For the full details on this feature see :help text-objects.
Subscribe to:
Posts (Atom)