我在swift中有这个功能
func *<T1:Sequence, T2:Sequence>(lhs: T1,rhs : T2) ->
[(T1.Iterator.Element,T2.Iterator.Element)]
{
let product = lhs.flatMap({ x in rhs.lazy.map{y in (x,y)}})
return product
}
我想让它评价懒惰。我知道我可以使用lhs.lazy.flatmap但是返回类型应该是什么?或者有更好的或其他方式来做这样的事情吗?
答案 0 :(得分:7)
您可以创建一个type-erased sequence,将其操作转发给基础
具有相同Element
类型的序列,隐藏了该特征
基础序列:
func *<T1:Sequence, T2:Sequence>(lhs: T1,rhs : T2) -> AnySequence<(T1.Iterator.Element,T2.Iterator.Element)>
{
return AnySequence (
lhs.lazy.flatMap { x in rhs.lazy.map { y in (x,y) }}
)
}
然后,您的代码独立于lazy.flatMap
的实际实现及其确切的返回类型(甚至可能随之改变
更新的Swift版本。)
答案 1 :(得分:0)
感谢亚历山大,我想出了
public class MainActivity extends AppCompatActivity {
private CountryService countryService = new CountryServiceImpl();
private static final String INSERT_DATA_ITEM_MESSAGE = "Insert data item selected";
private static final String EXIT_ITEM_MESSAGE = "Exit. Bye, bye";
private static final String ERROR_DURING_CONVERTING_JSON_TO_MAP_MESSAGE = "Error during converting JSON to Map. ";
private static final String JSON_FILE_NAME = "countries";
private static final String ASSET_FOLDER = "raw";
private static final String DATA_ARE_ALREADY_UPLOADED = "Data are already uploaded";
private static final String DATA_UPLOADED_SUCCESSFUL = "Data uploaded successful";
private static final String KEYWORD = "search";
private SQLiteDatabase sqLiteDatabase;
private SQLiteConnector sqLiteConnector;
private Spinner countryListSpinner;
private ListView listView;
private List<CountryWithCitiesDto> listCountryiesAndCities;
private ProgressBar progressBar;
private Map<String, String[]> getParsedMapFromJSONFile() {
InputStream inputStream = getResources().openRawResource(
getResources().getIdentifier(JSON_FILE_NAME,
ASSET_FOLDER, getPackageName()));
byte[] byteArrayFromJSONFile = JSONReader.getByteArrayFromJsonFile(inputStream);
if (byteArrayFromJSONFile != null) {
return buildMapFromByteArrayJSONFile(byteArrayFromJSONFile);
}
return Collections.emptyMap();
}
private Map<String, String[]> buildMapFromByteArrayJSONFile(byte[] jsonByteArray) {
try {
ObjectMapper mapper = new ObjectMapper();
return mapper.readValue(jsonByteArray, new TypeReference<Map<String, String[]>>() {
});
} catch (IOException e) {
Log.e(ClassNameTags.MAIN_ACTIVITY, ERROR_DURING_CONVERTING_JSON_TO_MAP_MESSAGE + e.getMessage());
return Collections.emptyMap();
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initDatabase();
countryListSpinner = (Spinner) findViewById(R.id.countryListSpinner);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
listView = (ListView) findViewById(R.id.cityListView);
progressBar.setVisibility(View.VISIBLE);
setAdapterToSpinner(setCountryListSpinner());
progressBar.setVisibility(View.INVISIBLE);
countryListSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int position, long arg3) {
List<String> cityList = new ArrayList<>();
for (int i = 0; i < listCountryiesAndCities.get(position).getCityList().size(); i++) {
cityList.add(listCountryiesAndCities.get(position).getCityList().get(i).getNameCity());
}
setAdapterToCityList(cityList);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Intent intent = new Intent(MainActivity.this, WikiActivity.class);
intent.putExtra(KEYWORD, listCountryiesAndCities.get((int) countryListSpinner.getSelectedItemId()).getCityList().get(position).getNameCity());
startActivity(intent);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.insert_data_menu:
if (listCountryiesAndCities.isEmpty()) {
progressBar.setVisibility(View.VISIBLE);
uploadFromJsonToDatabase();
setAdapterToSpinner(setCountryListSpinner());
progressBar.setVisibility(View.INVISIBLE);
Toast.makeText(this, DATA_UPLOADED_SUCCESSFUL, Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, DATA_ARE_ALREADY_UPLOADED, Toast.LENGTH_SHORT).show();
}
Log.i(ClassNameTags.MAIN_ACTIVITY, INSERT_DATA_ITEM_MESSAGE);
break;
case R.id.exit_menu:
Log.i(ClassNameTags.MAIN_ACTIVITY, EXIT_ITEM_MESSAGE);
break;
}
return super.onOptionsItemSelected(item);
}
private void initDatabase() {
sqLiteConnector = new SQLiteConnector(this);
sqLiteDatabase = sqLiteConnector.getWritableDatabase();
}
private void uploadFromJsonToDatabase() {
countryService.insertCountryWithCities(getParsedMapFromJSONFile(), sqLiteDatabase);
}
private String[] setCountryListSpinner() {
listCountryiesAndCities = countryService.getListCountriesWithCities(sqLiteDatabase);
String[] outArray = new String[listCountryiesAndCities.size()];
for (int i = 0; i < listCountryiesAndCities.size(); i++) {
System.out.println(listCountryiesAndCities.get(i).getCountry().getNameCountry());
outArray[i] = listCountryiesAndCities.get(i).getCountry().getNameCountry();
}
return outArray;
}
private void setAdapterToSpinner(String[] spinnerCountryArray) {
ArrayAdapter<String> adapter = new ArrayAdapter<>(this,
android.R.layout.simple_spinner_item, spinnerCountryArray);
countryListSpinner.setAdapter(adapter);
}
private void setAdapterToCityList(List<String> cityNames) {
ArrayAdapter<String> adapterToCityList
= new ArrayAdapter<>(this,
android.R.layout.simple_list_item_1,
cityNames);
listView.setAdapter(adapterToCityList);
adapterToCityList.notifyDataSetChanged();
}
}
这样可行,但不知何故,返回类型对我来说似乎有点太多了。