Hacker News new | ask | show | jobs
by sudomakeup 2123 days ago
So, I'm a bit rusty on haskell but I have some notes on a similar concept. Essentially, fmap with a twist - instead of applying the same function to a list of values, you have a list of functions that you want to evaluate on the same value. "fpam". In this case, we're dealing with a list of size 2

  fpam :: [(a -> b)] -> a -> [b]
  fpam fns v = fns <*> pure v
after that then fold the list with boolean &&

  foo :: (a -> Bool) -> (a -> Bool) -> a -> Bool
  foo f g x = foldr1 (&&) ( fpam [f,g] x)
or alternatively with no helper functions

  foo2 :: (a -> Bool) -> (a -> Bool) -> a -> Bool
  foo2 f g x = foldr1 (&&) ( [f,g] <*> pure x)
for example

  Prelude> foldr1  (&&) ( [ (==3), (==4) ]  <*> pure 3 )
  False
Alternate implementations:

    import Data.List
    import Data.Function
    import Control.Monad.State
    import Data.Foldable
    import Control.Arrow((>>>))
    import Control.Monad.Reader
    import Control.Monad.List

    applyList :: [(a -> a)] -> a -> a
    applyList list = execState $ for_ list modify

    applyList2 :: [(a -> a)] -> a -> a
    applyList2 = foldr1 (>>>) 

    fpam :: [(a -> b)] -> a -> [b]
    fpam fns v = fns <*> pure v

    fpam2 :: [(a -> b)] -> a -> [b]
    fpam2 fns = runReader $ forM fns reader

    fpam3 :: [(a -> b)] -> a -> [b]
    fpam3 fns v = fmap (\f -> f v) fns

    fpam4 :: [(a -> b)] -> a -> [b]
    fpam4 fns = runReaderT $ do
      fn <- lift fns
      reader fn
2 comments

Catching up on comments and kind of fuzzy, but this is pretty cool :)

    foldr1  (&&) ( [ (==3), (==4) ]  <*> pure 3 )