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

من موسوعة حسوب
اذهب إلى التنقل اذهب إلى البحث
سطر 1: سطر 1:
 
<noinclude>{{DISPLAYTITLE: الدالة <code>associate()‎</code> في لغة Kotlin}}</noinclude>
 
<noinclude>{{DISPLAYTITLE: الدالة <code>associate()‎</code> في لغة Kotlin}}</noinclude>
تعيد الدالة <code>associate()‎</code> قاموسًا (map) يحتوي ناتج تمرير عناصر المصفوفة أو المجموعة (<code>[[Kotlin/collections|Collection]]</code>) إلى الدالة <code>valueTransform</code> مُفهرسة بناتج تمرير عناصر المصفوفة أو المجموعة إلى الدالة <code>keySelector</code>.  
+
تعيد الدالة <code>associate()‎</code> قاموسًا (map) يحتوي أزواجًا "مفتاح-قيمة" ناتجة عن تمرير عناصر المصفوفة أو المجموعة (<code>[[Kotlin/collections|Collection]]</code>) إلى الدالة <code>transform</code>.  
  
ان كان لعنصرين نفس المفتاح (key) بعد تطبيق الدالة<code>keySelector</code> فإن الأخير هو الذي سيُضاف إلى القاموس.
+
إن كان لزوجين نفس المفتاح (key) فالأخير هو الذي سيُضاف إلى القاموس.
  
 
القاموس المُعاد يحافظ على نفس ترتيب التكرار الخاص بالمصفوفة أو المجموعة الأصلية.
 
القاموس المُعاد يحافظ على نفس ترتيب التكرار الخاص بالمصفوفة أو المجموعة الأصلية.
 
==البنية العامة==
 
