CodeBlanks Logo Icon CodeBlanks

Meydan okuma - Fonksiyonel zincir

JavaScript kursu · İleri seviye · Ders 97/100

Saf fonksiyonlar, kompozisyon ve curry ile bir metin işleme zinciri yazarsın.

Fonksiyonel zincir

Bu meydan okumada saf fonksiyonlardan oluşan bir zincir kuracaksın. Saf fonksiyonlar aynı girdi için her zaman aynı çıktıyı verir ve yan etkisi yoktur.

İki fonksiyonu birleştirmek için compose(f, g) yazarsın; dönen fonksiyon f(g(x)) hesaplar. Buna fonksiyon kompozisyonu denir.

Tek argümanlı fonksiyonu sırayla çağrılabilir hale getirmek için currying kullanırsın: curryAdd(a) bir fonksiyon döndürür, o da b alır ve a + b sonucunu verir.

Uygulama compose ve curryAdd fonksiyonlarını yaz

compose(f, g) yaz: bir x alan ve f(g(x)) döndüren fonksiyon geri verir. Sonra curryAdd(a) yaz: a alır ve b alıp a + b döndüren bir fonksiyon geri verir.

Örnek çözümü göster
function compose(f, g) { return function(x) { return f(g(x)); }; } ile başla. Sonra function curryAdd(a) { return function(b) { return a + b; }; } yaz.

Örnek çözüm:
function compose(f, g) {
  return function(x) {
    return f(g(x));
  };
}

function curryAdd(a) {
  return function(b) {
    return a + b;
  };
}

Kendini test et

  1. compose(f, g)(x) ne döndürür?

    function compose(f, g) {
      return function(x) {
        return f(g(x));
      };
    }
    • g(f(x))
    • f(x) sonra g(x)
    • f(g(x))
    • x(f, g)
    Cevabı göster

    f(g(x)) — Doğru! compose(f, g)(x) önce g'yi uygular, sonucu f'ye verir.

  2. compose fonksiyonunu tamamla

    function compose(f, g) {
      return function(x) {
        ____
      };
    }
    Cevabı göster
    function compose(f, g) {
      return function(x) {
        return f(g(x));
      };
    }
  3. Fonksiyonel kavramları eşleştir

    compose(f, g)
    f(g(x)) hesaplar
    curry(f)
    Adım adım argüman
    saf fonksiyon
    Yan etki yoktur
    higher-order
    Fonksiyon döndürür

İleri seviye bölümündeki tüm dersler