أساسيات البرمجة الوظيفية
في هذه الصفحة 8 موضوعات
نص الدروس والتمارين باللغة الإنجليزية، بينما تبقى واجهة الموقع وعناوين الدروس بالعربية.
نظرة عامة
الموضوعات
- تعبيرات Lambda
- الواجهات الوظيفية
- مراجع الدوال (method references)
تعلّم أولًا
54 دقيقة قراءة · 2.5 ساعة تدريب (تقدير)
مخرجات التعلم
- Write lambda expressions and explain them as implementations of a functional interface.
- Use
Predicate,Function,Consumer,Supplier, andBiFunction. - Replace lambdas with method references where clearer.
الدروس
المختبر والتمارين
Lab
BEG-035-LAB1 · Filters
Outcomes: 1–3 · Time: ~1.5 h Rewrite anonymous Comparator classes and custom filter loops using lambdas and method references. Keep a before/after copy. Acceptance: same behavior, less code.
Exercises (assessment: four exercises)
| ID | Outcome | Task |
|---|---|---|
| BEG-035-EX01 | 1 | Convert an anonymous Runnable that prints "hello" into a lambda; explain which interface it implements. |
| BEG-035-EX02 | 2 | Write one lambda for each: Predicate<Integer> (even), Function<String,Integer> (length), Consumer<String> (print upper-case), Supplier<String> ("ready"), BiFunction<Integer,Integer,Integer> (max). |
| BEG-035-EX03 | 3 | Rewrite as method references: s -> s.toUpperCase(), s -> System.out.println(s), () -> new ArrayList<String>(). |
| BEG-035-EX04 | 1 | Write filter(List<Integer>, Predicate<Integer>) returning a new list; use it for positives and for numbers over 100. |
أظهر الحلول
- EX01
Runnable r = () -> System.out.println("hello");It implementsRunnable, whose single method isrun. - EX02
n -> n % 2 == 0;s -> s.length();s -> System.out.println(s.toUpperCase());() -> "ready";(a, b) -> Math.max(a, b). - EX03
String::toUpperCase;System.out::println;ArrayList::new(asSupplier<List<String>>). - EX04 Loop,
if (p.test(x)) result.add(x);. Calls:filter(list, n -> n > 0),filter(list, n -> n > 100). - Mistakes (long lambdas, captured variables) are covered in L02 and demo L05.
الاختبار
Outcomes: O1 lambdas and functional interfaces · O2 standard interfaces · O3 method references.
اختبار ذاتي لك: لا توجد درجات، ولا يُحفظ أو يُرسل أي شيء. اختر إجابة ثم تحقق منها، ويمكنك المحاولة مرة أخرى كما تشاء.
الجزء العملي
Four exercises BEG-035-EX01 to EX04 plus the lab. Pass: all four exercises correct and the lab behaving the same as before with less code.
أخطاء شائعة
Multi-line lambdas that should be methods; modifying captured variables.
التقييم
4 exercises.
الصلة بالمسار الوظيفي
Streams (BEG-036), CompletableFuture (MID-006), Spring configuration.