joelnet / MojiScript

MojiScript is an async-first, opinionated, and functional library

Home Page:https://mojiscript.js.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

map should work with functors

joelnet opened this issue · comments

Map should work with functors and support fantasy-land spec.

This is an easy one. Modify map to check if it's an Iterable, if it is, leave call the existing function. If not, check for fantasy-land/map or map, if those are functions, execute it and return the value.

Code:

import map from 'mojiscript/list/map'

const functorA = {
  value: 3,
  'fantasy-land/map': function(f) { return f(this.value)}
}

const functorB = {
  value: 3,
  map: function(f) { return f(this.value)}
}

const double = x => x * 2

Actual:

map (x => x * 2) (functorA) //=> iterable[Symbol.iterator] is not a function

Expected:

map (x => x * 2) (functorA) //=> 6

Test

test('functor', () => {
  expect.assertions(1)
  const expected = 4
  const functor = () => ({
    map: func => func(2)
  })
  const actual = map(double)(functor)
  expect(actual).toMatchObject(expected)
})

test('functor fantasy-land', () => {
  expect.assertions(1)
  const expected = 4
  const functor = () => ({
   'fantasy-land/map': func => func(2)
  })
  const actual = map(double)(functor)
  expect(actual).toMatchObject(expected)
})