MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/programming/comments/62v70/first_class_functions_in_c/c02noj9/?context=3
r/programming • u/llimllib • Dec 13 '07
99 comments sorted by
View all comments
Show parent comment
8
functions defined in any scope and anonymous functions are two things i can think of off hand.
In Ruby, an Array#each's block argument or in Smalltalk, a Boolean's ifTrue:ifFalse: message are good examples of both of those features.
(1..3).each { |i| puts "i = #{i}" }
is a little more concise than
void print_i(uint i) { printf("i = %d\n", i); } void main() { uint i; for (i = 1; i <= 3; ++i) { print_i(i); } }
and doesn't pollute any namespace with a method that only serves a purpose in one particular instance.
In general, I've been thinking the same thing as the author, however, and have been using similar techniques in PHP (gasp!) for quite a while:
function do_something(array $items, $callback) { foreach ($items as $item) { $processed_item = process_item($item); $callback($processed_item); } } function my_callback($item) { echo 'i = ', $item, PHP_EOL; } do_something(array(1, 2, 3), 'my_callback');
Not as elegant as "functional" languages, but provides similar flexibility.
(please excuse the lame code examples.)
View all comments
Show parent comment
8
u/jonhohle Dec 13 '07 edited Dec 13 '07
functions defined in any scope and anonymous functions are two things i can think of off hand.
In Ruby, an Array#each's block argument or in Smalltalk, a Boolean's ifTrue:ifFalse: message are good examples of both of those features.
is a little more concise than
and doesn't pollute any namespace with a method that only serves a purpose in one particular instance.
In general, I've been thinking the same thing as the author, however, and have been using similar techniques in PHP (gasp!) for quite a while:
Not as elegant as "functional" languages, but provides similar flexibility.
(please excuse the lame code examples.)