==البنية العامة==
في حال استدعاء الدالة <code>associateBy()‎</code>عبر مصفوفة فستعيد قاموسًا (map) يحتوي عناصر المصفوفة مُفهرسة بناتج تمرير عناصر المصفوفة إلى الدالة <code>keySelector:</code><syntaxhighlight lang="kotlin">
+
تعيد الدالة <code>associate()‎</code> قاموسًا (map) يحتوي أزواجًا "مفتاح-قيمة" ناتجة عن تمرير عناصر المصفوفة إلى الدالة <code>transform</code>.<syntaxhighlight lang="kotlin">
inline fun <T, K> Array<out T>.associateBy(keySelector: (T) -> K): Map<K, T>
+
inline fun <T, K, V> Array<out T>.associate(
inline fun <K> ByteArray.associateBy(keySelector: (Byte) -> K): Map<K, Byte>
+
     transform: (T) -> Pair<K, V>
inline fun <K> ShortArray.associateBy(keySelector: (Short) -> K): Map<K, Short>
+
): Map<K, V>  
inline fun <K> IntArray.associateBy(keySelector: (Int) -> K): Map<K, Int>
+
inline fun <K, V> ByteArray.associate(
inline fun <K> LongArray.associateBy(keySelector: (Long) -> K): Map<K, Long>
+
     transform: (Byte) -> Pair<K, V>
inline fun <K> FloatArray.associateBy(keySelector: (Float) -> K): Map<K, Float>
 
inline fun <K> DoubleArray.associateBy(keySelector: (Double) -> K): Map<K, Double>
 
inline fun <K> BooleanArray.associateBy(keySelector: (Boolean) -> K): Map<K, Boolean>
 
inline fun <K> CharArray.associateBy(keySelector: (Char) -> K): Map<K, Char>
 
</syntaxhighlight>في حال استدعاء الدالة <code>associateBy()‎</code>عبر  مصفوفة فستعيد قاموسًا (map) يحتوي ناتج تمرير عناصر المصفوفة إلى الدالة <code>valueTransform</code> مُفهرسة بناتج تمرير عناصر المصفوفة إلى الدالة <code>keySelector:</code><syntaxhighlight lang="kotlin">
 
inline fun <T, K, V> Array<out T>.associateBy(
 
     keySelector: (T) -> K,  
 
    valueTransform: (T) -> V
 
): Map<K, V>
 
inline fun <K, V> ByteArray.associateBy(
 
     keySelector: (Byte) -> K,  
 
    valueTransform: (Byte) -> V
 
 
): Map<K, V>  
 
): Map<K, V>  
inline fun <K, V> ShortArray.associateBy(
+
inline fun <K, V> ShortArray.associate(
     keySelector: (Short) -> K,  
+
     transform: (Short) -> Pair<K, V>
    valueTransform: (Short) -> V
 
 
): Map<K, V>  
 
): Map<K, V>  
inline fun <K, V> IntArray.associateBy(
+
inline fun <K, V> IntArray.associate(
     keySelector: (Int) -> K,  
+
     transform: (Int) -> Pair<K, V>
    valueTransform: (Int) -> V
 
 
): Map<K, V>  
 
): Map<K, V>  
inline fun <K, V> LongArray.associateBy(
+
inline fun <K, V> LongArray.associate(
     keySelector: (Long) -> K,  
+
     transform: (Long) -> Pair<K, V>
    valueTransform: (Long) -> V
 
 
): Map<K, V>  
 
): Map<K, V>  
inline fun <K, V> FloatArray.associateBy(
+
inline fun <K, V> FloatArray.associate(
     keySelector: (Float) -> K,  
+
     transform: (Float) -> Pair<K, V>
    valueTransform: (Float) -> V
 
 
): Map<K, V>  
 
): Map<K, V>  
inline fun <K, V> DoubleArray.associateBy(
+
inline fun <K, V> DoubleArray.associate(
     keySelector: (Double) -> K,  
+
     transform: (Double) -> Pair<K, V>
    valueTransform: (Double) -> V
 
 
): Map<K, V>  
 
): Map<K, V>  
inline fun <K, V> BooleanArray.associateBy(
+
inline fun <K, V> BooleanArray.associate(
     keySelector: (Boolean) -> K,  
+
     transform: (Boolean) -> Pair<K, V>
    valueTransform: (Boolean) -> V
 
 
): Map<K, V>  
 
): Map<K, V>  
inline fun <K, V> CharArray.associateBy(
+
inline fun <K, V> CharArray.associate(
     keySelector: (Char) -> K,  
+
     transform: (Char) -> Pair<K, V>
    valueTransform: (Char) -> V
 
 
): Map<K, V>
 
): Map<K, V>
</syntaxhighlight>في حال استدعاء الدالة <code>associateBy()‎</code>عبر مجموعة (<code>[[Kotlin/collections|Collection]]</code>) فستعيد قاموسًا (map) يحتوي عناصر المجموعة مُفهرسة بناتج تمرير عناصر المجموعة إلى الدالة <code>keySelector:</code><syntaxhighlight lang="kotlin">
+
</syntaxhighlight>تعيد الدالة <code>associate()‎</code> قاموسًا (map) يحتوي أزواجًا "مفتاح-قيمة" ناتجة عن تمرير عناصر المجموعة (<code>[[Kotlin/collections|Collection]]</code>) إلى الدالة <code>transform</code>.<syntaxhighlight lang="kotlin">
inline fun <T, K> Iterable<T>.associateBy(keySelector: (T) -> K): Map<K, T>
+
inline fun <T, K, V> Iterable<T>.associate(
</syntaxhighlight>في حال استدعاء الدالة <code>associateBy()‎</code>عبر  مجموعة (<code>[[Kotlin/collections|Collection]]</code>) فستعيد قاموسًا (map) يحتوي ناتج تمرير عناصر  المجموعة (<code>[[Kotlin/collections|Collection]]</code>) إلى الدالة <code>valueTransform</code> مُفهرسة بناتج تمرير عناصر  المجموعة إلى الدالة <code>keySelector:</code><syntaxhighlight lang="kotlin">
+
    transform: (T) -> Pair<K, V>
inline fun <T, K, V> Iterable<T>.associateBy(
+
): Map<K, V>  
    keySelector: (T) -> K,
+
</syntaxhighlight>يُلاحَظ وجود الكلمة المفتاحية <code>inline</code> للدلالة على أن هذه الدالة مباشرة، وللمزيد من التفاصيل راجع <nowiki/>[[Kotlin/inline functions|توثيق الدوال المباشرة (inline functions)]].
    valueTransform: (T) -> V
 
): Map<K, V>
 
</syntaxhighlight>
 
  
 
==القيم المعادة==
 
==القيم المعادة==
 
قاموس. (انظر شرح الدالة أعلاه لمزيد من التفاصيل)
 
قاموس. (انظر شرح الدالة أعلاه لمزيد من التفاصيل)
 
==أمثلة==
 
