انتقل إلى المحتوى
أمين أبوزيد
BEG-035

أساسيات البرمجة الوظيفية

  • 5 درس
  • 54 دقيقة قراءة
  • 2.5 ساعة تدريب (تقدير)
في هذه الصفحة 8 موضوعات

نص الدروس والتمارين باللغة الإنجليزية، بينما تبقى واجهة الموقع وعناوين الدروس بالعربية.

نظرة عامة

الموضوعات

  • تعبيرات Lambda
  • الواجهات الوظيفية
  • مراجع الدوال (method references)

تعلّم أولًا

54 دقيقة قراءة · 2.5 ساعة تدريب (تقدير)

مخرجات التعلم

  1. Write lambda expressions and explain them as implementations of a functional interface.
  2. Use Predicate, Function, Consumer, Supplier, and BiFunction.
  3. Replace lambdas with method references where clearer.

الدروس

  1. 01تمرير السلوك كقيمة11 د قراءة
  2. 02صياغة Lambda12 د قراءة
  3. 03الواجهات الوظيفية القياسية10 د قراءة
  4. 04مراجع الدوال (Method references)10 د قراءة
  5. 05عرض عملي: استبدال الفئات المجهولة بتعبيرات lambda11 د قراءة

المختبر والتمارين

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)

IDOutcomeTask
BEG-035-EX011Convert an anonymous Runnable that prints "hello" into a lambda; explain which interface it implements.
BEG-035-EX022Write 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-EX033Rewrite as method references: s -> s.toUpperCase(), s -> System.out.println(s), () -> new ArrayList<String>().
BEG-035-EX041Write 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 implements Runnable, whose single method is run.
  • 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 (as Supplier<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.

اختبار ذاتي لك: لا توجد درجات، ولا يُحفظ أو يُرسل أي شيء. اختر إجابة ثم تحقق منها، ويمكنك المحاولة مرة أخرى كما تشاء.

  1. 1A functional interface has:O1
  2. 2(a, b) -> a + b is a:O1
  3. 3A lambda can use a local variable from outside if it is:O1
  4. 4Takes a value, returns boolean:O2
  5. 5Takes nothing, returns a value:O2
  6. 6Takes a value, returns nothing:O2
  7. 7Method name of Function:O2
  8. 8String::length is equal to:O3
  9. 9System.out::println is a reference to:O3
  10. 10If a lambda body needs ten lines, you should:O1

الجزء العملي

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.

اكتب للبحث في المستويات والمراحل والوحدات والدروس.