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

من موسوعة حسوب
اذهب إلى التنقل اذهب إلى البحث
(أنشأ الصفحة ب'<noinclude>{{DISPLAYTITLE: الدالة <code>component1()‎</code> في لغة Kotlin}}</noinclude> تعيد الدالة <code>component1()‎</code> قاموسًا...')
 
ط
 
(7 مراجعات متوسطة بواسطة 3 مستخدمين غير معروضة)
سطر 1: سطر 1:
<noinclude>{{DISPLAYTITLE: الدالة <code>component1()‎</code> في لغة Kotlin}}</noinclude>
+
<noinclude>{{DISPLAYTITLE: الدالة <code>component1()‎</code> في Kotlin}}</noinclude>
تعيد الدالة <code>component1()‎</code> قاموسًا متغيرا (mutable map) يحتوي ناتج تمرير عناصر المصفوفة أو المجموعة (<code>[[Kotlin/collections|Collection]]</code>) إلى الدالة <code>transform</code>.  
+
تعيد الدالة <code>component1()‎</code> العنصر الأول من المصفوفة أو المفتاح (key) لأول زوج (مفتاح/قيمة) في الخريطة (map) التي استدعيت معها. تسمح هذه الدالة باستخدام التصريحات التفكيكية (destructuring declarations) عند العمل على الخرائط. 
 +
==البنية العامة==
 +
يمكن استدعاء الدالة <code>component1()‎</code> مع المصفوفات والقوائم (list):<syntaxhighlight lang="kotlin">
 +
inline operator fun <T> Array<out T>.component1(): T
 +
inline operator fun ByteArray.component1(): Byte
 +
inline operator fun ShortArray.component1(): Short
 +
inline operator fun IntArray.component1(): Int
 +
inline operator fun LongArray.component1(): Long
 +
inline operator fun FloatArray.component1(): Float
 +
inline operator fun DoubleArray.component1(): Double
 +
inline operator fun BooleanArray.component1(): Boolean
 +
inline operator fun CharArray.component1(): Char
 +
inline operator fun <T> List<T>.component1(): T
  
ان كان لعنصرين نفس المفتاح (key) فإن الأخير هو الذي سيُضاف إلى القاموس.
 
==البنية العامة==
 
في حال استدعاء الدالة <code>component1()‎</code>عبر مصفوفة فستعيد القاموس المتغير <code>destination</code> وأزواجه توفرها الدالة <code>transform()‎</code> مُطبقةً على عناصر المصفوفة:<syntaxhighlight lang="kotlin">
 
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
 
 
</syntaxhighlight>
 
</syntaxhighlight>
  
في حال استدعاء الدالة <code>component1()‎</code>عبر مجموعة (<code>[[Kotlin/collections|Collection]]</code>)  فستعيد القاموس المتغير <code>destination</code> وأزواجه توفرها الدالة <code>keySelector()‎</code> مُطبقةً على عناصر المجموعة:<syntaxhighlight lang="kotlin">
+
ويمكن استدعاؤها مع الخرائط (maps):<syntaxhighlight lang="kotlin">
inline fun <T, K, V, M : MutableMap<in K, in V>> Iterable<T>.associateTo(
+
inline operator fun <K, V> Entry<K, V>.component1(): K  
    destination: M,
+
 
    transform: (T) -> Pair<K, V>
+
</syntaxhighlight>وجود الكلمة المفتاحية <code>inline</code> تدل على أنَّ هذه الدالة مباشرة (للمزيد من التفاصيل، راجع <nowiki/>[[Kotlin/inline functions|توثيق الدوال المباشرة (inline functions)]]).
): M
 
</syntaxhighlight>يُلاحَظ وجود الكلمة المفتاحية <code>inline</code> للدلالة على أن هذه الدالة مباشرة، وللمزيد من التفاصيل راجع <nowiki/>[[Kotlin/inline functions|توثيق الدوال المباشرة (inline functions)]].
 
 
==القيم المعادة==
 
==القيم المعادة==
قاموس. (انظر شرح الدالة أعلاه لمزيد من التفاصيل)
+
يُعاد العنصر الأول من المصفوفة أو قيمة المفتاح (key) لأول زوج (مفتاح/قيمة) في الخريطة (map) التي استدعيت معها.
 
==أمثلة==
 
