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

من موسوعة حسوب
اذهب إلى التنقل اذهب إلى البحث
(أنشأ الصفحة ب'<noinclude>{{DISPLAYTITLE: الدالة <code>associateTo()‎</code> في لغة Kotlin}}</noinclude> تعيد الدالة <code>associateByTo()‎</code> قاموسً...')
 
سطر 1: سطر 1:
 
<noinclude>{{DISPLAYTITLE: الدالة <code>associateTo()‎</code> في لغة Kotlin}}</noinclude>
 
<noinclude>{{DISPLAYTITLE: الدالة <code>associateTo()‎</code> في لغة Kotlin}}</noinclude>
تعيد الدالة <code>associateByTo()‎</code> قاموسًا متغيرا (mutable map) يحتوي ناتج تمرير عناصر المصفوفة أو المجموعة (<code>[[Kotlin/collections|Collection]]</code>) إلى الدالة <code>valueTransform</code> مُفهرسة بناتج تمرير عناصر المصفوفة أو المجموعة إلى الدالة <code>keySelector</code>.  
+
تعيد الدالة <code>associateByTo()‎</code> قاموسًا متغيرا (mutable map) يحتوي ناتج تمرير عناصر المصفوفة أو المجموعة (<code>[[Kotlin/collections|Collection]]</code>) إلى الدالة <code>transform</code>.  
  
 
ان كان لعنصرين نفس المفتاح (key) بعد تطبيق الدالة<code>keySelector</code> فإن الأخير هو الذي سيُضاف إلى القاموس.
 
ان كان لعنصرين نفس المفتاح (key) بعد تطبيق الدالة<code>keySelector</code> فإن الأخير هو الذي سيُضاف إلى القاموس.
 
==البنية العامة==
 
