Last active
November 17, 2019 21:03
-
-
Save adamcrussell/73f9c047416953e91c4b514ad6577239 to your computer and use it in GitHub Desktop.
Perl Weekly Challenge 034
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
/** | |
* Write a program that demonstrates using hash slices and/or array slices. | |
**/ | |
#include <map> | |
#include <vector> | |
#include <string> | |
#include <iostream> | |
#include <algorithm> | |
#include <iterator> | |
void print_element(std::string s){ | |
if(s != "") | |
std::cout << s << std::endl; | |
} | |
int main(int argc, char** argv){ | |
std::vector<int> v {2, 4}; | |
std::map<int, std::string> mapExp = { {1, "first"}, {2, "second"}, {3, "third"}, {4,"fourth"} }; | |
std::vector<std::string> vValue; | |
std::transform(mapExp.begin(), mapExp.end(), std::back_inserter(vValue), | |
[=](const std::pair<int, std::string> &mapItem){ | |
std::vector<int>::const_iterator iter = std::find(v.begin(), v.end(), mapItem.first); | |
if(iter != v.end()) | |
return mapItem.second; | |
return std::string(""); | |
}); | |
std::for_each(vValue.begin(), vValue.end(), print_element); | |
} |
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
use strict; | |
use warnings; | |
## | |
# Write a program that demonstrates using hash slices and/or array slices. | |
## | |
my %h = ( | |
1 => "first", | |
2 => "second", | |
3 => "third", | |
4 => "fourth" | |
); | |
my @a = @h{2, 4}; | |
print join("\n", @a) . "\n"; |
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
/** | |
* Write a program that demonstrates a dispatch table. | |
**/ | |
#include <map> | |
#include <string> | |
#include <iostream> | |
#include <functional> | |
int main(){ | |
std::map< const std::string , std::function<void(void)> > dispTable{ | |
{"hello",[](){ std::cout << "Hello!" << std::endl; } }, | |
{"goodbye",[](){ std::cout << "Goodbye!" << std::endl; } } | |
}; | |
dispTable["hello"](); | |
dispTable["goodbye"](); | |
}; |
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
use strict; | |
use warnings; | |
## | |
# Write a program that demonstrates a dispatch table. | |
## | |
my %dispatch_table = ( | |
hello => sub { print "Hello!\n"; }, | |
goodbye => sub { print "Goodbye!\n"; } | |
); | |
$dispatch_table{hello}(); | |
$dispatch_table{goodbye}(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment