-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Chapter 8 references 'find' function that is not present in the appendix #564
Comments
I think you're right :) |
Assuming ECMAScript 2015 (ES6) the example works with the following implementation of // find :: Pred a => a -> [b] -> b
const find = curry((fn, list) => list.find(fn)) |
Here is a // find :: (a -> Boolean) -> [a] -> a
const find = curry(function find(fn, xs) {
assert(
typeof fn === 'function' && Array.isArray(xs),
typeMismatch('(a -> Boolean) -> [a] -> a', [getType(fn), getType(xs), getType(xs)].join(' -> '), 'find'),
);
return xs.find(fn);
}); |
Note, that the example does not currently work with the support.js file for the book. Current example: // url :: IO String
const url = new IO(() => window.location.href);
// toPairs :: String -> [[String]]
const toPairs = compose(map(split('=')), split('&'));
// params :: String -> [[String]]
const params = compose(toPairs, last, split('?'));
// findParam :: String -> IO Maybe [String]
const findParam = key => map(compose(Maybe.of, find(compose(eq(key), head)), params), url);
// -- Impure calling code ----------------------------------------------
// run it by calling $value()!
findParam('searchTerm').$value();
// Just(['searchTerm', 'wafflehouse']) Changes needed for the example to work (assuming 'use strict'
const {
IO,
compose,
map,
split,
last,
Maybe,
find,
eq,
head,
} = require('../support')
const window = {}
window.location = {}
window.location.href = 'https://www.example.com/foo?bar=yes&searchTerm=wafflehouse&wedonot=care'
// url :: IO String
const url = new IO(() => window.location.href);
// toPairs :: String -> [[String]]
const toPairs = compose(map(split('=')), split('&'));
// params :: String -> [[String]]
const params = compose(toPairs, last, split('?'));
// findParam :: String -> IO Maybe [String]
const findParam = key => map(compose(Maybe.of, find(compose(eq(key), head)), params), url);
// -- Impure calling code ----------------------------------------------
// run it by calling .unsafePerformIO()!
// findParam('searchTerm').unsafePerformIO();
const result = findParam('searchTerm').unsafePerformIO();
console.log(result) // Just(['searchTerm', 'wafflehouse']) |
Oh no. There is 2 support.js files. One that is actually used, in this repo and What is the correct place to add missing functions? Is there any reason to have This is confusing and also the reason for #506 |
find is referenced in chapter 8 and is needed for the IO example: // findParam :: String -> IO Maybe [String] const findParam = key => map(compose(Maybe.of, find(compose(eq(key), head)), params), url);
find is referenced in chapter 8 and is needed for the IO example: // findParam :: String -> IO Maybe [String] const findParam = key => map(compose(Maybe.of, find(compose(eq(key), head)), params), url);
Chapter 8 has a section that references a
find
function:But there does not appear to be a
find
function in the Appendix. I believe this may have referencedfilter
before (per issue #303) and there is a definition offind
in there, so perhaps it just needs to be added to appendix?Thanks for great book - enjoying very much so far!
The text was updated successfully, but these errors were encountered: