鉴于int*string
的一组词典,其中第一个是"主要",我想回答这个问题:
对于所有其他词典,所有对于与主要词典相同的键具有相同的值吗?
我目前通过以下方式实现了这一目标:
let allSame = primary @ remaining
|> Seq.groupBy (fun (pos, _) -> pos)
|> Seq.map (fun (pos, items) -> (pos, items |> Seq.map (fun (_, name) -> name) |> Seq.distinct |> List.ofSeq))
|> Seq.exists (fun (_, names) -> names.Length > 1))
我想知道是否有更为惯用的方法来实现这一目标?
在分组pos
中复制int * (int * string) list
,然后必须缩减至int * string list
似乎有点多余,但遗憾的是groupBy
不是{SortOrder:int;Name:string;...}
提供价值预测超载。
给出一堆具有简化结构Field list -> (int * string) list
我要去:let primary = getFields <| fst x
let allSame = (primary) @ ((tail |> List.map (fun (m,_) -> getFields m)) |> List.collect (fun e -> e))
|> Seq.sortBy (fun (pos, _) -> pos)
|> Seq.pairwise
|> Seq.forall (fun ((_,namex),(_,namey)) -> Seq.forall2 (=) namex namey)
if allSame then
Some (fst x)
else
failwith "Some error message here"
&#34;主要&#34;只是列表的头部,我选择哪一个&#34;主要&#34;并不重要。因为我只对所有具有相同位置的字段是否也具有相同名称感兴趣。
这就是为什么我按位置分组,然后缩小到一个不同的名称列表,只计算条目(&gt; 1显然意味着一些分歧)。
这是我最终的结果:
package com.example.mayank.sunshine;
import android.os.Bundle;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;
/**
* A {@link PreferenceActivity} that presents a set of application settings.
* <p>
* See <a href="http://developer.android.com/design/patterns/settings.html">
* Android Design: Settings</a> for design guidelines and the <a
* href="http://developer.android.com/guide/topics/ui/settings.html">Settings
* API Guide</a> for more information on developing a Settings UI.
*/
public class SettingsActivity extends PreferenceFragment
implements Preference.OnPreferenceChangeListener {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Add 'general' preferences, defined in the XML file
addPreferencesFromResource(R.xml.pref_general);
// For all preferences, attach an OnPreferenceChangeListener so the UI summary can be
// updated when the preference changes.
bindPreferenceSummaryToValue(findPreference(getString(R.string.pref_location_key)));
}
/**
* Attaches a listener so the summary is always updated with the preference value.
* Also fires the listener once, to initialize the summary (so it shows up before the value
* is changed.)
*/
private void bindPreferenceSummaryToValue(Preference preference) {
// Set the listener to watch for value changes.
preference.setOnPreferenceChangeListener(this);
// Trigger the listener immediately with the preference's
// current value.
onPreferenceChange(preference,
PreferenceManager
.getDefaultSharedPreferences(preference.getContext())
.getString(preference.getKey(), ""));
}
@Override
public boolean onPreferenceChange(Preference preference, Object value) {
String stringValue = value.toString();
if (preference instanceof ListPreference) {
// For list preferences, look up the correct display value in
// the preference's 'entries' list (since they have separate labels/values).
ListPreference listPreference = (ListPreference) preference;
int prefIndex = listPreference.findIndexOfValue(stringValue);
if (prefIndex >= 0) {
preference.setSummary(listPreference.getEntries()[prefIndex]);
}
} else {
// For other preferences, set the summary to the value's simple string representation.
preference.setSummary(stringValue);
}
return true;
}
}
答案 0 :(得分:1)
就像@Carsten在评论中所说,按键排序然后比较每个KeyValuePair。作为额外的好处,Seq.forall
是懒惰的,并在第一次不匹配时停止评估。
[primary; remaining1; remaining2]
|> Seq.map (Seq.sortBy (fun (KeyValue(k,_)) -> k))
|> Seq.pairwise
|> Seq.forall (fun (x, y) -> Seq.forall2 (=) x y)
答案 1 :(得分:0)
为了纯粹的可读性,我更喜欢定义辅助函数来测试每个键/值对。
您的问题并不是说不匹配的密钥是否正常,请选择合适的代码:
(不匹配的键可以)
let looselyAllSame (primary :: remaining) =
let hasDifferentName key value =
primary |> Map.tryFind key |> Option.exists ((<>) value)
not (remaining |> List.exists (Map.exists hasDifferentName))
(不匹配的密钥不正常)
let strictlyAllSame (primary :: remaining)=
let hasSameName key value =
primary |> Map.tryFind key |> Option.exists ((=) value)
remaining |> List.forall (Map.forall hasSameName)