Skip to content

Latest commit

 

History

History
27 lines (21 loc) · 921 Bytes

filtering-by-identity.md

File metadata and controls

27 lines (21 loc) · 921 Bytes

Filtering By Identity

I'm reading Functional-Light JavaScript by Kyle Simpson, and learning a lot! Today I learned about the functional programming utility known as 'identity'.

Identity is a unary function that simply returns its argument. A simple idea that can be powerfully applied, as JavaScript coerces the returned argument to boolean:

> const identity = (arg) => arg
> ["", false, "keep", null, undefined, "these"].filter(identity)
[ 'keep', 'these' ]

I've done something similar for years by filtering to boolean, or writing my own (I didn't know it had this name) anonymous identity function.

> ["", false, "keep", null, undefined, "these"].filter(Boolean)
[ 'keep', 'these' ]
> ["", false, "keep", null, undefined, "these"].filter(arg => arg)
[ 'keep', 'these' ]

I like that functional programming has a concept for this and am excited so see how it can be applied to future work.