Belajar Functional Programming JavaScript
  • Introduction
  • Bab 01: Apa Yang Pernah Kita Lakukan?
  • Bab 02: Fungsi Kelas Satu (First Class Function)
  • Bab 03: Kebahagiaan Murni dengan Fungsi Murni (Pure Function)
  • Bab 04: Currying (Kari)
  • Bab 05: Coding dengan Composing
  • Bab 06: Contoh Aplikasi
  • Bab 07: Hindley-Milner dan Saya
  • Bab 08: Tupperware
  • Bab 09: Monadic Onions
  • Bab 10: Aplikatif Functors
  • Bab 11: Transformasi Lagi, Secara Alami
  • Bab 12: Melintasi Batu
  • Bab 13: Monoid Menyatukan Semuanya
  • Lampiran A: Dukungan Fungsi Esensial
  • Lampiran B: Dukungan Struktur Aljabar
  • Lampiran C: Utilitas Pointfree
Powered by GitBook
On this page
  • always
  • compose
  • curry
  • either
  • identity
  • inspect
  • left
  • liftA2
  • liftA3
  • maybe
  • nothing
  • reject

Was this helpful?

Lampiran A: Dukungan Fungsi Esensial

PreviousBab 13: Monoid Menyatukan SemuanyaNextLampiran B: Dukungan Struktur Aljabar

Last updated 3 years ago

Was this helpful?

Dalam lampiran ini, Anda akan menemukan beberapa implementasi JavaScript dasar dari berbagai fungsi yang dijelaskan dalam buku ini.

Ingatlah bahwa implementasi ini mungkin bukan implementasi tercepat atau paling efisien di luar sana; mereka dibuat hanya untuk tujuan pendidikan.

Untuk menemukan fungsi yang lebih siap produksi, lihat , , atau .

Perhatikan bahwa beberapa fungsi juga mengacu pada struktur aljabar yang didefinisikan dalam

always

// always :: a -> b -> a
const always = curry((a, b) => a);

compose

// compose :: ((y -> z), (x -> y),  ..., (a -> b)) -> a -> z
const compose =
  (...fns) =>
  (...args) =>
    fns.reduceRight((res, fn) => [fn.call(null, ...res)], args)[0];

curry

// curry :: ((a, b, ...) -> c) -> a -> b -> ... -> c
function curry(fn) {
  const arity = fn.length;

  return function $curry(...args) {
    if (args.length < arity) {
      return $curry.bind(null, ...args);
    }

    return fn.call(null, ...args);
  };
}

either

// either :: (a -> c) -> (b -> c) -> Either a b -> c
const either = curry((f, g, e) => {
  if (e.isLeft) {
    return f(e.$value);
  }

  return g(e.$value);
});

identity

// identity :: x -> x
const identity = (x) => x;

inspect

// inspect :: a -> String
const inspect = (x) => {
  if (x && typeof x.inspect === "function") {
    return x.inspect();
  }

  function inspectFn(f) {
    return f.name ? f.name : f.toString();
  }

  function inspectTerm(t) {
    switch (typeof t) {
      case "string":
        return `'${t}'`;
      case "object": {
        const ts = Object.keys(t).map((k) => [k, inspect(t[k])]);
        return `{${ts.map((kv) => kv.join(": ")).join(", ")}}`;
      }
      default:
        return String(t);
    }
  }

  function inspectArgs(args) {
    return Array.isArray(args)
      ? `[${args.map(inspect).join(", ")}]`
      : inspectTerm(args);
  }

  return typeof x === "function" ? inspectFn(x) : inspectArgs(x);
};

left

// left :: a -> Either a b
const left = (a) => new Left(a);

liftA2

// liftA2 :: (Applicative f) => (a1 -> a2 -> b) -> f a1 -> f a2 -> f b
const liftA2 = curry((fn, a1, a2) => a1.map(fn).ap(a2));

liftA3

// liftA3 :: (Applicative f) => (a1 -> a2 -> a3 -> b) -> f a1 -> f a2 -> f a3 -> f b
const liftA3 = curry((fn, a1, a2, a3) => a1.map(fn).ap(a2).ap(a3));

maybe

// maybe :: b -> (a -> b) -> Maybe a -> b
const maybe = curry((v, f, m) => {
  if (m.isNothing) {
    return v;
  }

  return f(m.$value);
});

nothing

// nothing :: Maybe a
const nothing = Maybe.of(null);

reject

// reject :: a -> Task a b
const reject = (a) => Task.rejected(a);
ramda
lodash
folktale
Lampiran B