DEV Community

Discussion on: Monad Say What? (Part 1)

 
evilsoft profile image
Ian Hofmann-Hicks • Edited

So good.

So there are a couple things you've shown me I need to do. First of I need to update:

These posts assume an understanding of "currying", "partial application" and "function composition". If you feel a bit fuzzy on these topics, there are many resources available on the webs to get you sorted out.

To read more like:

These posts assume an understanding of not only the JavaScript language, but how "currying", "partial application" and "function composition" is accomplished in Javascript....

Also good call on the needing of crocks for the POJ example. I need to break those out into functions like:

// isArray :: a -> Boolean
const isArray =
  Array.isArray

// isString :: a -> Boolean
const isString = x =>
  typeof x === 'string'

// isObject :: a -> Boolean
const isObject = x =>
  !!x && Object.prototype.toString.call(x) === '[object Object]'

Yeah people should not have to install crocks at this stage in the game.

When the data is an Array with at least one acceptable record, an empty Object will be returned.

How about

Unless the data is an Array with at least one acceptable record, an empty Object will be returned.

Arrays in JS can be of mixed type, so we typically denote that with [ * ], as opposed to something like [ a ] where we expect everything to be that a. Well when our functions can work with mixed typed Arrays. In reality we want to strive for [ a ] or even better something like [ Number ], but sometimes at the edge, we need the [ * ]

You got mostly the compose bits down, except you would just use compose, composeK is for Kleisli arrows ( basically >=> or fish in Haskell). Also you can use either compose or composeK like this:

// x => fn1(fn2(fn3(x)))
const fn =
  compose(fn1, fn2, fn3)

My immediate response is why would I use reduce when I just need to filter.

So this is more than just a filter, we are change types. we move from an Array to an Object. So we want to reduce with {} as our empty on the fold, then only add valid records keyed by their valid id to the accumulator. Does that make sense?

Thread Thread
 
evilsoft profile image
Ian Hofmann-Hicks • Edited

Also just to help a fellow Haskeller out here is a quick JS -> Haskell key:

  • compose -> . (but compose is n-Ary)
  • composeK -> >=> (but composeK is n-Ary)
  • [type].of -> pure for Applicative Functor / return for Monad
  • [instance].chain -> bind
  • liftA2 -> liftA2
  • converge -> S' or Phoenix Combinator.
Thread Thread
 
antonrich profile image
Anton

I mostly don't know about them. So you are telling me new stuff that I should learn : ))) That's great. Thanks.