==أمثلة==
===استخدام الدالة <code>()component1</code> مع اللوائح ===
+
استخدام الدالة <code>()component1</code> مع القوائم:<syntaxhighlight lang="kotlin">
تعرف الشيفرة الآتية لائحة باسم <code>list</code> مكونة من أربعة أعداد باستخدام الدالة <code>()listOf</code>، ثم تنشئ قاموسًا متغيرًا باسم <code>map</code> بتمرير الدالة <code>()func</code> (التي تحول عددًا إلى زوج) إلى<code>()associateTo</code>  ثم تطبع الناتج:<syntaxhighlight lang="kotlin">
 
 
fun main(args: Array<String>) {
 
fun main(args: Array<String>) {
     val list = listOf(1,2,3,4)
+
     val list = listOf('a', 'b', 'c', 'd', 'e')
     val map = mutableMapOf<Int, Int>()
+
     println(list.component1()) // a
    val func: (Int) -> Pair<Int,Int> = {Pair(it + 10,it * 4)}
+
}
     list.associateTo(map,func)
+
</syntaxhighlight>استخدام الدالة <code>()component1</code> مع الخرائط:<syntaxhighlight lang="kotlin">
 
+
fun main(args: Array<String>) {
    println(map) // {11=4, 12=8, 13=12, 14=16}
+
    val map = hashMapOf("foo" to 1, "bar" to 2)
 +
     for (x in map) {
 +
        println(x.component1()) // bar foo
 +
    }
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
 
==انظر أيضًا==
 
==انظر أيضًا==
*<code>[[Kotlin/collections/associateByTo|associateByTo()]]</code>‎: تعيد قاموسًا (map).
+
*الدالة <code>[[Kotlin/collections/associate|associate()]]</code>‎: تعيد خريطةً (map) تحوي أزواجًا من "مفتاح/قيمة" ناتجة عن تمرير عناصر مصفوفة أو مجموعة ([[Kotlin/collections|Collection]]) إلى الدالة <code>()transform</code>.
*<code>[[Kotlin/collections/associateBy|associateBy()]]</code>‎: تعيد قاموسًا (map).  
+
* الدالة <code>[[Kotlin/collections/asList|asList()]]</code>‎: تعيد قائمة تُغلّف المصفوفة الأصليَّة.
*<code>[[Kotlin/collections/associate|associate()]]</code>‎: تعيد قاموسًا (map).  
+
*الدالة <code>[[Kotlin/collections/contains|contains()]]</code>: تتحقق إن كانت [[Kotlin/Array|المصفوفة]] أو [[Kotlin/collections|المجموعة]] التي استُدعيت معها تحتوي على العنصر الممرّر إليها، أو إن كانت الخريطة التي استدعيت معها تحتوي على ذلك العنصر كمفتاح (key).
*<code>[[Kotlin/collections/asList|asList()]]</code>: تعيد لائحة والتي تُغلّف المصفوفة الاصلية.
+
*الخاصيّة <code>[[Kotlin/Array/size|Array.size]]</code>: تعبِّر عن عدد العناصر الموجودة في المصفوفة.  
 
 
* <code>[[Kotlin/collections/asIterable|asIterable()]]</code>‎: تُنشئ نسخة تكرارية (Iterable instance) والتي تُغلّف المصفوفة الاصلية وتعيد عناصرها عندما تُستخدم في حلقات التكرار (مثل<code>[[Kotlin/control flow#.D8.AA.D8.B9.D8.A8.D9.8A.D8.B1 for|for]]</code>).
 
  
 
==مصادر==
 
==مصادر==
*[https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/associate-to.html الدالة  associateTo()‎ في التوثيق الرسميّ للمكتبة القياسيّة في لغة Kotlin.]
+
*[https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/component1.html صفحة الدالة component1()‎ في التوثيق الرسميّ للمكتبة القياسيّة في لغة Kotlin.]
 
[[تصنيف:Kotlin]]
 
[[تصنيف:Kotlin]]
[[تصنيف:Kotlin Functions]]
+
[[تصنيف:Kotlin Function]]
 +
[[تصنيف:Kotlin Collection]]

المراجعة الحالية بتاريخ 07:27، 8 سبتمبر 2018

تعيد الدالة component1()‎ العنصر الأول من المصفوفة أو المفتاح (key) لأول زوج (مفتاح/قيمة) في الخريطة (map) التي استدعيت معها. تسمح هذه الدالة باستخدام التصريحات التفكيكية (destructuring declarations) عند العمل على الخرائط. 

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

يمكن استدعاء الدالة component1()‎ مع المصفوفات والقوائم (list):

inline operator fun <T> Array<out T>.component1(): T 
inline operator fun ByteArray.component1(): Byte 
inline operator fun ShortArray.component1(): Short
inline operator fun IntArray.component1(): Int 
inline operator fun LongArray.component1(): Long 
inline operator fun FloatArray.component1(): Float 
inline operator fun DoubleArray.component1(): Double 
inline operator fun BooleanArray.component1(): Boolean 
inline operator fun CharArray.component1(): Char 
inline operator fun <T> List<T>.component1(): T

ويمكن استدعاؤها مع الخرائط (maps):

inline operator fun <K, V> Entry<K, V>.component1(): K

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

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

يُعاد العنصر الأول من المصفوفة أو قيمة المفتاح (key) لأول زوج (مفتاح/قيمة) في الخريطة (map) التي استدعيت معها.

أمثلة

استخدام الدالة ()component1 مع القوائم:

fun main(args: Array<String>) {
    val list = listOf('a', 'b', 'c', 'd', 'e')
    println(list.component1()) // a
}

استخدام الدالة ()component1 مع الخرائط:

fun main(args: Array<String>) {
    val map = hashMapOf("foo" to 1, "bar" to 2)
    for (x in map) {
        println(x.component1()) // bar foo
    }
}

انظر أيضًا

  • الدالة associate()‎: تعيد خريطةً (map) تحوي أزواجًا من "مفتاح/قيمة" ناتجة عن تمرير عناصر مصفوفة أو مجموعة (Collection) إلى الدالة ()transform.
  • الدالة asList()‎: تعيد قائمة تُغلّف المصفوفة الأصليَّة.
  • الدالة contains()‎: تتحقق إن كانت المصفوفة أو المجموعة التي استُدعيت معها تحتوي على العنصر الممرّر إليها، أو إن كانت الخريطة التي استدعيت معها تحتوي على ذلك العنصر كمفتاح (key).
  • الخاصيّة Array.size: تعبِّر عن عدد العناصر الموجودة في المصفوفة.

مصادر