A cikin duniyar shirye-shirye masu aiki, ra'ayoyi sun samo asali daban-daban. Daga cikin su, 'yan kaษan na iya zama kamar ba zato ba tsammani kuma suna da wuyar fahimta kamar wanda za mu ษata a yau - Mai aiki. Amma da zarar kun fahimci ฦarfinsa da sassauฦarsa, masu aiki za su iya sauฦaฦe hanyoyin magance matsalolin shirye-shirye masu rikitarwa.
Fahimtar Ayyuka a Haskell
A functor, a cikin Haskell, shine nau'in da ke aiwatar da aikin Mai aiki nau'in nau'i. Da gaske wani abu ne da za a iya tsara shi. Functor ya ฦunshi abubuwa na farko guda biyu, aikin 'fmap' da wasu dokoki waษanda ke tabbatar da daidaiton ษabi'a.
Ana amfani da 'fmap' don amfani da aiki zuwa ฦima a cikin tsari ba tare da canza tsarin kansa ba. Wannan yana nufin, masu aiki suna ษaukar mahallin aiwatar da ayyuka yayin da suke barin mahallin ba ya tasiri. Yana ba mu damar mai da hankali kan warware matsalolin ba tare da yin cudanya da mu'amala da mahallin ba.
instance Functor Maybe where fmap func (Just x) = Just (func x) fmap func Nothing = Nothing
Dokokin Functor
Don nau'in don cancanta a matsayin mai aiki, dole ne ya cika dokoki biyu da aka sani da Dokokin Ayyuka. Waษannan dokokin suna kiyaye amincin tsarin da muke aiki akai.
Doka 1: Taswirar aikin ainihi akan mai aiki yakamata ya dawo da ainihin ma'aikaci - fmap = id
fmap id (Just 3) -- Returns: Just 3 fmap id Nothing -- Returns: Nothing
Doka ta 2: Haษa ayyuka guda biyu sannan taswirar aikin da aka samu akan ma'aikaci ya zama iri ษaya da fara taswirar aiki ษaya sannan taswirar wani - fmap (f . g) == fmap f . fmap g
fmap (abs . negate) (Just 5) -- Returns: Just 5 (fmap abs . fmap negate) (Just 5) -- Returns: Just 5
Aiwatar da Ayyuka don Magance Matsaloli
Yanzu, bari mu kwatanta yadda Functors za su iya magance matsaloli yadda ya kamata. Misali, ka ce muna da jerin yuwuwar sakamako a matsayin jerin ฦima. Idan muna son haษaka kowane ษayan, a al'ada, dole ne mu rubuta lambar tukunyar jirgi da yawa. Shigar da Ayyuka.
'fmap' na iya yin jujjuya lissafin cikin sauฦi, ba tare da ฦetare ฦimar komai ba da amfani da aiki kawai zuwa ฦima. Duk yayin da yake riฦe ainihin tsari.
let maybes = [Just 1, Nothing, Just 3] fmap (+1) <$> maybes -- Returns: [Just 2, Nothing, Just 4]
Don haka, masu aiki suna ba da ingantacciyar hanya, mai hankali don sarrafa ฦididdiga a cikin mahallin. Suna samar da wani muhimmin sashi na ikon Haskell don gudanar da ayyuka masu rikitarwa tare da sauฦi, ฦayatacciyar lamba.