折叠展平列表

时间:2017-01-24 00:19:07

标签: dart

我能够使用折叠

来展平列表
flattenWithFold(Iterable list) => list.fold([], (List xs, s) {
s is Iterable ? xs.addAll(flattenWithFold(s)) : xs.add(s);
   return xs;
});

执行时

print(flattenWithFold([1,[3,5,[1,2]],[2,1],6])); 

它产生正确的结果[1,3,5,1,2,2,1,6]

但是当我尝试重构使用..add时,它会产生不正确的结果

flattenWithFold1(Iterable list) => list.fold([], (List xs, s) => xs..add(
              s is Iterable ? xs.addAll(flattenWithFold1(s)) : s));

有人可以在执行时解释为什么有空[1,3,5,1,2,null,null,2,1,null,6]?

print(flattenWithFold1([1,[3,5,[1,2]],[2,1],6]));

1 个答案:

答案 0 :(得分:5)

您的结果中显示null,因为如果您s Iterable xs..add(xs.addAll(flattenWithFold1(s)) addAllvoid是一种null方法,但由于您将其用作表达式,因此会返回xs。因此,您要将展平的元素添加到null,但随后又添加void,这是... gulong clientConnectedConfigureId = g_signal_connect(m_server, "client-connected", (GCallback)ClientConnected, this); ... static void ClientConnected(GstRTSPServer *gstrtspserver, GstRTSPClient *arg1, gpointer user_data) { gulong describeConfigureId = g_signal_connect(arg1, "describe-request", (GCallback)DescribeRequest, user_data); //Do not forget about g_signal_handler_disconnect } void DescribeRequest(GstRTSPClient *gstrtspclient, GstRTSPContext *arg1, gpointer user_data) { gchar *urlString = gst_rtsp_url_get_request_uri(arg1->uri); std::stringstream ssTemp; ssTemp << "DescribeRequest - urlString = " << urlString; InternalLog::Debug(ssTemp.str()); g_free(urlString); } 方法的返回值。