Functional Libraries in JS

Wai Lin Kyaw
3 min readOct 20, 2021
lambda symbol

Ramda

သူကတော့ utility functions တွေ အများဆုံး ပေးထားတဲ့ library တစ်ခုပါ။ သူ့ရဲ့ functions တွေက curried လုပ်ပြီးသား ဖြစ်တယ်။ immutability ရမယ်၊ နောက် တစ်ချို့ functions တွေက compose လုပ်တဲ့နေရာမှာ transducer အနေနဲ့ အလုပ်လုပ်တာ ရှိတယ်။ downside က သူ့ရဲ့ function တွေက garbage in -> garbage out style နဲ့ အလုပ်လုပ်တာပါ။

Folktale

သူကလည်း JS Functional world မှာ standard library တစ်ခုလို့ ပြောလို့ရတယ်။ documentation ရော သူ့ရဲ့ design ရောကောင်းတယ်။ pure error handling အတွက် Maybe/Result စတဲ့ ADT type တွေ support လုပ်ထားတာရှိတယ်။ validation အတွက် Either, Validation စတဲ့ ကောင်တွေ နောက် concurrency နဲ့ async programming အတွက် Task ADT လည်းပါတယ်။ သူကတော့ အလွယ်ပြောရရင်တော့ Promise လိုပဲ ဒါပေမဲ့ လုံးဝ pure ဖြစ်တယ်။ တကယ့် pure functional setting နဲ့ သွားမယ်ဆိုရင် promise အစား Task သုံးလို့ရတယ်။ အဲ့ဒီ အပြင်ကို promise တွေက cancel လုပ်လို့မရဘူး။ ဒီမှာက cancellation support လုပ်တယ်။

rubico

ဒီကောင်က functional နဲ့ asynchronous code တွေရောရေးတဲ့ အခါမှာ အလုပ်ရှုပ်သက်သာအောင် လုပ်ပေးတဲ့ သဘောပဲ။ library က သိပ်ပြီး mature တော့ မဖြစ်သေးဘူး။ asynchronous လို့ဆိုတဲ့နေရာမှာ promise တစ်ခုထဲကို ဆိုလိုတာပါ။ downside က async နဲ့ sync ရောရေးတယ် ဆိုပါစို့။ သူက synchronous function တွေကိုလည်း promise chain ထဲထည့်ပြီး run ပစ်တာပါ။

crocks

JS မှာက ADT တွေကောင်းကောင်း support လုပ်တဲ့ library ရှားပါတယ်၊ မရှိတာတော့ မဟုတ်ပါဘူး။ crocksjs က ADT တွေ တော်တော်များများ ပါတယ်၊ ဥပမာ အားဖြင့် Async (aka Task), Either, Maybe နောက် State, Reader စတာတွေလည်း support လုပ်တယ်။ တစ်ခြား type တွေလည်း ပါတယ်။ အသုံးများတဲ့ Monoid တွေလည်းပါတယ်။ Logic, Combinators, Predicate စတဲ့ category အလိုက် utility functions တွေလည်းပါတယ်။ type တွေ တစ်ခုကနေ တစ်ခုပြောင်းတဲ့ အခါမှာ လိုတဲ့ natural transformation functions တွေ လည်းပါတယ်။ doc မှာသွားကြည့်လိုက်ရင် စုံပါတယ်။

သူလည်း ramda, folktale တို့လောက် mature မဖြစ်သေးပေမဲ့ စောင့်ကြည့်ရမယ့် အထဲမှာပါပါတယ်။

monet.js

သူကတော့ monad တွေ အတွက် library ပေါ့။ Maybe, Either, Validation, IO, Immutable List, Reader,Free စတဲ့ type တွေပါတယ်။ monad သီးသန့် library လိုချင်ရင် ဒီကောင် သုံးသင့်ပါတယ်။

Fluture

