الفرق بين المراجعتين ل"Kotlin/collections/foldIndexed"

من موسوعة حسوب
اذهب إلى التنقل اذهب إلى البحث
(أنشأ الصفحة ب'<noinclude>{{DISPLAYTITLE: الدالة <code>foldIndexed()‎</code> في لغة Kotlin}}</noinclude> يختلف سلوك الدالة<code>foldIndexed()‎</code> حس...')
 
سطر 1: سطر 1:
 
<noinclude>{{DISPLAYTITLE: الدالة <code>foldIndexed()‎</code> في لغة Kotlin}}</noinclude>
 
<noinclude>{{DISPLAYTITLE: الدالة <code>foldIndexed()‎</code> في لغة Kotlin}}</noinclude>
يختلف سلوك الدالة<code>foldIndexed()‎</code> حسب نوع المعاملات (parameters) المُمرّرة إليها (انظر فقرة [[Kotlin/collections/fold#.D8.A7.D9.84.D8.A8.D9.86.D9.8A.D8.A9 .D8.A7.D9.84.D8.B9.D8.A7.D9.85.D8.A9|البنية العامة]]).  
+
تُراكِم الدالة<code>foldIndexed()‎</code> القيم بدءًا من القيمة المُمرّرة <code>initial</code> وتطبّق العملية <code>operation</code> من اليسار إلى اليمين على القيمة المُتراكمة الراهنة وعلى العنصر التالي من [[Kotlin/Array|المصفوفة]] أو [[Kotlin/collections|المجموعة]] التي استُدعيت عبرها وكذلك فهرسه. وتُعيد القيمة النهائية.  
 
==البنية العامة==
 
==البنية العامة==
<syntaxhighlight lang="kotlin">
+
يمكن استدعاء الدالة<code>foldIndexed()‎</code> على [[Kotlin/Array|المصفوفات]].<syntaxhighlight lang="kotlin">
inline fun <T, R> Array<out T>.fold(
+
inline fun <T, R> Array<out T>.foldIndexed(
 
     initial: R,  
 
     initial: R,  
     operation: (acc: R, T) -> R
+
     operation: (index: Int, acc: R, T) -> R
 
): R  
 
): R  
inline fun <R> ByteArray.fold(
+
inline fun <R> ByteArray.foldIndexed(
 
     initial: R,  
 
     initial: R,  
     operation: (acc: R, Byte) -> R
+
     operation: (index: Int, acc: R, Byte) -> R
 
): R  
 
): R  
inline fun <R> ShortArray.fold(
+
inline fun <R> ShortArray.foldIndexed(
 
     initial: R,  
 
     initial: R,  
     operation: (acc: R, Short) -> R
+
     operation: (index: Int, acc: R, Short) -> R
 
): R  
 
): R  
inline fun <R> IntArray.fold(
+
inline fun <R> IntArray.foldIndexed(
 
     initial: R,  
 
     initial: R,  
     operation: (acc: R, Int) -> R
+
     operation: (index: Int, acc: R, Int) -> R
 
): R  
 
): R  
inline fun <R> LongArray.fold(
+
inline fun <R> LongArray.foldIndexed(
 
     initial: R,  
 
     initial: R,  
     operation: (acc: R, Long) -> R
+
     operation: (index: Int, acc: R, Long) -> R
 
): R  
 
): R  
inline fun <R> FloatArray.fold(
+
inline fun <R> FloatArray.foldIndexed(
 
     initial: R,  
 
     initial: R,  
     operation: (acc: R, Float) -> R
+
     operation: (index: Int, acc: R, Float) -> R
 
): R  
 
): R  
inline fun <R> DoubleArray.fold(
+
inline fun <R> DoubleArray.foldIndexed(
 
     initial: R,  
 
     initial: R,  
     operation: (acc: R, Double) -> R
+
     operation: (index: Int, acc: R, Double) -> R
 
): R  
 
): R  
inline fun <R> BooleanArray.fold(
+
inline fun <R> BooleanArray.foldIndexed(
 
     initial: R,  
 
     initial: R,  
     operation: (acc: R, Boolean) -> R
+
     operation: (index: Int, acc: R, Boolean) -> R
 
): R  
 
): R  
inline fun <R> CharArray.fold(
+
inline fun <R> CharArray.foldIndexed(
 
     initial: R,  
 
     initial: R,  
     operation: (acc: R, Char) -> R
+
     operation: (index: Int, acc: R, Char) -> R
 
): R  
 
): R  
inline fun <T, R> Iterable<T>.fold(
+
 
 +
</syntaxhighlight>يمكن استدعاء غلدالة<code>foldIndexed()‎</code> على [[Kotlin/Array|المجموعات]].<syntaxhighlight lang="kotlin">
 +
inline fun <T, R> Iterable<T>.foldIndexed(
 
     initial: R,  
 
     initial: R,  
     operation: (acc: R, T) -> R
+
     operation: (index: Int, acc: R, T) -> R
): R
+
): R
 
+
</syntaxhighlight>يُلاحَظ وجود الكلمة المفتاحية <code>inline</code> للدلالة على أن هذه الدالة مباشرة، وللمزيد من التفاصيل راجع <nowiki/>[[Kotlin/inline functions|توثيق الدوال المباشرة (inline functions)]].
</syntaxhighlight>ستُراكِم الدالة<code>fold()‎</code> القيم بدءًا من القيمة المُمرّرة <code>initial</code> وتطبّق العملية <code>operation</code> من اليسار إلى اليمين على القيمة المُتراكمة الراهنة وكل عناصر [[Kotlin/Array|المصفوفة]] أو [[Kotlin/collections|المجموعة]] التي استُدعيت عبرها. وستُعيد القيمة النهائية.<syntaxhighlight lang="kotlin">
 
inline fun <T, K, R> Grouping<T, K>.fold(
 
    initialValueSelector: (key: K, element: T) -> R,
 
    operation: (key: K, accumulator: R, element: T) -> R
 
): Map<K, R>
 
</syntaxhighlight>تُجمّع الدالة<code>fold()‎</code> القيم من المصدر <code>[[Kotlin/collections/Grouping|Grouping]]</code> حسب المفاتيح (keys) وتطبّق العملية <code>operation</code> على عناصر كل مجموعة (group) بشكل متتابع، فتُمرّر  قيمة التراكم الراهنة والعنصر الراهن كوسيطين (arguments)، وتخزّن النتائج في [[Kotlin/collections/Map|قاموس]] جديد. القيمة الأولية توفّرها الدالة <code>initialValueSelector</code> التي تُزوّد الدالة <code>fold()‎</code> بقيمة أولية لكل مجموعة (group). ثم تعيد  [[Kotlin/collections/Map|قاموسًا]] يربط بين مفاتيح كل مجموعة (group) مع نتيجة تراكم عناصرها.<syntaxhighlight lang="kotlin">
 
inline fun <T, K, R> Grouping<T, K>.fold(
 
    initialValue: R,  
 
    operation: (accumulator: R, element: T) -> R
 
): Map<K, R>
 
</syntaxhighlight>تُجمّع الدالة<code>fold()‎</code> القيم من المصدر <code>[[Kotlin/collections/Grouping|Grouping]]</code> حسب المفاتيح (keys) وتطبّق العملية <code>operation</code> على عناصر كل مجموعة  (group) بشكل متتابع، فتُمرّر  قيمة التراكم الراهنة والعنصر الراهن كوسيطين (arguments)، وتخزّن النتائج في [[Kotlin/collections/Map|قاموس]] جديد. القيمة الأولية هي الوسيط الممرّر  <code>initialValue</code>. ثم تعيد  [[Kotlin/collections/Map|قاموسًا]] يربط بين مفاتيح كل مجموعة (group) مع نتيجة تراكم عناصرها.
 
 
 
يُلاحَظ وجود الكلمة المفتاحية <code>inline</code> للدلالة على أن هذه الدالة مباشرة، وللمزيد من التفاصيل راجع <nowiki/>[[Kotlin/inline functions|توثيق الدوال المباشرة (inline functions)]].
 
 
 
 
==القيم المٌعادة==
 
==القيم المٌعادة==
انظر فقرة [[Kotlin/collections/fold#.D8.A7.D9.84.D8.A8.D9.86.D9.8A.D8.A9 .D8.A7.D9.84.D8.B9.D8.A7.D9.85.D8.A9|البنية العامة]].  
+
ناتج تطبيق العملية المُمرّرة <code>operation</code> تراكميًا على عناصر [[Kotlin/Array|المصفوفة]] أو [[Kotlin/collections|المجموعة]] التي استُدعيت عبرها وكذلك فهارسها.  
 
==أمثلة==
 
==أمثلة==
 
===استخدام الدالة <code>()foldIndexed</code> مع المصفوفات===
 
===استخدام الدالة <code>()foldIndexed</code> مع المصفوفات===
تعرف الشيفرة الآتية <nowiki/>[[Kotlin/Array|مصفوفة]] باسم <code>array</code>مكونة من ستة أعداد باستخدام الدالة <code>()arrayOf</code>، ثم تستخدم الدالة <code>()foldIndexed</code> على <code>array</code> بتمرير القيمة الأوّلية <code>0</code> إضافة إلى عملية الإضافة، ثم تطبع الناتج:<syntaxhighlight lang="kotlin">
+
تعرف الشيفرة الآتية <nowiki/>[[Kotlin/Array|مصفوفة]] باسم <code>array</code> مكونة من ستة أعداد باستخدام الدالة <code>()arrayOf</code>، ثم تستخدم الدالة <code>()foldIndexed</code> على <code>array</code> بتمرير القيمة الأوّلية <code>0</code> إضافة إلى عملية تُضيف القيمة المتراكمة الراهنة إلى حاصل ضرب عناصر <code>array</code> في فهارسها، ثم تطبع الناتج:<syntaxhighlight lang="kotlin">
 
fun main(args: Array<String>) {
 
fun main(args: Array<String>) {
 
val array = arrayOf(1,2,3,4,5,6)
 
val array = arrayOf(1,2,3,4,5,6)
  
val acc = array.fold(0) { sum, element -> sum + element }
+
val acc = array.foldIndexed(0) { acc , element , index -> acc + element*index }
print(acc) // 21
+
print(acc) // 719
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
سطر 74: سطر 62:
  
 
==مصادر==
 
==مصادر==
*[https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/fold.html الدالة  foldIndexed()‎ في التوثيق الرسميّ للمكتبة القياسيّة في لغة Kotlin.]
+
*[https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/fold-indexed.html الدالة  foldIndexed()‎ في التوثيق الرسميّ للمكتبة القياسيّة في لغة Kotlin.]
 
[[تصنيف:Kotlin]]
 
[[تصنيف:Kotlin]]
 
[[تصنيف:Kotlin Functions]]
 
[[تصنيف:Kotlin Functions]]

مراجعة 14:30، 17 مايو 2018

تُراكِم الدالةfoldIndexed()‎ القيم بدءًا من القيمة المُمرّرة initial وتطبّق العملية operation من اليسار إلى اليمين على القيمة المُتراكمة الراهنة وعلى العنصر التالي من المصفوفة أو المجموعة التي استُدعيت عبرها وكذلك فهرسه. وتُعيد القيمة النهائية.

البنية العامة

يمكن استدعاء الدالةfoldIndexed()‎ على المصفوفات.

inline fun <T, R> Array<out T>.foldIndexed(
    initial: R, 
    operation: (index: Int, acc: R, T) -> R
): R 
inline fun <R> ByteArray.foldIndexed(
    initial: R, 
    operation: (index: Int, acc: R, Byte) -> R
): R 
inline fun <R> ShortArray.foldIndexed(
    initial: R, 
    operation: (index: Int, acc: R, Short) -> R
): R 
inline fun <R> IntArray.foldIndexed(
    initial: R, 
    operation: (index: Int, acc: R, Int) -> R
): R 
inline fun <R> LongArray.foldIndexed(
    initial: R, 
    operation: (index: Int, acc: R, Long) -> R
): R 
inline fun <R> FloatArray.foldIndexed(
    initial: R, 
    operation: (index: Int, acc: R, Float) -> R
): R 
inline fun <R> DoubleArray.foldIndexed(
    initial: R, 
    operation: (index: Int, acc: R, Double) -> R
): R 
inline fun <R> BooleanArray.foldIndexed(
    initial: R, 
    operation: (index: Int, acc: R, Boolean) -> R
): R 
inline fun <R> CharArray.foldIndexed(
    initial: R, 
    operation: (index: Int, acc: R, Char) -> R
): R

يمكن استدعاء غلدالةfoldIndexed()‎ على المجموعات.

inline fun <T, R> Iterable<T>.foldIndexed(
    initial: R, 
    operation: (index: Int, acc: R, T) -> R
): R

يُلاحَظ وجود الكلمة المفتاحية inline للدلالة على أن هذه الدالة مباشرة، وللمزيد من التفاصيل راجع توثيق الدوال المباشرة (inline functions).

القيم المٌعادة

ناتج تطبيق العملية المُمرّرة operation تراكميًا على عناصر المصفوفة أو المجموعة التي استُدعيت عبرها وكذلك فهارسها.

أمثلة

استخدام الدالة ()foldIndexed مع المصفوفات

تعرف الشيفرة الآتية مصفوفة باسم array مكونة من ستة أعداد باستخدام الدالة ()arrayOf، ثم تستخدم الدالة ()foldIndexed على array بتمرير القيمة الأوّلية 0 إضافة إلى عملية تُضيف القيمة المتراكمة الراهنة إلى حاصل ضرب عناصر array في فهارسها، ثم تطبع الناتج:

fun main(args: Array<String>) {
val array = arrayOf(1,2,3,4,5,6)

val acc = array.foldIndexed(0) { acc , element , index -> acc + element*index }
print(acc) // 719
}

انظر أيضًا

مصادر