Example 1 — Official decodeString
Two promap layers: one decodes a letter, the other applies it to a whole word. This is the sample from the Ramda docs.
const decodeChar = R.promap((s) => s.charCodeAt(), String.fromCharCode, R.add(-8));
const decodeString = R.promap(R.split(''), R.join(''), R.map(decodeChar));
console.log(decodeString('ziuli')); // "ramda" How It Works
- decodeChar.
charCodeAtturns a letter into a number,R.add(-8)shifts it, andString.fromCharCodeturns it back. - Pre.
R.split('')turns'ziuli'into['z', 'i', 'u', 'l', 'i']. - Core.
R.map(decodeChar)decodes each letter →['r', 'a', 'm', 'd', 'a']. - Post.
R.join('')glues them back →'ramda'.