အပေါ်မှာ ပြောခဲ့တဲ့ Task ကို နာမည် အမျိုးမျိုး ခေါ်ကြတာရှိတယ်၊ Future, Task, crock မှာဆို Async စသည်ဖြင့်။ ဒီကောင်ကလည်း Task ပါပဲ နာမည်ကို Fluture လို့ပေးထားတာပဲ ရှိတာပါ။ အားသာချက်ကတော့ async heavy ဖြစ်တဲ့ project တွေမှာ သုံးလို့ရတယ်။ Node ရော browser ရော support လုပ်တယ်။ Functor, Bi-Functor, Monad, Applicative, Alt စတဲ့ interfaces တွေ satisfy ဖြစ်တယ်။ Fantasy Land spec နဲ့လည်း ကိုက်တော့ တစ်ခြား lib တွေနဲ့ တွဲသုံးလို့ရတယ်။

promise <-> future အပြန်အလှန် transform လုပ်လို့ရတယ်။ resource management လည်း ပါတယ်။ Folktale က Task နဲ့ ယှဉ်ရင်တော့ Folktake Task က auto caching ပါတယ် သူကမပါဘူး။ exception catch လုပ်တာတို့ error handling တို့မှာတော့ တစ်ခြား ကောင်တွေထက်ပိုအဆင်ပြေတယ်။

Sanctuary

ဒီကောင်ကတော့ fp library တွေထဲမှာ ရေးရတာ strict အဖြစ်ဆုံးပဲ။ သူက Haskell နဲ့ PureScript ကနေ inspired ယူထားတာလို့ဆိုတယ်။ Type class တွေသိရင် ပိုကောင်းတယ်၊ သိဖို့လိုတယ်။ နောက် သူက ramda တို့လို garbage in -> garbage out style နဲ့သွားတာမဟုတ်ဘူး။ ramda မှာ တစ်ချို့နေရာမှာတွေ this binding သုံးတာ နောက် အပေါ်မှာ ပြောခဲ့သလို transducer လိုမျိုးတွေ sanctuary မှာ မရှိပါဘူး။

Currying မှာလည်း ramda မှာဆို curried function ကို argument နှစ်ခု သုံးခု တစ်ပြိုင်တည်း ပေးလို့ရတယ်။ argumnt order ကို ကျော်ပြီးပေးချင်ရင် gap ပေါ့ R.__ လိုကောင်မျိုးတွေပေးလို့ရတယ်။ sanctuary မှာကျတော့ တစ်ကြိမ်မှာ တစ်ခုပဲ allow လုပ်တယ်။ အပေါ်မှာ ပြောခဲ့တဲ့ Fluture ကလည်း sanctuary compatible library ပါ။

ဆိုပါတော့ Ramda နဲ့ object တစ်ခုရဲ့ property တစ်ခုကိုယူမယ်၊ Maybe နဲ့ null check လုပ်ချင်တယ်ဆိုရင်

R.compose(Maybe.of, R.prop(‘name’)); // Maybe String

ဒီလိုရေးရမယ်။ sanctuary ရဲ့ function တွေက ဥပမာ prop ရှိမယ်ဆိုရင်

S.prop(‘name’); // Maybe String

အားသာချက်က Type safe ဖြစ်တယ်။ တစ်ခုရှိတာကတော့ sanctuary သုံးပြီဆိုရင် ဖြစ်နိုင်ခြေ ရှိသမျှ exception တွေ type checking တွေ handle လုပ်ရမယ်။ ပိုပြီး strict ဖြစ်တယ်။

long term မှာတော့ ဒီလို type safe ဖြစ်တာက စိတ်ချရပါတယ်။

sanctuary အောက်မှာလည်း သူ့ရဲ့ component တွေအနေနဲ့ တစ်ခြား type တွေ ရှိတယ်။ ဥပမာ Maybe, Either…

ဒီထဲမှာ ပါတာတွေ အကုန်လုံးနီးပါး ပါတဲ့ library တစ်ခုရှိတယ်။ fp-ts သူလည်း သိသင့်အထဲမှာ ပါပါတယ်။

ဘာသုံးမယ်ဆိုတာကတော့ အားသားချက် အားနည်းချက် အလိုက် ကိုယ်ရေးမဲ့ project ပေါ်မူတည်ပြီး ဆုံးဖြတ်ရမှာပါ

--

--