DEV Community

Discussion on: Haskell for javascript programmers.

Collapse
 
bennypowers profile image
Benny Powers 🇮🇱🇨🇦 • Edited

Cool stuff. Haskell is such a pleasure to use.

A few notes:

No arrays in functional JS? Why the heck not?

const head = ([x, ...xs]) => x
const tail = ([x, ...xs]) => xs

head([1, 2, 3]) // 1
tail([1, 2, 3]) // [2, 3]
Enter fullscreen mode Exit fullscreen mode

I've done plenty of functional js that used the array built-in. Since dynamism is one of JS's strengths, why throw it out?

import { All, Any, mreduceMap, compose, propOr, not, isArray } from 'crocks'

const includes = x => xs => isArray(xs) && xs.includes(x)

const isAdmin = compose(includes('admin'), propOr([], 'roles'))

const areAllAdmins = mreduceMap(All, isAdmin)
const noneAreAdmins = not(mreduceMap(Any, isAdmin))

areAllAdmins([
  {name: 'Anton', roles: ['author', 'admin']},
  {name: 'Ben', roles: ['author', 'founder', 'admin']},
]) // true

noneAreAdmins([
  {name: 'Bill', roles: ['author']},
  {name: 'Ben', roles: ['author', 'founder', 'admin']},
]) // false
Enter fullscreen mode Exit fullscreen mode