MojiScript Map/Filter/Reduce example.
You could also use map
, filter
, reduce
from Sanctuary or Ramda or lodash/fp or any other library.
# clone repository
git clone https://github.com/joelnet/MojiScript.git
# enter directory
cd MojiScript/examples/map-filter-reduce
# install dependencies
npm ci
npm start
You should see the output
8
Basic test run
npm test
Tests with coverage
npm test -- --coverage
Tests are written in JavaScript using Jest and should follow Jest best practices.
import log from 'mojiscript/console/log'
import run from 'mojiscript/core/run'
import main from './main'
const dependencies = {
log
}
const state = [ 1, 2, 3 ]
run ({ dependencies, state, main }) // => 8
import pipe from 'mojiscript/core/pipe'
import filter from 'mojiscript/list/filter'
import map from 'mojiscript/list/map'
import reduce from 'mojiscript/list/reduce'
import { add, double, isOdd } from './lib/math'
// main :: Dependencies -> [Number] -> Number
const main = ({ log }) => pipe ([
filter (isOdd), // [1, 2, 3] => [1, 3]
map (double), // [1, 3] => [2, 6]
reduce (add) (0), // [2, 6] => 8,
log
])
export default main