==البنية العامة==
في حال استدعاء الدالة <code>associateByTo()‎</code>عبر مصفوفة فستعيد قاموسًا متغيرًا (mutable map) مفاتيحه توفرها الدالة <code>keySelector()‎</code> مُطبقةً على عناصر المصفوفة وقيمه هي عناصر المصفوفة:<syntaxhighlight lang="kotlin">
+
في حال استدعاء الدالة <code>associateByTo()‎</code>عبر مصفوفة فستعيد القاموس المتغير <code>destination</code> أزواجه توفرها الدالة <code>transform()‎</code> مُطبقةً على عناصر المصفوفة:<syntaxhighlight lang="kotlin">
inline fun <T, K, M : MutableMap<in K, in T>> Array<out T>.associateByTo(
+
inline fun <T, K, V, M : MutableMap<in K, in V>> Array<out T>.associateTo(
 
     destination: M,  
 
     destination: M,  
     keySelector: (T) -> K
+
     transform: (T) -> Pair<K, V>
 
): M  
 
): M  
inline fun <K, M : MutableMap<in K, in Byte>> ByteArray.associateByTo(
+
inline fun <K, V, M : MutableMap<in K, in V>> ByteArray.associateTo(
 
     destination: M,  
 
     destination: M,  
     keySelector: (Byte) -> K
+
     transform: (Byte) -> Pair<K, V>
 
): M  
 
): M  
inline fun <K, M : MutableMap<in K, in Short>> ShortArray.associateByTo(
+
inline fun <K, V, M : MutableMap<in K, in V>> ShortArray.associateTo(
 
     destination: M,  
 
     destination: M,  
     keySelector: (Short) -> K
+
     transform: (Short) -> Pair<K, V>
 
): M  
 
): M  
inline fun <K, M : MutableMap<in K, in Int>> IntArray.associateByTo(
+
inline fun <K, V, M : MutableMap<in K, in V>> IntArray.associateTo(
 
     destination: M,  
 
     destination: M,  
     keySelector: (Int) -> K
+
     transform: (Int) -> Pair<K, V>
 
): M  
 
): M  
inline fun <K, M : MutableMap<in K, in Long>> LongArray.associateByTo(
+
inline fun <K, V, M : MutableMap<in K, in V>> LongArray.associateTo(
 
     destination: M,  
 
     destination: M,  
     keySelector: (Long) -> K
+
     transform: (Long) -> Pair<K, V>
 
): M  
 
): M  
inline fun <K, M : MutableMap<in K, in Float>> FloatArray.associateByTo(
+
inline fun <K, V, M : MutableMap<in K, in V>> FloatArray.associateTo(
 
     destination: M,  
 
     destination: M,  
     keySelector: (Float) -> K
+
     transform: (Float) -> Pair<K, V>
 
): M  
 
): M  
inline fun <K, M : MutableMap<in K, in Double>> DoubleArray.associateByTo(
+
inline fun <K, V, M : MutableMap<in K, in V>> DoubleArray.associateTo(
 
     destination: M,  
 
     destination: M,  
     keySelector: (Double) -> K
+
     transform: (Double) -> Pair<K, V>
 
): M  
 
): M  
inline fun <K, M : MutableMap<in K, in Boolean>> BooleanArray.associateByTo(
+
inline fun <K, V, M : MutableMap<in K, in V>> BooleanArray.associateTo(
 
     destination: M,  
 
     destination: M,  
     keySelector: (Boolean) -> K
+
     transform: (Boolean) -> Pair<K, V>
 
): M  
 
): M  
inline fun <K, M : MutableMap<in K, in Char>> CharArray.associateByTo(
+
inline fun <K, V, M : MutableMap<in K, in V>> CharArray.associateTo(
 
     destination: M,  
 
     destination: M,  
     keySelector: (Char) -> K
+
     transform: (Char) -> Pair<K, V>
 
): M  
 
): M  
</syntaxhighlight>في حال استدعاء الدالة <code>associateByTo()‎</code>عبر مصفوفة فستعيد قاموسًا متغيرًا (mutable map) مفاتيحه توفرها الدالة <code>keySelector()‎</code> مُطبقةً على عناصر المصفوفة وقيمه توفرها الدالة <code>valueTransform()‎</code> مُطبقةً على عناصر المصفوفة:<syntaxhighlight lang="kotlin">
+
</syntaxhighlight>
inline fun <T, K, V, M : MutableMap<in K, in V>> Array<out T>.associateByTo(
+
 
    destination: M,
+
في حال استدعاء الدالة <code>associateByTo()‎</code>عبر مجموعة (<code>[[Kotlin/collections|Collection]]</code>)  فستعيد القاموس المتغير <code>destination</code> أزواجه توفرها الدالة <code>keySelector()‎</code> مُطبقةً على عناصر المجموعة:<syntaxhighlight lang="kotlin">
    keySelector: (T) -> K,
+
inline fun <T, K, V, M : MutableMap<in K, in V>> Iterable<T>.associateTo(
    valueTransform: (T) -> V
 
): M
 
inline fun <K, V, M : MutableMap<in K, in V>> ByteArray.associateByTo(
 
    destination: M,
 
    keySelector: (Byte) -> K,
 
    valueTransform: (Byte) -> V
 
): M
 
inline fun <K, V, M : MutableMap<in K, in V>> ShortArray.associateByTo(
 
    destination: M,
 
    keySelector: (Short) -> K,
 
    valueTransform: (Short) -> V
 
): M
 
inline fun <K, V, M : MutableMap<in K, in V>> IntArray.associateByTo(
 
    destination: M,
 
    keySelector: (Int) -> K,
 
    valueTransform: (Int) -> V
 
): M
 
inline fun <K, V, M : MutableMap<in K, in V>> LongArray.associateByTo(
 
    destination: M,
 
    keySelector: (Long) -> K,
 
    valueTransform: (Long) -> V
 
): M
 
inline fun <K, V, M : MutableMap<in K, in V>> FloatArray.associateByTo(
 
    destination: M,
 
    keySelector: (Float) -> K,
 
    valueTransform: (Float) -> V
 
): M
 
inline fun <K, V, M : MutableMap<in K, in V>> DoubleArray.associateByTo(
 
    destination: M,
 
    keySelector: (Double) -> K,
 
    valueTransform: (Double) -> V
 
): M
 
inline fun <K, V, M : MutableMap<in K, in V>> BooleanArray.associateByTo(
 
    destination: M,
 
    keySelector: (Boolean) -> K,
 
    valueTransform: (Boolean) -> V
 
): M
 
inline fun <K, V, M : MutableMap<in K, in V>> CharArray.associateByTo(
 
 
     destination: M,  
 
     destination: M,  
     keySelector: (Char) -> K,  
+
     transform: (T) -> Pair<K, V>
    valueTransform: (Char) -> V
 
 
): M  
 
): M  
</syntaxhighlight>في حال استدعاء الدالة <code>associateByTo()‎</code>عبر مجموعة (<code>[[Kotlin/collections|Collection]]</code>)  فستعيد قاموسًا متغيرًا (mutable map) مفاتيحه توفرها الدالة <code>keySelector()‎</code> مُطبقةً على عناصر المجموعة وقيمها هي عناصر المجموعة:<syntaxhighlight lang="kotlin">
 
