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

من موسوعة حسوب
اذهب إلى التنقل اذهب إلى البحث
(أنشأ الصفحة ب'<noinclude>{{DISPLAYTITLE: الدالة <code>component1()‎</code> في لغة Kotlin}}</noinclude> تعيد الدالة <code>component1()‎</code> قاموسًا...')
 
سطر 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 component) الخاصة بمدخل القاموس (map entry)، وتسمح هذه الدالة باستخدام التصريحات التفكيكية (destructuring declarations) عند العمل على القواميس.
 +
==البنية العامة==
 +
يمكن استدعاء الدالة <code>component1()‎</code>عبر المصفوفات واللوائح (<code>List</code>):<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">
+
ويمكن استدعاء الدالة <code>component1()‎</code>عبر القواميس (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>
 
): M
 
 
</syntaxhighlight>يُلاحَظ وجود الكلمة المفتاحية <code>inline</code> للدلالة على أن هذه الدالة مباشرة، وللمزيد من التفاصيل راجع <nowiki/>[[Kotlin/inline functions|توثيق الدوال المباشرة (inline functions)]].
 
</syntaxhighlight>يُلاحَظ وجود الكلمة المفتاحية <code>inline</code> للدلالة على أن هذه الدالة مباشرة، وللمزيد من التفاصيل راجع <nowiki/>[[Kotlin/inline functions|توثيق الدوال المباشرة (inline functions)]].
 
==القيم المعادة==
 
==القيم المعادة==
قاموس. (انظر شرح الدالة أعلاه لمزيد من التفاصيل)
+
العنصر الأول من المصفوفة التي استُدعيت عبرها، وفي حال استُدعيت عبر قاموس فستعيد مركبة المفتاح (key component) الخاصة بمدخل القاموس (map entry).
 
==أمثلة==
 
==أمثلة==
 
===استخدام الدالة <code>()component1</code> مع اللوائح ===
 
===استخدام الدالة <code>()component1</code> مع اللوائح ===
تعرف الشيفرة الآتية لائحة باسم <code>list</code> مكونة من أربعة أعداد باستخدام الدالة <code>()listOf</code>، ثم تنشئ قاموسًا متغيرًا باسم <code>map</code> بتمرير الدالة <code>()func</code> (التي تحول عددًا إلى زوج) إلى<code>()associateTo</code> ثم تطبع الناتج:<syntaxhighlight lang="kotlin">
+
تعرف الشيفرة الآتية لائحة باسم <code>list</code> مكونة من خمسة حروف باستخدام الدالة <code>()listOf</code>، ثم تستخدم الدالة <code>()component1</code> للحصول على العنصر الأول من اللائحة <code>list</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>
  
    println(map) // {11=4, 12=8, 13=12, 14=16}
+
===استخدام الدالة <code>()component1</code> مع القواميس ===
 +
تعرف الشيفرة الآتية قاموسًا باسم <code>map</code> مكونة من  زوجين باستخدام الدالة <code>()hashMapOf</code>، ثم تستخدم الدالة <code>()component1</code> للحصول على المركب الأول من مداخل القاموس (entries) في الحلقة <code>[[Kotlin/control flow#.D8.AA.D8.B9.D8.A8.D9.8A.D8.B1 for|for]]</code> ثم تطبع الناتج:<syntaxhighlight lang="kotlin">
 +
fun main(args: Array<String>) {
 +
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/associateBy|associateBy()]]</code>‎: تعيد قاموسًا (map).
 
 
*<code>[[Kotlin/collections/associate|associate()]]</code>‎: تعيد قاموسًا (map).  
 
*<code>[[Kotlin/collections/associate|associate()]]</code>‎: تعيد قاموسًا (map).  
*<code>[[Kotlin/collections/asList|asList()]]</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 Functions]]

مراجعة 13:27، 12 مايو 2018

تعيد الدالة component1()‎ العنصر الأول من المصفوفة التي استُدعيت عبرها، وفي حال استُدعيت عبر قاموس فستعيد مركبة المفتاح (key component) الخاصة بمدخل القاموس (map entry)، وتسمح هذه الدالة باستخدام التصريحات التفكيكية (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

ويمكن استدعاء الدالة component1()‎عبر القواميس (maps):

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

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

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

العنصر الأول من المصفوفة التي استُدعيت عبرها، وفي حال استُدعيت عبر قاموس فستعيد مركبة المفتاح (key component) الخاصة بمدخل القاموس (map entry).

أمثلة

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

تعرف الشيفرة الآتية لائحة باسم list مكونة من خمسة حروف باستخدام الدالة ()listOf، ثم تستخدم الدالة ()component1 للحصول على العنصر الأول من اللائحة list ثم تطبع الناتج:

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

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

تعرف الشيفرة الآتية قاموسًا باسم map مكونة من زوجين باستخدام الدالة ()hashMapOf، ثم تستخدم الدالة ()component1 للحصول على المركب الأول من مداخل القاموس (entries) في الحلقة for ثم تطبع الناتج:

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

انظر أيضًا

مصادر