==أمثلة==
===استخدام الدالة <code>()associateBy</code> مع اللوائح بتمرير دالة واحدة===
+
===استخدام الدالة <code>()associate</code> مع اللوائح ===
تعرف الشيفرة الآتية لائحة باسم <code>list</code> مكونة من ثلاثة أحرف باستخدام الدالة <code>()listOf</code>، ثم تنشئ قاموسًا باسم <code>map</code> بتمرير الدالة <code>()upper</code> (التي تحول الحروف إلى الحالة UpperCase) إلى<code>()associateBy</code>  ثم تطبع الناتج:<syntaxhighlight lang="kotlin">
+
تعرف الشيفرة الآتية لائحة باسم <code>list</code> مكونة من ثلاثة أحرف باستخدام الدالة <code>()listOf</code>، ثم تنشئ قاموسًا باسم <code>map</code> بتمرير الدالة <code>()toMap</code> (التي تحول الحروف إلى زوج مكون من الحرف في الحالة UpperCase ومن ترتيب الحرف) إلى<code>()associate</code>  ثم تطبع الناتج:<syntaxhighlight lang="kotlin">
 
fun main(args: Array<String>) {
 
fun main(args: Array<String>) {
 
     val list = listOf('a','b','c')
 
     val list = listOf('a','b','c')
     val upper: (Char) -> Char = {it.toUpperCase()}
+
     val toMap: (Char) -> Pair<Char,Int> = {Pair(it.toUpperCase(),it.toInt())}
    val map = list.associateBy(upper)
+
     val map = list.associate(toMap)
    println(map) // {A=a, B=b, C=c}
+
 
}
 
</syntaxhighlight>
 
===استخدام الدالة <code>()associateBy</code> مع اللوائح بتمرير دالتين===
 
تعرف الشيفرة الآتية لائحة باسم <code>list</code> مكونة من ثلاثة أحرف باستخدام الدالة <code>()listOf</code>، ثم تنشئ قاموسًا باسم <code>map</code> بتمرير الدالة <code>()upper</code> (التي تحول الحروف إلى الحالة UpperCase) والدالة<code>()multr</code> (التي تحول الحروف إلى عدد) إلى<code>()associateBy</code>  ثم تطبع الناتج:<syntaxhighlight lang="kotlin">
 
fun main(args: Array<String>) {
 
    val list = listOf('a','b','c')
 
    val upper: (Char) -> Char = {it.toUpperCase()}
 
    val mult: (Char) -> Int = {it.toInt()}
 
     val map = list.associateBy(upper, mult)
 
 
     println(map) // {A=97, B=98, C=99}
 
     println(map) // {A=97, B=98, C=99}
 
}
 
}
سطر 91: سطر 60:
  
 
==مصادر==
 
==مصادر==
*[https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/associate-by.html الدالة  associate()‎ في التوثيق الرسميّ للمكتبة القياسيّة في لغة Kotlin.]
+
*[https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/associate.html الدالة  associate()‎ في التوثيق الرسميّ للمكتبة القياسيّة في لغة Kotlin.]
 
[[تصنيف:Kotlin]]
 
[[تصنيف:Kotlin]]
 
[[تصنيف:Kotlin Functions]]
 
[[تصنيف:Kotlin Functions]]

مراجعة 19:25، 10 مايو 2018

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

إن كان لزوجين نفس المفتاح (key) فالأخير هو الذي سيُضاف إلى القاموس.

القاموس المُعاد يحافظ على نفس ترتيب التكرار الخاص بالمصفوفة أو المجموعة الأصلية.

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

تعيد الدالة associate()‎ قاموسًا (map) يحتوي أزواجًا "مفتاح-قيمة" ناتجة عن تمرير عناصر المصفوفة إلى الدالة transform.

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

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

inline fun <T, K, V> Iterable<T>.associate(
    transform: (T) -> Pair<K, V>
): Map<K, V>

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

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

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

أمثلة

استخدام الدالة ()associate مع اللوائح

تعرف الشيفرة الآتية لائحة باسم list مكونة من ثلاثة أحرف باستخدام الدالة ()listOf، ثم تنشئ قاموسًا باسم map بتمرير الدالة ()toMap (التي تحول الحروف إلى زوج مكون من الحرف في الحالة UpperCase ومن ترتيب الحرف) إلى()associate ثم تطبع الناتج:

fun main(args: Array<String>) {
    val list = listOf('a','b','c')
    val toMap: (Char) -> Pair<Char,Int> = {Pair(it.toUpperCase(),it.toInt())}
    val map = list.associate(toMap)

    println(map) // {A=97, B=98, C=99}
}

انظر أيضًا

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

مصادر