inline fun <T, K, M : MutableMap<in K, in T>> Iterable<T>.associateByTo(
 
    destination: M,
 
    keySelector: (T) -> K
 
):
 
</syntaxhighlight>في حال استدعاء الدالة <code>associateByTo()‎</code>عبر مجموعة (<code>[[Kotlin/collections|Collection]]</code>) فستعيد قاموسًا متغيرًا (mutable map) مفاتيحه توفرها الدالة <code>keySelector()‎</code> مُطبقةً على عناصر المجموعة وقيمه توفرها الدالة <code>valueTransform()‎</code> مُطبقةً على عناصر المجموعة:<syntaxhighlight lang="kotlin">
 
inline fun <T, K, V, M : MutableMap<in K, in V>> Iterable<T>.associateByTo(
 
    destination: M,
 
    keySelector: (T) -> K,
 
    valueTransform: (T) -> V
 
): M
 
 
</syntaxhighlight>يُلاحَظ وجود الكلمة المفتاحية <code>inline</code> للدلالة على أن هذه الدالة مباشرة، وللمزيد من التفاصيل راجع <nowiki/>[[Kotlin/inline functions|توثيق الدوال المباشرة (inline functions)]].
 
</syntaxhighlight>يُلاحَظ وجود الكلمة المفتاحية <code>inline</code> للدلالة على أن هذه الدالة مباشرة، وللمزيد من التفاصيل راجع <nowiki/>[[Kotlin/inline functions|توثيق الدوال المباشرة (inline functions)]].
 
 
==القيم المعادة==
 
==القيم المعادة==
 
قاموس. (انظر شرح الدالة أعلاه لمزيد من التفاصيل)
 
قاموس. (انظر شرح الدالة أعلاه لمزيد من التفاصيل)

مراجعة 12:10، 11 مايو 2018

تعيد الدالة associateByTo()‎ قاموسًا متغيرا (mutable map) يحتوي ناتج تمرير عناصر المصفوفة أو المجموعة (Collection) إلى الدالة transform.

ان كان لعنصرين نفس المفتاح (key) بعد تطبيق الدالةkeySelector فإن الأخير هو الذي سيُضاف إلى القاموس.

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

في حال استدعاء الدالة associateByTo()‎عبر مصفوفة فستعيد القاموس المتغير destination أزواجه توفرها الدالة transform()‎ مُطبقةً على عناصر المصفوفة:

