Reputation: 1745
I am new to the world of functional programming and am trying to work through a few points of confusion to me with Sanctuary.js. Here is the scenario, I would like to use a pipe to handle reading from a mongoose database, checking to make sure I did not get a null value and transforming the data that is returned in some fashion before returning to the client. Trying to think in terms of functional programming, it seems like I should use a Monad's Just/Nothing for handling nulls and map in the pipe for transforming the data. Here is an example using arrays to give a basic gist of what I am thinking:
//first scenario
const arrayList = [];
arrayList.push({
a: {
b: 1,
c: 2,
d: 3
}
})
const findData = (arrayList) => arrayList
const checkForNull = (arrItems) => arrItems === null ? S.Nothing : S.Just(arrItems)
const test = (arr) => console.log(arr)
let value = pipe([
findData,
checkForNull,
map(x => x.map(y => ({
a: {
b: y.a.b + 1,
c: y.a.c + 1,
d: y.a.d + 1
}
}))),
test
])
value(arrayList);
//2nd scenario
const findData2 = (index) => arrayList[index]
const checkForNull2 = (data) => data === null ? S.Nothing : S.Just(data)
let value2 = pipe([
findData2,
checkForNull2,
map(x => ({
a: {
b: x.a.b + 2,
c: x.a.c + 3,
d: x.a.d + 4
}
})),
test
])
value2(0);
The top scenario simulates an array of objects coming from a database and the second a single object. 1) If I am using Just/Nothing, what is the sanctuary method that will check for null and return Just if not null and nothing otherwise, like Maybe.toMaybe in ramda-fantasy. 2) I noticed in the sanctuary docs that there was a pipeP that has been deprecated. How should I handle promises returned in the pipeline? If for example findData in the first example returned Promise.resolve for the case above, how should this be handled now?
Upvotes: 1
Views: 351