inline fun <T, K, V, M : MutableMap<in K, in V>> Array<out T>.associateTo(
    destination: M, 
    transform: (T) -> Pair<K, V>
): M 
inline fun <K, V, M : MutableMap<in K, in V>> ByteArray.associateTo(
    destination: M, 
    transform: (Byte) -> Pair<K, V>
): M 
inline fun <K, V, M : MutableMap<in K, in V>> ShortArray.associateTo(
    destination: M, 
    transform: (Short) -> Pair<K, V>
): M 
inline fun <K, V, M : MutableMap<in K, in V>> IntArray.associateTo(
    destination: M, 
    transform: (Int) -> Pair<K, V>
): M 
inline fun <K, V, M : MutableMap<in K, in V>> LongArray.associateTo(
    destination: M, 
    transform: (Long) -> Pair<K, V>
): M 
inline fun <K, V, M : MutableMap<in K, in V>> FloatArray.associateTo(
    destination: M, 
    transform: (Float) -> Pair<K, V>
): M 
inline fun <K, V, M : MutableMap<in K, in V>> DoubleArray.associateTo(
    destination: M, 
    transform: (Double) -> Pair<K, V>
): M 
inline fun <K, V, M : MutableMap<in K, in V>> BooleanArray.associateTo(
    destination: M, 
    transform: (Boolean) -> Pair<K, V>
): M 
inline fun <K, V, M : MutableMap<in K, in V>> CharArray.associateTo(
    destination: M, 
    transform: (Char) -> Pair<K, V>
): M

في حال استدعاء الدالة associateByTo()‎عبر مجموعة (Collection)  فستعيد القاموس المتغير destination أزواجه توفرها الدالة keySelector()‎ مُطبقةً على عناصر المجموعة:

inline fun <T, K, V, M : MutableMap<in K, in V>> Iterable<T>.associateTo(
    destination: M, 
    transform: (T) -> Pair<K, V>
): M

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

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

قاموس. (انظر شرح الدالة أعلاه لمزيد من التفاصيل)

أمثلة

استخدام الدالة ()associateByToTo مع اللوائح بتمرير دالة واحدة

تعرف الشيفرة الآتية لائحة باسم list مكونة من أربعة أعداد باستخدام الدالة ()listOf، ثم تنشئ قاموسًا متغيرًا باسم map بتمرير الدالة ()func (التي تضرب الأعداد في 4) إلى()associateByToTo ثم تطبع الناتج:

fun main(args: Array<String>) {
    val list = listOf(1,2,3,4)
    val map = mutableMapOf<Int, Int>()
    val func: (Int) -> Int = {it*4}

    list.associateByTo(map,func)

    println(map) // {4=1, 8=2, 12=3, 16=4}
}

استخدام الدالة ()associateByToTo مع اللوائح بتمرير دالتين

تعرف الشيفرة الآتية لائحة باسم list مكونة من ثلاثة أحرف باستخدام الدالة ()listOf، ثم تنشئ قاموسًا متغيرًا باسم map بتمرير الدالة ()func (التي تضرب الأعداد في 4) والدالة()func2 (التي تضيف 4 إلى العدد الممرر إليها) إلى()associateByToTo ثم تطبع الناتج:

fun main(args: Array<String>) {
    val list = listOf(1,2,3,4)
    val map = mutableMapOf<Int, Int>()
    val func: (Int) -> Int = {it*4}
    val func2: (Int) -> Int = {it + 4}

    list.associateByTo(map,func,func2)

    println(map) // {4=5, 8=6, 12=7, 16=8}
}

انظر أيضًا

  • associateBy()‎: تعيد قاموسًا (map).
  • associate()‎: تعيد قاموسًا (map).
  • asList()‎: تعيد لائحة والتي تُغلّف المصفوفة الاصلية.
  • asIterable()‎: تُنشئ نسخة تكرارية (Iterable instance) والتي تُغلّف المصفوفة الاصلية وتعيد عناصرها عندما تُستخدم في حلقات التكرار (مثلfor).

مصادر